Fixed some things
This commit is contained in:
parent
c21b229882
commit
41c474d2a5
8 changed files with 239 additions and 138 deletions
|
|
@ -9,12 +9,12 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
private ArrayList<byte[]> bytes;
|
||||
/** The current size of the stream */
|
||||
private int size;
|
||||
/** points to the current index in the Arraylist */
|
||||
private int globalPointer;
|
||||
/** points localy in the current index in the ArrayList */
|
||||
/** points to the current index in the ArrayList */
|
||||
private int byteArrayIndex;
|
||||
/** points locally in the current index in the ArrayList */
|
||||
private int localPointer;
|
||||
/** The current position */
|
||||
private int currentPos;
|
||||
private int pos;
|
||||
|
||||
/**
|
||||
* Create a new instance of DynamicByteArrayStream
|
||||
|
|
@ -22,9 +22,9 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
public DynamicByteArrayStream(){
|
||||
bytes = new ArrayList<byte[]>();
|
||||
size = 0;
|
||||
globalPointer = 0;
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
currentPos = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -36,71 +36,77 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
size += b.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read() throws IOException {
|
||||
if(pos >= size) return -1;
|
||||
|
||||
int ret = bytes.get(byteArrayIndex)[localPointer] & 0xff;
|
||||
pos++;
|
||||
localPointer++;
|
||||
if(localPointer >= bytes.get(byteArrayIndex).length){
|
||||
byteArrayIndex++;
|
||||
localPointer = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public synchronized int read(byte b[], int off, int len) {
|
||||
//System.out.println("*****************************************************");
|
||||
//System.out.println("off: "+off+" len: "+len);
|
||||
//System.out.println("size: "+size+" arraylen: "+bytes.size());
|
||||
//System.out.println("pos: "+pos+" localPointer: "+localPointer);
|
||||
if(len <= 0) return 0;
|
||||
if(pos >= size) return -1;
|
||||
|
||||
int bytes_read = 0;
|
||||
if(pos+len >= size) len = size - pos;
|
||||
for(int i=0; i<len ;i++){
|
||||
byte[] src = bytes.get(byteArrayIndex);
|
||||
if(localPointer+len-i >= src.length){
|
||||
//System.out.println("1");
|
||||
int length = src.length-localPointer;
|
||||
System.arraycopy(src, localPointer, b, off+i, length);
|
||||
|
||||
localPointer = 0;
|
||||
byteArrayIndex++;
|
||||
bytes_read += length;
|
||||
i += length;
|
||||
}
|
||||
else{
|
||||
//System.out.println("2");
|
||||
int length = len-i;
|
||||
System.arraycopy(src, localPointer, b, off+i, length);
|
||||
|
||||
localPointer += length;
|
||||
bytes_read += length;
|
||||
i += length;
|
||||
}
|
||||
}
|
||||
pos += len;
|
||||
//System.out.println("new_pos: "+pos+" read: "+bytes_read);
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
public synchronized int available() {
|
||||
return size - pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears this stream from the byte arrays
|
||||
*/
|
||||
public synchronized void clear(){
|
||||
size = 0;
|
||||
globalPointer = 0;
|
||||
localPointer = 0;
|
||||
currentPos = 0;
|
||||
|
||||
reset();
|
||||
bytes.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read() throws IOException {
|
||||
if(currentPos >= size){
|
||||
return -1;
|
||||
}
|
||||
int ret = bytes.get(globalPointer)[localPointer] & 0xff;
|
||||
currentPos++;
|
||||
localPointer++;
|
||||
if(localPointer >= bytes.get(globalPointer).length){
|
||||
globalPointer++;
|
||||
localPointer = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/*
|
||||
public synchronized int read(byte b[], int off, int len) {
|
||||
System.out.println("read off:"+off+" len: "+len);
|
||||
if(currentPos+off >= size){
|
||||
return -1;
|
||||
}
|
||||
off += localPointer;
|
||||
while(off>0){
|
||||
if(bytes.get(globalPointer).length < off){
|
||||
globalPointer++;
|
||||
off -= bytes.get(globalPointer).length;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
int length;
|
||||
int oldLen = len;
|
||||
while(len > 0){
|
||||
length = bytes.get(globalPointer).length;
|
||||
System.arraycopy(b, 0, bytes.get(globalPointer), 0, (length<len ? length : len));
|
||||
len -= length;
|
||||
if(len > 0) globalPointer++;
|
||||
if(bytes.size() <= globalPointer) break;
|
||||
}
|
||||
localPointer = 0;
|
||||
currentPos += ( len<0 ? oldLen : oldLen-len);
|
||||
return ( len<0 ? oldLen : oldLen-len);
|
||||
}*/
|
||||
|
||||
public synchronized int available() {
|
||||
return size - currentPos;
|
||||
}
|
||||
|
||||
public synchronized void reset() {
|
||||
globalPointer = 0;
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
currentPos = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
//bytes = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue