1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.io.InputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.OutputStream ; 27 import java.sql.SQLException ; 28 import java.sql.Blob ; 29 30 46 public final class ByteArrayBlob implements Blob 47 { 48 51 private final byte[] mBytes; 52 53 public ByteArrayBlob(byte[] bytes) 54 { 55 if (bytes == null) 56 { 57 bytes = new byte[0]; 58 } 59 60 mBytes = bytes; 61 } 62 63 public InputStream getBinaryStream() throws SQLException 64 { 65 return new ByteArrayInputStream (mBytes); 66 } 67 68 public byte[] getBytes(long pos, int length) throws SQLException 69 { 70 if (length < 0 || length > mBytes.length || pos > mBytes.length) 72 { 73 return new byte[0]; 74 } 75 76 if (pos <= 0) 77 { 78 pos = 1; } 80 81 byte[] buffer = new byte[length]; 82 83 System.arraycopy(mBytes, (int)pos - 1, buffer, 0, length); 84 return buffer; 85 } 86 87 public long length() throws SQLException 88 { 89 return mBytes.length; 90 } 91 92 public long position(Blob pattern , long start) throws SQLException 93 { 94 return position(pattern.getBytes(0, (int)pattern.length()), start); 95 } 96 97 public long position(byte pattern[], long start) throws SQLException 98 { 99 int max = mBytes.length - pattern.length; 101 102 if (start < 0) 103 { 104 start = 0; } else if (start >= mBytes.length) 106 { 107 return -1; } 109 110 if (pattern.length == 0) 111 { 112 return -1; } 114 115 byte first = pattern[0]; 116 int i = (int)start; 117 118 while (true) 119 { 120 while (i <= max && mBytes[i] != first) 122 { 123 i++; 124 } 125 126 if (i > max) 127 { 128 return -1; } 130 131 int j = i + 1; 133 int end = j + pattern.length - 1; 134 int k = 1; 135 boolean cont = true; 136 137 while (cont && j < end) 140 { 141 if (mBytes[j++] != pattern[k++]) 142 { 143 i++; 144 cont = false; 145 } 146 } 148 if (cont) 149 { 150 return i; 151 } 152 } 153 } 154 155 public OutputStream setBinaryStream(long pos) 156 throws SQLException 157 { 158 throw new UnsupportedOperationException ("ByteArrayBlob is immutable"); 159 } 160 public int setBytes(long pos, byte[] bytes) 161 throws SQLException 162 { 163 throw new UnsupportedOperationException ("ByteArrayBlob is immutable"); 164 } 165 public int setBytes(long pos, byte[] bytes, int offset, int length) 166 throws SQLException 167 { 168 throw new UnsupportedOperationException ("ByteArrayBlob is immutable"); 169 } 170 public void truncate(long length) 171 throws SQLException 172 { 173 throw new UnsupportedOperationException ("ByteArrayBlob is immutable"); 174 } 175 } 176 177 | Popular Tags |