1 19 20 package org.apache.cayenne.util; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.sql.Blob ; 26 import java.sql.SQLException ; 27 28 import org.apache.cayenne.CayenneRuntimeException; 29 30 40 public class MemoryBlob implements Blob { 41 42 volatile byte[] data; 43 44 public MemoryBlob() { 45 this(new byte[0]); 46 } 47 48 54 public MemoryBlob(byte[] data) { 55 56 if (data == null) { 57 throw new CayenneRuntimeException("Null data"); 58 } 59 60 this.data = data; 61 } 62 63 71 public long length() throws SQLException { 72 return data.length; 73 } 74 75 94 public byte[] getBytes(long pos, final int length) throws SQLException { 95 96 final byte[] ldata = data; 97 final int dlen = ldata.length; 98 99 pos--; 100 101 if (pos < 0 || pos > dlen) { 102 throw new SQLException ("Invalid pos: " + (pos + 1)); 103 } 104 105 if (length < 0 || length > dlen - pos) { 106 throw new SQLException ("length: " + length); 107 } 108 109 final byte[] out = new byte[length]; 110 System.arraycopy(ldata, (int) pos, out, 0, length); 111 return out; 112 } 113 114 122 public InputStream getBinaryStream() throws SQLException { 123 return new ByteArrayInputStream (data); 124 } 125 126 138 public long position(final byte[] pattern, long start) throws SQLException { 139 140 final byte[] ldata = data; 141 final int dlen = ldata.length; 142 143 if (start > dlen || pattern == null) { 144 return -1; 145 } 146 else if (start < 1) { 147 start = 0; 148 } 149 else { 150 start--; 151 } 152 153 final int plen = pattern.length; 154 155 if (plen == 0 || start > dlen - plen) { 156 return -1; 157 } 158 159 final int stop = dlen - plen; 160 final byte b0 = pattern[0]; 161 162 outer_loop: for (int i = (int) start; i <= stop; i++) { 163 if (ldata[i] != b0) { 164 continue; 165 } 166 167 int len = plen; 168 int doffset = i; 169 int poffset = 0; 170 171 while (len-- > 0) { 172 if (ldata[doffset++] != pattern[poffset++]) { 173 continue outer_loop; 174 } 175 } 176 177 return i + 1; 178 } 179 180 return -1; 181 } 182 183 196 public long position(final Blob pattern, long start) throws SQLException { 197 198 final byte[] ldata = data; 199 final int dlen = ldata.length; 200 201 if (start > dlen || pattern == null) { 202 return -1; 203 } 204 else if (start < 1) { 205 start = 0; 206 } 207 else { 208 start--; 209 } 210 211 final long plen = pattern.length(); 212 213 if (plen == 0 || start > dlen - plen) { 214 return -1; 215 } 216 217 final int iplen = (int) plen; 219 byte[] bap; 220 221 if (pattern instanceof MemoryBlob) { 222 bap = ((MemoryBlob) pattern).data; 223 } 224 else { 225 bap = pattern.getBytes(1, iplen); 226 } 227 228 final int stop = dlen - iplen; 229 final byte b0 = bap[0]; 230 231 outer_loop: for (int i = (int) start; i <= stop; i++) { 232 if (ldata[i] != b0) { 233 continue; 234 } 235 236 int len = iplen; 237 int doffset = i; 238 int poffset = 0; 239 240 while (len-- > 0) { 241 if (ldata[doffset++] != bap[poffset++]) { 242 continue outer_loop; 243 } 244 } 245 246 return i + 1; 247 } 248 249 return -1; 250 } 251 252 255 public int setBytes(long pos, byte[] bytes) throws SQLException { 256 throw new SQLException ("Not supported"); 257 } 258 259 262 public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { 263 throw new SQLException ("Not supported"); 264 } 265 266 269 public OutputStream setBinaryStream(long pos) throws SQLException { 270 throw new SQLException ("Not supported"); 271 } 272 273 282 public void truncate(final long len) throws SQLException { 283 284 final byte[] ldata = data; 285 286 if (len < 0 || len > ldata.length) { 287 throw new SQLException ("Invalid length: " + Long.toString(len)); 288 } 289 290 if (len == ldata.length) { 291 return; 292 } 293 294 byte[] newData = new byte[(int) len]; 295 System.arraycopy(ldata, 0, newData, 0, (int) len); 296 data = newData; 297 } 298 } 299 | Popular Tags |