Renamed Boundary buffer and improved InputStreamCloser

This commit is contained in:
Ziver Koc 2016-07-06 17:35:58 +02:00
parent f77a757b6d
commit 765063ae47
3 changed files with 255 additions and 257 deletions

View file

@ -1,241 +1,241 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2015 Ziver Koc * Copyright (c) 2015 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.io; package zutil.io;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /**
* TODO: boundry * TODO: boundry
* *
* @author Ziver * @author Ziver
* *
*/ */
public class BoundaryBufferedInputStream extends FilterInputStream{ public class BufferedBoundaryInputStream extends FilterInputStream{
/** The size of the buffer in Byte */ /** The size of the buffer in bytes */
public static final int DEFAULT_BUF_SIZE = 64*1024; public static final int DEFAULT_BUF_SIZE = 64*1024;
/** The raw buffer */ /** The raw buffer */
protected byte buffer[]; protected byte buffer[];
/** The end position of the buffer */ /** The end position of the buffer */
protected int buf_end = 0; protected int buf_end = 0;
/** The current position in the buffer */ /** The current position in the buffer */
protected int buf_pos = 0; protected int buf_pos = 0;
/** The boundary (the delimiter) */ /** The boundary (the delimiter) */
protected byte[] boundary; protected byte[] boundary;
/** Boundary position, 0< means no boundary found */ /** Boundary position, 0< means no boundary found */
protected int bound_pos; protected int bound_pos;
/** /**
* Creates a instance of this class with a default buffer size of 64K * Creates a instance of this class with a default buffer size of 64K
* *
* @param in is the InputStream that the buffer will use * @param in is the InputStream that the buffer will use
*/ */
public BoundaryBufferedInputStream(InputStream in){ public BufferedBoundaryInputStream(InputStream in){
this(in, DEFAULT_BUF_SIZE); this(in, DEFAULT_BUF_SIZE);
} }
/** /**
* Creates a instance of this class * Creates a instance of this class
* *
* @param in is the InputStream that the buffer will use * @param in is the InputStream that the buffer will use
* @param buf_size speifies the buffer size * @param buf_size speifies the buffer size
*/ */
public BoundaryBufferedInputStream(InputStream in, int buf_size){ public BufferedBoundaryInputStream(InputStream in, int buf_size){
super(in); super(in);
buf_end = 0; buf_end = 0;
buf_pos = 0; buf_pos = 0;
buffer = new byte[buf_size]; buffer = new byte[buf_size];
bound_pos = -1; bound_pos = -1;
} }
/** /**
* Moves the remaining data to the beginning of the * Moves the remaining data to the beginning of the
* buffer and then fills the buffer with data from * buffer and then fills the buffer with data from
* the source stream to the buffer * the source stream to the buffer
* *
* @return the size of the buffer * @return the size of the buffer
*/ */
protected int fillBuffer() throws IOException { protected int fillBuffer() throws IOException {
int leftover = buf_end - buf_pos; int leftover = buf_end - buf_pos;
// is there any data available // is there any data available
if(leftover < 0 && super.available() <= 0) if(leftover < 0 && super.available() <= 0)
return -1; // EOF return -1; // EOF
// Move the end of the buffer to the start to not miss any split boundary // Move the end of the buffer to the start to not miss any split boundary
System.arraycopy(buffer, buf_pos, buffer, 0, buf_end); System.arraycopy(buffer, buf_pos, buffer, 0, buf_end);
// Copy in new data from the stream // Copy in new data from the stream
int n = super.read(buffer, leftover, buffer.length ); int n = super.read(buffer, leftover, buffer.length );
// Reset positions // Reset positions
if(n+leftover >= 0) { if(n+leftover >= 0) {
buf_end = leftover + n; buf_end = leftover + n;
buf_pos = 0; buf_pos = 0;
} }
searchNextBoundary(); searchNextBoundary();
return n+leftover; return n+leftover;
} }
/** /**
* @return the next byte in the buffer * @return the next byte in the buffer
*/ */
public final int read() throws IOException{ public final int read() throws IOException{
if(buf_pos >= buf_end-boundary.length) { if(buf_pos >= buf_end-boundary.length) {
if(fillBuffer() <= 0) if(fillBuffer() <= 0)
return -1; // EOF return -1; // EOF
} }
if(bound_pos == buf_pos) if(bound_pos == buf_pos)
return -1; // boundary return -1; // boundary
return buffer[buf_pos++]; return buffer[buf_pos++];
} }
/** /**
* Fills the given array with data from the buffer * Fills the given array with data from the buffer
* *
* @param b is the array that will be filled * @param b is the array that will be filled
* @return the amount of bytes read or -1 if EOF * @return the amount of bytes read or -1 if EOF
*/ */
public int read(byte b[]) throws IOException { public int read(byte b[]) throws IOException {
return read(b, 0, b.length); return read(b, 0, b.length);
} }
/** /**
* Reads a given length of bytes from the buffer * Reads a given length of bytes from the buffer
* *
* @param b is the array that will be filled * @param b is the array that will be filled
* @param off is the offset in the array * @param off is the offset in the array
* @param len is the amount to read * @param len is the amount to read
* @return the amount of bytes read or -1 if EOF * @return the amount of bytes read or -1 if EOF
*/ */
public int read(byte b[], int off, int len) throws IOException { public int read(byte b[], int off, int len) throws IOException {
if(bound_pos == buf_pos) if(bound_pos == buf_pos)
return -1; // boundary return -1; // boundary
if(buf_pos >= buf_end-boundary.length) { if(buf_pos >= buf_end-boundary.length) {
if(fillBuffer() <= 0) if(fillBuffer() <= 0)
return -1; // EOF return -1; // EOF
} }
int leftover = buf_end - buf_pos; int leftover = buf_end - buf_pos;
// The request is larger then the buffer size // The request is larger then the buffer size
if(len > leftover){ if(len > leftover){
len = leftover; len = leftover;
} }
// the boundary is in the read range // the boundary is in the read range
if(buf_pos < bound_pos && bound_pos < buf_pos+len){ if(buf_pos < bound_pos && bound_pos < buf_pos+len){
len = buf_pos - bound_pos; len = buf_pos - bound_pos;
} }
System.arraycopy(buffer, buf_pos, b, off, len); System.arraycopy(buffer, buf_pos, b, off, len);
buf_pos += len; buf_pos += len;
return len; return len;
} }
/** /**
* Skips over the boundary at the current position in the buffer * Skips over the boundary at the current position in the buffer
*/ */
public void next(){ public void next(){
if(bound_pos == buf_pos){ if(bound_pos == buf_pos){
buf_pos += boundary.length; buf_pos += boundary.length;
searchNextBoundary(); searchNextBoundary();
} }
} }
/** /**
* Searches for the nearest boundary from the current position * Searches for the nearest boundary from the current position
*/ */
protected void searchNextBoundary(){ protected void searchNextBoundary(){
for(int i=buf_pos; i<buf_end; i++){ for(int i=buf_pos; i<buf_end; i++){
for(int b=0; b < boundary.length; b++){ for(int b=0; b < boundary.length; b++){
if(buffer[i] != boundary[b]) if(buffer[i] != boundary[b])
break; break;
else if(b == boundary.length-1){ else if(b == boundary.length-1){
bound_pos = i; bound_pos = i;
return; return;
} }
} }
} }
bound_pos = -1; bound_pos = -1;
} }
/** /**
* Skips a specific amounts of bytes in the buffer. * Skips a specific amounts of bytes in the buffer.
* Note that his method does not check for boundaries. * Note that his method does not check for boundaries.
* *
* @param n the number of bytes to be skipped. * @param n the number of bytes to be skipped.
* @return the actual number of bytes skipped. * @return the actual number of bytes skipped.
*/ */
public long skip(long n) throws IOException { public long skip(long n) throws IOException {
int leftover = buf_end - buf_pos; int leftover = buf_end - buf_pos;
if(n > leftover){ if(n > leftover){
buf_pos = buf_end; buf_pos = buf_end;
return leftover; return leftover;
} }
return buf_pos += n; return buf_pos += n;
} }
/** /**
* Sets the boundary for the stream * Sets the boundary for the stream
*/ */
public void setBoundary(String b){ public void setBoundary(String b){
this.boundary = b.getBytes(); this.boundary = b.getBytes();
} }
/** /**
* Sets the boundary for the stream * Sets the boundary for the stream
*/ */
public void setBoundary(byte[] b){ public void setBoundary(byte[] b){
boundary = new byte[b.length]; boundary = new byte[b.length];
System.arraycopy(b, 0, boundary, 0, b.length); System.arraycopy(b, 0, boundary, 0, b.length);
} }
/** /**
* @return if current position in the buffer is a boundary * @return if current position in the buffer is a boundary
*/ */
public boolean isBoundary(){ public boolean isBoundary(){
return bound_pos == buf_pos; return bound_pos == buf_pos;
} }
/** /**
* @return an estimate of the number of bytes that can be read (or skipped * @return an estimate of the number of bytes that can be read (or skipped
* over) from this buffered input stream without blocking. * over) from this buffered input stream without blocking.
* @exception IOException if an I/O error occurs. * @exception IOException if an I/O error occurs.
*/ */
public int available() throws IOException { public int available() throws IOException {
return buf_end - buf_pos; return buf_end - buf_pos;
} }
/** /**
* Tests if this input stream supports the mark and * Tests if this input stream supports the mark and
* reset methods. * reset methods.
*/ */
public boolean markSupported(){ public boolean markSupported(){
return false; return false;
} }
} }

11
src/zutil/io/InputStreamCloser.java Normal file → Executable file
View file

@ -30,23 +30,24 @@ import java.io.InputStream;
/** /**
* A simple Class that mirrors a InputStream but * A simple Class that mirrors a InputStream but
* also has an additional Closeable object that * also has an additional Closeable objects that
* will be closed with the InputStream * will be closed when the this object is closed.
* *
* @author Ziver * @author Ziver
*/ */
public class InputStreamCloser extends InputStream{ public class InputStreamCloser extends InputStream{
private Closeable c; private Closeable[] c;
private InputStream in; private InputStream in;
public InputStreamCloser(InputStream in, Closeable c){ public InputStreamCloser(InputStream in, Closeable... c){
this.c = c; this.c = c;
this.in = in; this.in = in;
} }
public void close() throws IOException { public void close() throws IOException {
in.close(); in.close();
c.close(); for (Closeable stream : c)
stream.close();
} }
// Mirror functions // Mirror functions

View file

@ -32,13 +32,12 @@ import static org.junit.Assert.assertEquals;
@SuppressWarnings("resource") @SuppressWarnings("resource")
public class BoundaryBufferedInputStreamTest { public class BufferedBoundaryInputStreamTest {
@Test @Test
public void testReadB1() throws IOException { public void testReadB1() throws IOException {
StringInputStream inin = new StringInputStream(); StringInputStream inin = new StringInputStream("aaa#aaaaaaaaaaaaaaaa#aaaaaaaaaaaaaaa#");
BoundaryBufferedInputStream in = new BoundaryBufferedInputStream(inin); BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
inin.add("aaa#aaaaaaaaaaaaaaaa#aaaaaaaaaaaaaaa#");
in.setBoundary("#"); in.setBoundary("#");
@ -63,10 +62,9 @@ public class BoundaryBufferedInputStreamTest {
@Test @Test
public void testOnlyBoundaries() throws IOException { public void testOnlyBoundaries() throws IOException {
StringInputStream inin = new StringInputStream(); StringInputStream inin = new StringInputStream("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
BoundaryBufferedInputStream in = new BoundaryBufferedInputStream(inin); BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
inin.add("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
in.setBoundary("a"); in.setBoundary("a");
int n; int n;
@ -83,9 +81,8 @@ public class BoundaryBufferedInputStreamTest {
@Test @Test
public void testNoBounds() throws IOException { public void testNoBounds() throws IOException {
String data = "1234567891011121314151617181920"; String data = "1234567891011121314151617181920";
StringInputStream inin = new StringInputStream(); StringInputStream inin = new StringInputStream(data);
BoundaryBufferedInputStream in = new BoundaryBufferedInputStream(inin); BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
inin.add(data);
in.setBoundary("#"); in.setBoundary("#");
int out; int out;