com.hfg.util.io.InputStreamSegment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util.io;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
//------------------------------------------------------------------------------
/**
An InputStream variant for reading up to a specified marker.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class InputStreamSegment extends FilterInputStream
{
//##########################################################################
// PRIVATE FIELDS
//##########################################################################
private byte[] mSegmentEndMarker;
private byte[] mBuffer;
private int mBufferPtr;
private int mBufferLimit;
private int mBufferReadLimit;
private boolean mEndOfSegmentReached = false;
private static int sDefaultBufferSize = 4 * 1024;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public InputStreamSegment(InputStream inStream, byte[] inSegmentEndMarker)
{
super(inStream);
if (0 == inSegmentEndMarker.length)
{
throw new RuntimeException("No segment end marker was specifed!");
}
mSegmentEndMarker = inSegmentEndMarker;
mBuffer = new byte[(inSegmentEndMarker.length * 2 > sDefaultBufferSize ? inSegmentEndMarker.length * 2 : sDefaultBufferSize)];
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public InputStreamSegment setBufferSize(int inValue)
{
if (inValue < mSegmentEndMarker.length)
{
throw new RuntimeException("The buffer size cannot be smaller than the size of the segment end marker!");
}
mBuffer = new byte[inValue];
return this;
}
//--------------------------------------------------------------------------
public int read()
throws IOException
{
if (mBufferPtr >= mBufferReadLimit)
{
if (!mEndOfSegmentReached)
{
fillBuffer();
}
else
{
return -1;
}
}
return (mBufferPtr < mBufferReadLimit ? (int) mBuffer[mBufferPtr++] & 0xff : -1);
}
//--------------------------------------------------------------------------
public int read(byte[] buffer, int offset, int length)
throws IOException
{
int index = offset;
int theChar = 0;
while (index - offset < length
&& (theChar = read()) != -1)
{
buffer[index++] = (byte) theChar;
}
int bytesRead = index - offset;
if (0 == bytesRead
&& -1 == theChar)
{
bytesRead = -1;
}
return bytesRead;
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private void fillBuffer()
throws IOException
{
int markerIndex = mBufferLimit - mBufferReadLimit;
// Move leftover content to the beginning of the buffer
if (mBufferPtr > 0)
{
System.arraycopy(mBuffer, mBufferReadLimit, mBuffer, 0, mBufferLimit - mBufferReadLimit);
mBufferPtr = 0;
mBufferLimit = mBufferLimit - mBufferReadLimit;
mBufferReadLimit = 0;
}
while (mBufferLimit < mBuffer.length)
{
int nextChar = in.read();
if (-1 == nextChar)
{
throw new IOException("End of stream reached without detecting the segment end marker '" + new String(mSegmentEndMarker) + "'!");
}
mBuffer[mBufferLimit++] = (byte) nextChar;
if (nextChar == mSegmentEndMarker[markerIndex])
{
if (markerIndex == mSegmentEndMarker.length - 1)
{
// Found the segment end marker.
mEndOfSegmentReached = true;
break;
}
markerIndex++;
}
else
{
if (nextChar == mSegmentEndMarker[0])
{
mBufferReadLimit += markerIndex;
markerIndex = 1;
}
else
{
mBufferReadLimit += (markerIndex + 1);
markerIndex = 0;
}
}
}
}
}