1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
package SQLite;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Internal class implementing java.io.InputStream on
* SQLite 3.4.0 incremental blob I/O interface.
*/
class BlobR extends InputStream {
/**
* Blob instance
*/
private Blob blob;
/**
* Read position, file pointer.
*/
private int pos;
/**
* Contruct InputStream from blob instance.
*/
BlobR(Blob blob) {
this.blob = blob;
this.pos = 0;
}
/**
* Return number of available bytes for reading.
* @return available input bytes
*/
public int available() throws IOException {
int ret = blob.size - pos;
return (ret < 0) ? 0 : ret;
}
/**
* Mark method; dummy to satisfy InputStream class.
*/
public void mark(int limit) {
}
/**
* Reset method; dummy to satisfy InputStream class.
*/
public void reset() throws IOException {
}
/**
* Mark support; not for this class.
* @return always false
*/
public boolean markSupported() {
return false;
}
/**
* Close this blob InputStream.
*/
public void close() throws IOException {
blob.close();
blob = null;
pos = 0;
}
/**
* Skip over blob data.
*/
public long skip(long n) throws IOException {
long ret = pos + n;
if (ret < 0) {
ret = 0;
pos = 0;
} else if (ret > blob.size) {
ret = blob.size;
pos = blob.size;
} else {
pos = (int) ret;
}
return ret;
}
/**
* Read single byte from blob.
* @return byte read
*/
public int read() throws IOException {
byte b[] = new byte[1];
int n = blob.read(b, 0, pos, b.length);
if (n > 0) {
pos += n;
return b[0];
}
return -1;
}
/**
* Read byte array from blob.
* @param b byte array to be filled
* @return number of bytes read
*/
public int read(byte b[]) throws IOException {
int n = blob.read(b, 0, pos, b.length);
if (n > 0) {
pos += n;
return n;
}
return -1;
}
/**
* Read slice of byte array from blob.
* @param b byte array to be filled
* @param off offset into byte array
* @param len length to be read
* @return number of bytes read
*/
public int read(byte b[], int off, int len) throws IOException {
if (off + len > b.length) {
len = b.length - off;
}
if (len < 0) {
return -1;
}
if (len == 0) {
return 0;
}
int n = blob.read(b, off, pos, len);
if (n > 0) {
pos += n;
return n;
}
return -1;
}
}
/**
* Internal class implementing java.io.OutputStream on
* SQLite 3.4.0 incremental blob I/O interface.
*/
class BlobW extends OutputStream {
/**
* Blob instance
*/
private Blob blob;
/**
* Read position, file pointer.
*/
private int pos;
/**
* Contruct OutputStream from blob instance.
*/
BlobW(Blob blob) {
this.blob = blob;
this.pos = 0;
}
/**
* Flush blob; dummy to satisfy OutputStream class.
*/
public void flush() throws IOException {
}
/**
* Close this blob OutputStream.
*/
public void close() throws IOException {
blob.close();
blob = null;
pos = 0;
}
/**
* Write blob data.
* @param v byte to be written at current position.
*/
public void write(int v) throws IOException {
byte b[] = new byte[1];
b[0] = (byte) v;
pos += blob.write(b, 0, pos, 1);
}
/**
* Write blob data.
* @param b byte array to be written at current position.
*/
public void write(byte[] b) throws IOException {
if (b != null && b.length > 0) {
pos += blob.write(b, 0, pos, b.length);
}
}
/**
* Write blob data.
* @param b byte array to be written.
* @param off offset within byte array
* @param len length of data to be written
*/
public void write(byte[] b, int off, int len) throws IOException {
if (b != null) {
if (off + len > b.length) {
len = b.length - off;
}
if (len <= 0) {
return;
}
pos += blob.write(b, off, pos, len);
}
}
}
/**
* Class to represent SQLite3 3.4.0 incremental blob I/O interface.
*
* Note, that all native methods of this class are
* not synchronized, i.e. it is up to the caller
* to ensure that only one thread is in these
* methods at any one time.
*/
public class Blob {
/**
* Internal handle for the SQLite3 blob.
*/
private long handle = 0;
/**
* Cached size of blob, setup right after blob
* has been opened.
*/
protected int size = 0;
/**
* Return InputStream for this blob
* @return InputStream
*/
public InputStream getInputStream() {
return new BlobR(this);
}
/**
* Return OutputStream for this blob
* @return OutputStream
*/
public OutputStream getOutputStream() {
return new BlobW(this);
}
/**
* Close blob.
*/
public native void close();
/**
* Internal blob write method.
* @param b byte array to be written
* @param off offset into byte array
* @param pos offset into blob
* @param len length to be written
* @return number of bytes written to blob
*/
native int write(byte[] b, int off, int pos, int len) throws IOException;
/**
* Internal blob read method.
* @param b byte array to be written
* @param off offset into byte array
* @param pos offset into blob
* @param len length to be written
* @return number of bytes written to blob
*/
native int read(byte[] b, int off, int pos, int len) throws IOException;
/**
* Destructor for object.
*/
protected native void finalize();
/**
* Internal native initializer.
*/
private static native void internal_init();
static {
internal_init();
}
}
|