This class allows access to an array of bytes so that you can read and write primitives and sub-arrays of bytes from it. I find it useful and while written before NIO I feel there are some advantaged over mapped buffers.
I am not sure this is an answer to a frequently asked question but if you have ever needed something like this it does come in awfully handy.
import java.io.IOException;
public final class Bufferedpage{
private boolean needsFlush;
private byte[] buff;
private int pos;
public Bufferedpage(){
buff = new byte[0];
needsFlush = false;
pos = 0;
}
public Bufferedpage(int initialsize){
buff = new byte[initialsize];
for(int i=0;i<buff.length;i++){
buff[i] = 0x00;
}
needsFlush = false;
pos = 0;
}
public void loadBuffer(byte[] toLoad){
buff = toLoad;
needsFlush = false;
pos = 0;
}
public byte[] getBufferContents(){
needsFlush = false;
return buff;
}
public boolean bufferNeedsFlush(){
return needsFlush;
}
public void seek(int position){
pos = position;
if(pos<0){
pos = 0;
}
}
public int getPosition(){
return pos;
}
public int getSize(){
return buff.length;
}
public int readInt()throws IOException{
int ch1 = readByte();
int ch2 = readByte();
int ch3 = readByte();
int ch4 = readByte();
return ((ch1 << 24) & 0xFF000000) + ((ch2 << 16) & 0x00FF0000) + ((ch3 << 8) & 0x0000FF00) + ((ch4 << 0) & 0x000000FF);
}
public byte readByte()throws IOException{
if(pos>=buff.length){
throw new IOException("Buffer Overflow Exception");
}
byte tr = buff[pos];
pos++;
return tr;
}
public long readLong()throws IOException{
return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
}
public float readFloat()throws IOException{
return Float.intBitsToFloat(readInt());
}
public double readDouble()throws IOException{
return Double.longBitsToDouble(readLong());
}
public short readShort()throws IOException{
byte ch1 = readByte();
byte ch2 = readByte();
return (short)(((ch1 << 8 )& 0x0000FF00) + ((ch2 << 0)& 0x000000FF));
}
public boolean readBoolean()throws IOException{
return (readByte()==0x01);
}
public void writeByte(byte b)throws IOException{
write(b);
}
public void writeShort(short s)throws IOException{
write((byte)((s >>> 8) & 0xFF));
write((byte)((s >>> 0) & 0xFF));
}
public void writeInt(int v)throws IOException{
write((byte)((v >>> 24) & 0xFF));
write((byte)((v >>> 16) & 0xFF));
write((byte)((v >>> 8) & 0xFF));
write((byte)((v >>> 0) & 0xFF));
}
public void writeLong(long v)throws IOException{
write((byte)((v >>> 56) & 0xFF));
write((byte)((v >>> 48) & 0xFF));
write((byte)((v >>> 40) & 0xFF));
write((byte)((v >>> 32) & 0xFF));
write((byte)((v >>> 24) & 0xFF));
write((byte)((v >>> 16) & 0xFF));
write((byte)((v >>> 8) & 0xFF));
write((byte)((v >>> 0) & 0xFF));
}
public void writeFloat(float v)throws IOException{
writeInt(Float.floatToIntBits(v));
}
public void writeDouble(double v)throws IOException{
writeLong(Double.doubleToLongBits(v));
}
public void writeBoolean(boolean b)throws IOException{
if(b){
writeByte((byte)0x01);
}else{
writeByte((byte)0x00);
}
}
public byte[] readBytes(int size)throws IOException{
if((buff.length-pos)<size){
throw new IOException("Buffer Overflow Exception");
}
byte[] temp = new byte[size];
System.arraycopy(buff,pos,temp,0,size);
return temp;
}
public void writeBytes(byte[] b)throws IOException{
if((buff.length-pos)<b.length){
throw new IOException("Buffer Overflow Exception");
}
System.arraycopy(b,0,buff,pos,b.length);
needsFlush = true;
}
private void write(byte b)throws IOException{
if(pos>=buff.length){
throw new IOException("Buffer Overflow Exception");
}
buff[pos] = b;
pos++;
needsFlush = true;
}
}
JavaIoFaq