1 19 package com.mysql.jdbc; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import java.sql.SQLException ; 26 27 28 47 public class Blob implements java.sql.Blob , OutputStreamWatcher { 48 50 51 private ResultSet creatorResultSet; 52 53 59 60 private byte[] binaryData = null; 61 62 63 private int columnIndex; 64 65 67 70 Blob(byte[] data) { 71 setBinaryData(data); 72 this.creatorResultSet = null; 73 this.columnIndex = 0; 74 } 75 76 80 Blob(byte[] data, ResultSet creatorResultSet, int columnIndex) { 81 setBinaryData(data); 82 this.creatorResultSet = creatorResultSet; 83 this.columnIndex = columnIndex; 84 } 85 86 88 91 public OutputStream setBinaryStream(long indexToWriteAt) 92 throws SQLException { 93 if (indexToWriteAt < 1) { 94 throw new SQLException ("indexToWriteAt must be >= 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 95 } 96 97 WatchableOutputStream bytesOut = new WatchableOutputStream(); 98 bytesOut.setWatcher(this); 99 100 if (indexToWriteAt > 0) { 101 bytesOut.write(this.binaryData, 0, (int) (indexToWriteAt - 1)); 102 } 103 104 return bytesOut; 105 } 106 107 114 public java.io.InputStream getBinaryStream() throws SQLException { 115 return new ByteArrayInputStream (getBinaryData()); 116 } 117 118 121 public int setBytes(long writeAt, byte[] bytes, int offset, int length) 122 throws SQLException { 123 OutputStream bytesOut = setBinaryStream(writeAt); 124 125 try { 126 bytesOut.write(bytes, offset, length); 127 } catch (IOException ioEx) { 128 throw new SQLException ("IO Error while writing bytes to blob", 129 SQLError.SQL_STATE_GENERAL_ERROR); 130 } finally { 131 try { 132 bytesOut.close(); 133 } catch (IOException doNothing) { 134 ; } 136 } 137 138 return length; 139 } 140 141 144 public int setBytes(long writeAt, byte[] bytes) throws SQLException { 145 return setBytes(writeAt, bytes, 0, bytes.length); 146 } 147 148 160 public byte[] getBytes(long pos, int length) throws SQLException { 161 if (pos < 1) { 162 throw new SQLException ("Position 'pos' can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 163 } 164 165 byte[] newData = new byte[length]; 166 System.arraycopy(getBinaryData(), (int) (pos - 1), newData, 0, length); 167 168 return newData; 169 } 170 171 179 public long length() throws SQLException { 180 return getBinaryData().length; 181 } 182 183 194 public long position(java.sql.Blob pattern, long start) 195 throws SQLException { 196 return position(pattern.getBytes(0, (int) pattern.length()), start); 197 } 198 199 202 public long position(byte[] pattern, long start) throws SQLException { 203 throw new SQLException ("Not implemented"); 204 } 205 206 209 public void streamClosed(byte[] byteData) { 210 this.binaryData = byteData; 211 } 212 213 216 public void truncate(long arg0) throws SQLException { 217 throw new NotImplemented(); 218 } 219 220 private void setBinaryData(byte[] binaryData) { 221 this.binaryData = binaryData; 222 } 223 224 private byte[] getBinaryData() { 225 return binaryData; 226 } 227 } 228 | Popular Tags |