1 23 24 package org.objectweb.cjdbc.driver; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.Serializable ; 28 import java.sql.SQLException ; 29 30 import org.objectweb.cjdbc.common.exceptions.NotImplementedException; 31 import org.objectweb.cjdbc.common.exceptions.driver.DriverSQLException; 32 33 50 public class Blob implements java.sql.Blob , Serializable 51 { 52 53 byte[] internalArray; 54 55 56 58 61 public long length() throws SQLException 62 { 63 checkInitialized(); 64 return internalArray.length; 65 } 66 67 70 public byte[] getBytes(long sqlPos, int length) throws SQLException 71 { 72 checkInitialized(); 73 checkSQLRangeIsSupported(sqlPos, length); 74 75 int arrayPos = (int) (sqlPos - 1); 76 return resizedByteArray(internalArray, arrayPos, Math.min(length, internalArray.length - arrayPos)); } 80 81 84 public java.io.InputStream getBinaryStream() throws SQLException 85 { 86 checkInitialized(); 87 return new ByteArrayInputStream (internalArray); 88 } 89 90 93 public long position(byte[] pattern, long sqlStart) throws SQLException 94 { 95 checkInitialized(); 96 checkSQLRangeIsSupported(sqlStart, 0); 97 98 throw new NotImplementedException("position not yet implemented"); 99 } 100 101 104 public long position(java.sql.Blob pattern, long sqlStart) 105 throws SQLException 106 { 107 checkInitialized(); 108 checkSQLRangeIsSupported(sqlStart, 0); 109 110 return position(pattern.getBytes(0, (int) pattern.length()), sqlStart); 112 } 113 114 116 119 public int setBytes(long sqlStartPos, byte[] srcArray) throws SQLException 120 { 121 return this.setBytes(sqlStartPos, srcArray, 0, srcArray.length); 122 } 123 124 127 public int setBytes(long sqlStartPos, byte[] srcArray, int srcArrayOffset, 128 int copiedLength) throws SQLException 129 { 130 checkInitialized(); 131 checkSQLRangeIsSupported(sqlStartPos, copiedLength); 132 133 int minimumLengthNeeded = (int) (sqlStartPos - 1) + copiedLength; 134 135 if (this.length() < minimumLengthNeeded) 138 internalArray = resizedByteArray(internalArray, 0, minimumLengthNeeded); 139 140 143 System.arraycopy(srcArray, srcArrayOffset, internalArray, 147 (int) (sqlStartPos - 1), copiedLength); 148 149 153 return copiedLength; 154 } 155 156 159 public java.io.OutputStream setBinaryStream(long sqlStart) 160 throws SQLException 161 { 162 checkInitialized(); 163 checkSQLRangeIsSupported(sqlStart, 0); 164 165 return new BlobOutputStream(this, (int) (sqlStart - 1)); 166 } 167 168 171 public void truncate(long newLen) throws SQLException 172 { 173 checkInitialized(); 174 175 if (newLen >= this.length()) 176 return; 177 178 internalArray = resizedByteArray(internalArray, 0, (int) newLen); 179 } 180 181 185 public void free() 186 { 187 internalArray = null; 188 } 189 190 192 198 public Blob(byte[] src) 199 { 200 this.internalArray = resizedByteArray(src, 0, src.length); 202 } 203 204 207 byte[] getInternalByteArray() 208 { 209 return internalArray; 210 } 211 212 private void checkInitialized() throws DriverSQLException 213 { 214 if (null == internalArray) 215 throw new DriverSQLException("Blob has been freed"); 216 } 217 218 228 private static void checkSQLRangeIsSupported(long sqlStart, int len) 229 throws SQLException 230 { 231 long arrayStart = sqlStart - 1; 232 if (arrayStart < 0) 233 { 234 throw new DriverSQLException("Illegal argument: start of Blob (" 235 + sqlStart + ") cannot be less than 1"); 236 } 237 if (Integer.MAX_VALUE <= arrayStart + len) 238 { 239 throw new NotImplementedException("End of Blob (" + (sqlStart + len) 240 + ") is too great. Blobs greater than " + Integer.MAX_VALUE 241 + " are not supported"); 242 } 243 } 244 245 253 private byte[] resizedByteArray(byte[] src, int srcStart, int newSize) 254 { 255 byte[] newArray = new byte[newSize]; 256 System.arraycopy(src, srcStart, newArray, 0, Math.min( 257 src.length - srcStart, newSize)); return newArray; 260 } 261 262 } | Popular Tags |