support for ByteSequence

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@469986 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2006-11-01 17:13:39 +00:00
parent b3e315fcc2
commit 3f74b0e1c0
2 changed files with 41 additions and 3 deletions

View File

@ -21,6 +21,7 @@ import java.io.DataInput;
import java.io.IOException;
import java.io.InputStream;
import java.io.UTFDataFormatException;
import org.apache.activemq.util.ByteSequence;
/**
* Optimized ByteArrayInputStream that can be used more than once
*
@ -29,15 +30,27 @@ import java.io.UTFDataFormatException;
public final class StoreByteArrayInputStream extends InputStream implements DataInput{
private byte[] buf;
private int pos;
private int offset;
/**
* Creates a <code>WireByteArrayInputStream</code>.
* Creates a <code>StoreByteArrayInputStream</code>.
*
* @param buf the input buffer.
*/
public StoreByteArrayInputStream(byte buf[]){
this.buf=buf;
this.pos=0;
this.offset = 0;
}
/**
* Creates a <code>StoreByteArrayInputStream</code>.
*
* @param sequence the input buffer.
*/
public StoreByteArrayInputStream(ByteSequence sequence){
this.buf=sequence.getData();
this.offset=this.pos=sequence.getOffset();
}
/**
@ -47,8 +60,12 @@ public final class StoreByteArrayInputStream extends InputStream implements Data
this(new byte[0]);
}
/**
*
* @return the size
*/
public int size(){
return pos;
return pos-offset;
}
/**
@ -59,7 +76,7 @@ public final class StoreByteArrayInputStream extends InputStream implements Data
}
/**
* reset the <code>WireByteArrayInputStream</code> to use an new byte array
* reset the <code>StoreByteArrayInputStream</code> to use an new byte array
*
* @param newBuff
*/
@ -68,6 +85,16 @@ public final class StoreByteArrayInputStream extends InputStream implements Data
pos=0;
}
/**
* reset the <code>StoreByteArrayInputStream</code> to use an new ByteSequence
* @param sequence
*
*/
public void restart(ByteSequence sequence){
this.buf=sequence.getData();
this.pos=sequence.getOffset();
}
/**
* re-start the input stream - reusing the current buffer
*

View File

@ -21,6 +21,7 @@ import java.io.DataOutput;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UTFDataFormatException;
import org.apache.activemq.util.ByteSequence;
/**
* Optimized ByteArrayOutputStream
*
@ -60,6 +61,14 @@ public final class StoreByteArrayOutputStream extends OutputStream implements Da
pos=0;
}
/**
* Get a ByteSequence from the stream
* @return the byte sequence
*/
public ByteSequence toByteSequence() {
return new ByteSequence(buf, 0, pos);
}
/**
* Writes the specified byte to this byte array output stream.
*
@ -118,6 +127,8 @@ public final class StoreByteArrayOutputStream extends OutputStream implements Da
return pos;
}
public void writeBoolean(boolean v){
ensureEnoughBuffer(1);
buf[pos++]=(byte) (v?1:0);