01: package com.mockrunner.mock.connector.cci;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.OutputStream;
06:
07: import javax.resource.cci.Streamable;
08:
09: import com.mockrunner.base.NestedApplicationException;
10: import com.mockrunner.util.common.ArrayUtil;
11: import com.mockrunner.util.common.StreamUtil;
12:
13: /**
14: * Streamable <code>Record</code> backed by a byte array.
15: */
16: public class MockStreamableByteArrayRecord extends MockRecord implements
17: Streamable {
18: private byte[] content;
19:
20: public MockStreamableByteArrayRecord() {
21: this (MockStreamableByteArrayRecord.class.getName());
22: }
23:
24: public MockStreamableByteArrayRecord(String name) {
25: this (name, name);
26: }
27:
28: public MockStreamableByteArrayRecord(String name, String description) {
29: super (name, description);
30: content = null;
31: }
32:
33: /**
34: * Returns a copy of the underlying byte array.
35: * @return the underlying data
36: */
37: public byte[] getContent() {
38: if (null == content)
39: return null;
40: return (byte[]) content.clone();
41: }
42:
43: /**
44: * Sets the content of this record. The specified array
45: * will be copied.
46: * @param content the content
47: */
48: public void setContent(byte[] content) {
49: if (null == content) {
50: this .content = null;
51: } else {
52: this .content = (byte[]) content.clone();
53: }
54: }
55:
56: public void read(InputStream stream) throws IOException {
57: if (null == stream) {
58: this .content = null;
59: } else {
60: this .content = StreamUtil.getStreamAsByteArray(stream);
61: }
62: }
63:
64: public void write(OutputStream stream) throws IOException {
65: if (null != stream && null != this .content) {
66: stream.write(this .content);
67: stream.flush();
68: }
69: }
70:
71: public boolean equals(Object object) {
72: if (!super .equals(object))
73: return false;
74: MockStreamableByteArrayRecord other = (MockStreamableByteArrayRecord) object;
75: return ArrayUtil.areArraysEqual(content, other.content);
76: }
77:
78: public int hashCode() {
79: return super .hashCode() * 31
80: + ArrayUtil.computeHashCode(content);
81: }
82:
83: public Object clone() {
84: try {
85: MockStreamableByteArrayRecord clone = (MockStreamableByteArrayRecord) super
86: .clone();
87: clone.setContent(this .content);
88: return clone;
89: } catch (Exception exc) {
90: throw new NestedApplicationException(exc);
91: }
92: }
93: }
|