Moved some files and fixed some performance issues
This commit is contained in:
parent
bd7f3542f8
commit
bd58efdd37
6 changed files with 38 additions and 114 deletions
|
|
@ -1,91 +0,0 @@
|
|||
package zutil.io;
|
||||
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class BoundryBufferedInputStream extends FilterInputStream{
|
||||
|
||||
protected BoundryBufferedInputStream(InputStream in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
|
||||
public int read() throws IOException {
|
||||
return in.read();
|
||||
}
|
||||
|
||||
public int read(byte b[], int off, int len) throws IOException {
|
||||
return in.read(b, off, len);
|
||||
}
|
||||
|
||||
public long skip(long n) throws IOException {
|
||||
return in.skip(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an estimate of the number of bytes that can be read (or
|
||||
* skipped over) from this input stream without blocking by the next
|
||||
* caller of a method for this input stream. The next caller might be
|
||||
* the same thread or another thread. A single read or skip of this
|
||||
* many bytes will not block, but may read or skip fewer bytes.
|
||||
* <p>
|
||||
* This method returns the result of {@link #in in}.available().
|
||||
*
|
||||
* @return an estimate of the number of bytes that can be read (or skipped
|
||||
* over) from this input stream without blocking.
|
||||
* @exception IOException if an I/O error occurs.
|
||||
*/
|
||||
public int available() throws IOException {
|
||||
return in.available();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Marks the current position in this input stream. A subsequent
|
||||
* call to the <code>reset</code> method repositions this stream at
|
||||
* the last marked position so that subsequent reads re-read the same bytes.
|
||||
* <p>
|
||||
* The <code>readlimit</code> argument tells this input stream to
|
||||
* allow that many bytes to be read before the mark position gets
|
||||
* invalidated.
|
||||
* <p>
|
||||
* This method simply performs <code>in.mark(readlimit)</code>.
|
||||
*
|
||||
* @param readlimit the maximum limit of bytes that can be read before
|
||||
* the mark position becomes invalid.
|
||||
* @see java.io.FilterInputStream#in
|
||||
* @see java.io.FilterInputStream#reset()
|
||||
*/
|
||||
public synchronized void mark(int readlimit) {
|
||||
in.mark(readlimit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repositions this stream to the position at the time the
|
||||
* <code>mark</code> method was last called on this input stream.
|
||||
* <p>
|
||||
* This method
|
||||
* simply performs <code>in.reset()</code>.
|
||||
* <p>
|
||||
* Stream marks are intended to be used in
|
||||
* situations where you need to read ahead a little to see what's in
|
||||
* the stream. Often this is most easily done by invoking some
|
||||
* general parser. If the stream is of the type handled by the
|
||||
* parse, it just chugs along happily. If the stream is not of
|
||||
* that type, the parser should toss an exception when it fails.
|
||||
* If this happens within readlimit bytes, it allows the outer
|
||||
* code to reset the stream and try another parser.
|
||||
*
|
||||
* @exception IOException if the stream has not been marked or if the
|
||||
* mark has been invalidated.
|
||||
* @see java.io.FilterInputStream#in
|
||||
* @see java.io.FilterInputStream#mark(int)
|
||||
*/
|
||||
public synchronized void reset() throws IOException {
|
||||
in.reset();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -12,8 +12,8 @@ import java.io.RandomAccessFile;
|
|||
* @author Ziver
|
||||
*/
|
||||
public class BufferedRandomAccessFile extends RandomAccessFile{
|
||||
// The size of the buffer
|
||||
private int BUF_SIZE = 256;
|
||||
// The size of the buffer in Byte
|
||||
private int BUF_SIZE = 64*1024;
|
||||
|
||||
// The Buffer
|
||||
byte buffer[];
|
||||
|
|
@ -73,22 +73,6 @@ public class BufferedRandomAccessFile extends RandomAccessFile{
|
|||
buffer = new byte[BUF_SIZE];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the next byte in the buffer
|
||||
*/
|
||||
public final int read() throws IOException{
|
||||
if(buf_pos >= buf_end) {
|
||||
if(fillBuffer() < 0)
|
||||
return -1;
|
||||
}
|
||||
if(buf_end == 0) {
|
||||
return -1;
|
||||
} else {
|
||||
buf_pos++;
|
||||
return buffer[buf_pos-1];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads in data from the file to the buffer
|
||||
*
|
||||
|
|
@ -116,6 +100,22 @@ public class BufferedRandomAccessFile extends RandomAccessFile{
|
|||
file_pos = super.getFilePointer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the next byte in the buffer
|
||||
*/
|
||||
public final int read() throws IOException{
|
||||
if(buf_pos >= buf_end) {
|
||||
if(fillBuffer() < 0)
|
||||
return -1;
|
||||
}
|
||||
if(buf_end == 0) {
|
||||
return -1;
|
||||
} else {
|
||||
buf_pos++;
|
||||
return buffer[buf_pos-1];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the given array with data from the buffer
|
||||
*
|
||||
|
|
@ -135,13 +135,26 @@ public class BufferedRandomAccessFile extends RandomAccessFile{
|
|||
* @return the amount of bytes read or -1 if eof
|
||||
*/
|
||||
public int read(byte b[], int off, int len) throws IOException {
|
||||
if(buf_pos >= buf_end) {
|
||||
if(fillBuffer() < 0)
|
||||
return -1; // EOF
|
||||
}
|
||||
|
||||
// Copy from buffer
|
||||
int leftover = buf_end - buf_pos;
|
||||
if(len <= leftover) {
|
||||
System.arraycopy(buffer, buf_pos, b, off, len);
|
||||
buf_pos += len;
|
||||
return len;
|
||||
}
|
||||
for(int i = 0; i < len; i++) {
|
||||
|
||||
System.arraycopy(buffer, buf_pos, b, off, leftover);
|
||||
int n = super.read(b, off+leftover, len-leftover );
|
||||
fillBuffer();
|
||||
if( n >= 0 )
|
||||
return leftover + n;
|
||||
return leftover;
|
||||
/*for(int i = 0; i < len; i++) {
|
||||
int c = this.read();
|
||||
if(c != -1)
|
||||
b[off+i] = (byte)c;
|
||||
|
|
@ -149,7 +162,7 @@ public class BufferedRandomAccessFile extends RandomAccessFile{
|
|||
return i;
|
||||
}
|
||||
}
|
||||
return len;
|
||||
return len;*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
112
src/zutil/io/DynamicByteArrayStream.java
Normal file
112
src/zutil/io/DynamicByteArrayStream.java
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package zutil.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DynamicByteArrayStream extends InputStream{
|
||||
/** The byte array container */
|
||||
private ArrayList<byte[]> bytes;
|
||||
/** The current size of the stream */
|
||||
private int size;
|
||||
/** 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 pos;
|
||||
|
||||
/**
|
||||
* Create a new instance of DynamicByteArrayStream
|
||||
*/
|
||||
public DynamicByteArrayStream(){
|
||||
bytes = new ArrayList<byte[]>();
|
||||
size = 0;
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append an byte array to the stream
|
||||
* @param b The byte array to add
|
||||
*/
|
||||
public synchronized void add(byte[] b){
|
||||
bytes.add(b);
|
||||
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;
|
||||
reset();
|
||||
bytes.clear();
|
||||
}
|
||||
|
||||
public synchronized void reset() {
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
//bytes = null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue