com.hfg.util.io.ByteSource 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.IOException;
import java.io.RandomAccessFile;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import com.hfg.exception.ProgrammingException;
//------------------------------------------------------------------------------
/**
* Abstraction to normalize RandomAccessFiles and byte[].
*
* @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 ByteSource
{
private ByteBuffer mByteBuffer;
private RandomAccessFile mRandomAccessFile;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public ByteSource(byte[] inBytes)
{
mByteBuffer = ByteBuffer.wrap(inBytes);
}
//---------------------------------------------------------------------------
public ByteSource(ByteBuffer inByteBuffer)
{
mByteBuffer = inByteBuffer;
}
//---------------------------------------------------------------------------
public ByteSource(RandomAccessFile inRandomAccessFile)
{
mRandomAccessFile = inRandomAccessFile;
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public void seek(long inPosition)
throws IOException
{
if (mByteBuffer != null)
{
mByteBuffer.position((int) inPosition);
}
else if (mRandomAccessFile != null)
{
mRandomAccessFile.seek(inPosition);
}
else
{
throw new ProgrammingException("Unsupported byte source!");
}
}
//---------------------------------------------------------------------------
public int read() throws IOException
{
int theByte;
if (mByteBuffer != null)
{
theByte = mByteBuffer.get();
}
else if (mRandomAccessFile != null)
{
theByte = mRandomAccessFile.read();
}
else
{
throw new ProgrammingException("Unsupported byte source!");
}
return theByte;
}
//---------------------------------------------------------------------------
public int read(byte inByteDest[], int inOffset, int inLength)
throws IOException
{
int numBytesRead;
if (mByteBuffer != null)
{
int remaining = mByteBuffer.remaining();
try
{
mByteBuffer.get(inByteDest, inOffset, inLength);
numBytesRead = inLength;
}
catch (BufferUnderflowException e)
{
numBytesRead = remaining;
}
}
else if (mRandomAccessFile != null)
{
mRandomAccessFile.readFully(inByteDest, inOffset, inLength);
numBytesRead = inLength;
}
else
{
throw new ProgrammingException("Unsupported byte source!");
}
return numBytesRead;
}
//---------------------------------------------------------------------------
public int read(byte inByteDest[])
throws IOException
{
return read(inByteDest, 0, inByteDest.length);
}
//---------------------------------------------------------------------------
/**
* Reads a signed 32-bit integer from this souce. This method reads 4
* bytes from the source, starting at the current pointer.
*/
public final int readInt()
throws IOException
{
int theInt;
if (mByteBuffer != null)
{
theInt = mByteBuffer.getInt();
}
else if (mRandomAccessFile != null)
{
theInt = mRandomAccessFile.readInt();
}
else
{
throw new ProgrammingException("Unsupported byte source!");
}
return theInt;
}
}