001 package com.mockrunner.mock.jdbc;
002
003 import java.io.ByteArrayInputStream;
004 import java.io.IOException;
005 import java.io.InputStream;
006 import java.io.OutputStream;
007 import java.sql.Blob;
008 import java.sql.SQLException;
009 import java.util.ArrayList;
010 import java.util.List;
011
012 import org.apache.commons.logging.Log;
013 import org.apache.commons.logging.LogFactory;
014
015 import com.mockrunner.util.common.ArrayUtil;
016 import com.mockrunner.util.common.CollectionUtil;
017
018 /**
019 * Mock implementation of <code>Blob</code>.
020 */
021 public class MockBlob implements Blob, Cloneable
022 {
023 private final static Log log = LogFactory.getLog(MockBlob.class);
024 private List blobData;
025
026 public MockBlob(byte[] data)
027 {
028 blobData = ArrayUtil.getListFromByteArray(data);
029 }
030
031 public long length() throws SQLException
032 {
033 return blobData.size();
034 }
035
036 public byte[] getBytes(long pos, int length) throws SQLException
037 {
038 if(pos < 0) pos = 0;
039 return ArrayUtil.getByteArrayFromList(blobData, (int)(pos - 1), length);
040 }
041
042 public InputStream getBinaryStream() throws SQLException
043 {
044 return new ByteArrayInputStream(ArrayUtil.getByteArrayFromList(blobData));
045 }
046
047 public long position(byte[] pattern, long start) throws SQLException
048 {
049 byte[] data = ArrayUtil.getByteArrayFromList(blobData);
050 int index = ArrayUtil.indexOf(data, pattern, (int)(start - 1));
051 if(-1 != index) index += 1;
052 return index;
053 }
054
055 public long position(Blob pattern, long start) throws SQLException
056 {
057 return position(pattern.getBytes(1, (int)pattern.length()), start);
058 }
059
060 public int setBytes(long pos, byte[] bytes) throws SQLException
061 {
062 if(pos < 0) pos = 0;
063 ArrayUtil.addBytesToList(bytes, blobData, (int)(pos - 1));
064 return bytes.length;
065 }
066
067 public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException
068 {
069 if(pos < 0) pos = 0;
070 ArrayUtil.addBytesToList(bytes, offset, len, blobData, (int)(pos - 1));
071 return len;
072 }
073
074 public OutputStream setBinaryStream(long pos) throws SQLException
075 {
076 return new BlobOutputStream((int)(pos - 1));
077 }
078
079 public void truncate(long len) throws SQLException
080 {
081 blobData = CollectionUtil.truncateList(blobData, (int)len);
082 }
083
084 private class BlobOutputStream extends OutputStream
085 {
086 private int index;
087
088 public BlobOutputStream(int index)
089 {
090 this.index = index;
091 }
092
093 public void write(int byteValue) throws IOException
094 {
095 byte[] bytes = new byte[] {(byte)byteValue};
096 ArrayUtil.addBytesToList(bytes, blobData, index);
097 index++;
098 }
099 }
100
101 public boolean equals(Object obj)
102 {
103 if(null == obj) return false;
104 if(!obj.getClass().equals(this.getClass())) return false;
105 MockBlob other = (MockBlob)obj;
106 return blobData.equals(other.blobData);
107 }
108
109 public int hashCode()
110 {
111 return blobData.hashCode();
112 }
113
114 public String toString()
115 {
116 StringBuffer buffer = new StringBuffer("Blob data: ");
117 for(int ii = 0; ii < blobData.size(); ii++)
118 {
119 buffer.append("[" + blobData.get(ii).toString() + "] ");
120 }
121 return buffer.toString();
122 }
123
124 public Object clone()
125 {
126 try
127 {
128 MockBlob blob = (MockBlob)super.clone();
129 blob.blobData = new ArrayList(blobData);
130 return blob;
131 }
132 catch(CloneNotSupportedException exc)
133 {
134 log.error(exc.getMessage(), exc);
135 }
136 return null;
137 }
138 }