Added sqlite support and fixed some issues

This commit is contained in:
Ziver Koc 2014-07-01 19:54:30 +00:00
parent 78205d97af
commit fcbaef3e76
26 changed files with 348 additions and 371 deletions

View file

@ -29,24 +29,25 @@ import java.util.ArrayList;
public class DynamicByteArrayStream extends InputStream{
/** The byte array container */
private ArrayList<byte[]> bytes;
/** Current virtual size of the stream */
private int size;
/** Points the current byte array index */
private int arrayIndex;
/** Points to a local index in the current byte array */
private int arrayLocalIndex;
/** Current virtual position of the stream */
private int pos;
private int globalPos;
/** Current virtual size of the stream */
private int globalSize;
/** Points the current byte array index */
private int globalArrayIndex;
/** Points to a local index in the current byte array */
private int localArrayOffset;
/**
* Create a new instance of DynamicByteArrayStream
*/
public DynamicByteArrayStream(){
bytes = new ArrayList<byte[]>();
size = 0;
arrayIndex = 0;
arrayLocalIndex = 0;
pos = 0;
globalPos = 0;
globalSize = 0;
globalArrayIndex = 0;
localArrayOffset = 0;
}
/**
@ -56,7 +57,7 @@ public class DynamicByteArrayStream extends InputStream{
*/
public synchronized void append(byte[] b){
bytes.add(b);
size += b.length;
globalSize += b.length;
}
/**
@ -71,70 +72,70 @@ public class DynamicByteArrayStream extends InputStream{
byte[] new_b = new byte[length];
System.arraycopy(b, offset, new_b, 0, length);
bytes.add(new_b);
size += length;
globalSize += length;
}
@Override
public synchronized int read() throws IOException {
if(pos >= size) return -1;
if(globalPos >= globalSize) return -1;
int ret = bytes.get(arrayIndex)[arrayLocalIndex] & 0xff;
pos++;
arrayLocalIndex++;
if(arrayLocalIndex >= bytes.get(arrayIndex).length){
arrayIndex++;
arrayLocalIndex = 0;
int ret = bytes.get(globalArrayIndex)[localArrayOffset] & 0xff;
globalPos++;
localArrayOffset++;
if(localArrayOffset >= bytes.get(globalArrayIndex).length){
globalArrayIndex++;
localArrayOffset = 0;
}
return ret;
}
public synchronized int read(byte b[], int off, int len) {
if(len <= 0) return 0;
if(pos >= size) return -1;
if(globalPos >= globalSize) return -1;
int bytes_read=0;
if(pos+len >= size) len = size - pos;
for(; bytes_read<len ;bytes_read++){
byte[] src = bytes.get(arrayIndex);
if(globalPos+len >= globalSize) len = globalSize - globalPos;
while(bytes_read<len){
byte[] src = bytes.get(globalArrayIndex);
// Read length is LONGER than local array
if(arrayLocalIndex +len-bytes_read >= src.length){
int length = src.length- arrayLocalIndex;
System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
if(localArrayOffset +len-bytes_read > src.length){
int length = src.length- localArrayOffset;
System.arraycopy(src, localArrayOffset, b, off+bytes_read, length);
arrayLocalIndex = 0;
arrayIndex++;
localArrayOffset = 0;
globalArrayIndex++;
bytes_read += length;
}
// Read length is SHORTER than local array
else{
int length = len-bytes_read;
System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
System.arraycopy(src, localArrayOffset, b, off+bytes_read, length);
arrayLocalIndex += length;
localArrayOffset += length;
bytes_read += length;
}
}
pos += len;
globalPos += len;
return bytes_read;
}
public synchronized int available() {
return size - pos;
return globalSize - globalPos;
}
/**
* Clears this stream from the byte arrays
*/
public synchronized void clear(){
size = 0;
globalSize = 0;
reset();
bytes.clear();
}
public synchronized void reset() {
arrayIndex = 0;
arrayLocalIndex = 0;
pos = 0;
globalArrayIndex = 0;
localArrayOffset = 0;
globalPos = 0;
}
public void close() throws IOException {
@ -145,8 +146,8 @@ public class DynamicByteArrayStream extends InputStream{
* @return all of the buffers content as a byte array.
*/
public byte[] getBytes(){
byte[] data = new byte[size];
this.read(data, 0, size);
byte[] data = new byte[globalSize];
this.read(data, 0, globalSize);
return data;
}