001 package com.mockrunner.mock.connector.cci; 002 003 import java.io.IOException; 004 import java.io.InputStream; 005 import java.io.OutputStream; 006 007 import javax.resource.cci.Streamable; 008 009 import com.mockrunner.base.NestedApplicationException; 010 import com.mockrunner.util.common.ArrayUtil; 011 import com.mockrunner.util.common.StreamUtil; 012 013 /** 014 * Streamable <code>Record</code> backed by a byte array. 015 */ 016 public class MockStreamableByteArrayRecord extends MockRecord implements Streamable 017 { 018 private byte[] content; 019 020 public MockStreamableByteArrayRecord() 021 { 022 this(MockStreamableByteArrayRecord.class.getName()); 023 } 024 025 public MockStreamableByteArrayRecord(String name) 026 { 027 this(name, name); 028 } 029 030 public MockStreamableByteArrayRecord(String name, String description) 031 { 032 super(name, description); 033 content = null; 034 } 035 036 /** 037 * Returns a copy of the underlying byte array. 038 * @return the underlying data 039 */ 040 public byte[] getContent() 041 { 042 if(null == content) return null; 043 return (byte[])content.clone(); 044 } 045 046 /** 047 * Sets the content of this record. The specified array 048 * will be copied. 049 * @param content the content 050 */ 051 public void setContent(byte[] content) 052 { 053 if(null == content) 054 { 055 this.content = null; 056 } 057 else 058 { 059 this.content = (byte[])content.clone(); 060 } 061 } 062 063 public void read(InputStream stream) throws IOException 064 { 065 if(null == stream) 066 { 067 this.content = null; 068 } 069 else 070 { 071 this.content = StreamUtil.getStreamAsByteArray(stream); 072 } 073 } 074 075 public void write(OutputStream stream) throws IOException 076 { 077 if(null != stream && null != this.content) 078 { 079 stream.write(this.content); 080 stream.flush(); 081 } 082 } 083 084 public boolean equals(Object object) 085 { 086 if(!super.equals(object)) return false; 087 MockStreamableByteArrayRecord other = (MockStreamableByteArrayRecord)object; 088 return ArrayUtil.areArraysEqual(content, other.content); 089 } 090 091 public int hashCode() 092 { 093 return super.hashCode() * 31 + ArrayUtil.computeHashCode(content); 094 } 095 096 public Object clone() 097 { 098 try 099 { 100 MockStreamableByteArrayRecord clone = (MockStreamableByteArrayRecord)super.clone(); 101 clone.setContent(this.content); 102 return clone; 103 } 104 catch(Exception exc) 105 { 106 throw new NestedApplicationException(exc); 107 } 108 } 109 }