2010-10-27 18:04:52 +00:00
|
|
|
package zutil.io;
|
|
|
|
|
|
|
|
|
|
import java.io.FilterInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-09 17:19:33 +00:00
|
|
|
* TODO: boundry
|
|
|
|
|
*
|
2010-10-27 18:04:52 +00:00
|
|
|
* @author Ziver
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class BoundaryBufferedInputStream extends FilterInputStream{
|
|
|
|
|
/** The size of the buffer in Byte */
|
|
|
|
|
public static final int DEFAULT_BUF_SIZE = 64*1024;
|
|
|
|
|
|
|
|
|
|
/** The Buffer */
|
|
|
|
|
protected byte buffer[];
|
|
|
|
|
/** The end of the buffer */
|
|
|
|
|
protected int buf_end = 0;
|
|
|
|
|
/** The position in the buffer */
|
|
|
|
|
protected int buf_pos = 0;
|
|
|
|
|
/** The boundary */
|
|
|
|
|
protected byte[] boundary;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a instance of this class with a default buffer size of 64K
|
|
|
|
|
*
|
2011-06-24 23:20:59 +00:00
|
|
|
* @param in is the InputStream that the buffer will use
|
2010-10-27 18:04:52 +00:00
|
|
|
*/
|
|
|
|
|
public BoundaryBufferedInputStream(InputStream in){
|
|
|
|
|
this(in, DEFAULT_BUF_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a instance of this class
|
|
|
|
|
*
|
|
|
|
|
* @param in is the InputStream that the buffer will use
|
|
|
|
|
* @param buf_size speifies the buffer size
|
|
|
|
|
*/
|
|
|
|
|
public BoundaryBufferedInputStream(InputStream in, int buf_size){
|
|
|
|
|
super(in);
|
2011-06-24 23:20:59 +00:00
|
|
|
buf_end = 0;
|
|
|
|
|
buf_pos = 0;
|
2010-10-27 18:04:52 +00:00
|
|
|
buffer = new byte[buf_size];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Moves the remaining data to the beginning of the
|
|
|
|
|
* buffer and then fills the buffer with data from
|
|
|
|
|
* the source stream to the buffer
|
|
|
|
|
*
|
2011-06-24 23:20:59 +00:00
|
|
|
* @return the size of the buffer
|
2010-10-27 18:04:52 +00:00
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
protected int fillBuffer() throws IOException {
|
|
|
|
|
int leftover = buf_end - buf_pos;
|
|
|
|
|
System.arraycopy(buffer, buf_pos, buffer, 0, buf_end);
|
|
|
|
|
int n = super.read(buffer, leftover, buffer.length );
|
|
|
|
|
if(n+leftover >= 0) {
|
|
|
|
|
buf_end = leftover + n;
|
|
|
|
|
buf_pos = 0;
|
|
|
|
|
}
|
|
|
|
|
return n+leftover;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-24 23:20:59 +00:00
|
|
|
* @return the next byte in the buffer
|
2010-10-27 18:04:52 +00:00
|
|
|
*/
|
|
|
|
|
public final int read() throws IOException{
|
2011-06-24 23:20:59 +00:00
|
|
|
if(buf_pos >= buf_end-boundary.length) {
|
2010-10-27 18:04:52 +00:00
|
|
|
if(fillBuffer() < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if(buf_end == 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
2011-06-24 23:20:59 +00:00
|
|
|
return buffer[buf_pos++];
|
2010-10-27 18:04:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fills the given array with data from the buffer
|
|
|
|
|
*
|
2011-06-24 23:20:59 +00:00
|
|
|
* @param b is the array that will be filled
|
|
|
|
|
* @return the amount of bytes read or -1 if EOF
|
|
|
|
|
*/
|
2010-10-27 18:04:52 +00:00
|
|
|
public int read(byte b[]) throws IOException {
|
|
|
|
|
return read(b, 0, b.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads a given length of bytes from the buffer
|
|
|
|
|
*
|
|
|
|
|
* @param b is the array that will be filled
|
|
|
|
|
* @param off is the offset in the array
|
|
|
|
|
* @param len is the amount to read
|
2011-06-24 23:20:59 +00:00
|
|
|
* @return the amount of bytes read or -1 if EOF
|
2010-10-27 18:04:52 +00:00
|
|
|
*/
|
|
|
|
|
public int read(byte b[], int off, int len) throws IOException {
|
2011-06-24 23:20:59 +00:00
|
|
|
if(buf_pos >= buf_end-boundary.length) {
|
2010-10-27 18:04:52 +00:00
|
|
|
if(fillBuffer() < 0)
|
|
|
|
|
return -1; // EOF
|
|
|
|
|
}
|
|
|
|
|
int leftover = buf_end - buf_pos;
|
|
|
|
|
// Copy from buffer
|
|
|
|
|
if(len <= leftover) {
|
|
|
|
|
System.arraycopy(buffer, buf_pos, b, off, len);
|
|
|
|
|
buf_pos += len;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-24 23:20:59 +00:00
|
|
|
/**
|
|
|
|
|
* TODO: Skips over the boundary
|
|
|
|
|
*/
|
|
|
|
|
public void next(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 18:04:52 +00:00
|
|
|
/**
|
|
|
|
|
* @param n the number of bytes to be skipped.
|
|
|
|
|
* @return the actual number of bytes skipped.
|
|
|
|
|
*/
|
|
|
|
|
public long skip(long n) throws IOException {
|
|
|
|
|
int leftover = buf_end - buf_pos;
|
|
|
|
|
if(n > leftover){
|
|
|
|
|
buf_pos = buf_end;
|
|
|
|
|
return leftover;
|
|
|
|
|
}
|
|
|
|
|
return buf_pos += n;
|
|
|
|
|
}
|
2011-06-24 23:20:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the boundary for the stream
|
|
|
|
|
*/
|
|
|
|
|
public void setBoundary(String b){
|
|
|
|
|
this.boundary = b.getBytes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the boundary for the stream
|
|
|
|
|
*/
|
|
|
|
|
public void setBoundary(byte[] b){
|
|
|
|
|
boundary = new byte[b.length];
|
|
|
|
|
System.arraycopy(b, 0, boundary, 0, b.length);
|
|
|
|
|
}
|
2010-10-27 18:04:52 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 buf_end - buf_pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-24 23:20:59 +00:00
|
|
|
* Tests if this input stream supports the mark and
|
|
|
|
|
* reset methods.
|
|
|
|
|
*/
|
|
|
|
|
public boolean markSupported(){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-10-27 18:04:52 +00:00
|
|
|
}
|