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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UTFDataFormatException; import java.io.UTFDataFormatException;
import org.apache.activemq.util.ByteSequence;
/** /**
* Optimized ByteArrayInputStream that can be used more than once * 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{ public final class StoreByteArrayInputStream extends InputStream implements DataInput{
private byte[] buf; private byte[] buf;
private int pos; private int pos;
private int offset;
/** /**
* Creates a <code>WireByteArrayInputStream</code>. * Creates a <code>StoreByteArrayInputStream</code>.
* *
* @param buf the input buffer. * @param buf the input buffer.
*/ */
public StoreByteArrayInputStream(byte buf[]){ public StoreByteArrayInputStream(byte buf[]){
this.buf=buf; this.buf=buf;
this.pos=0; 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]); this(new byte[0]);
} }
/**
*
* @return the size
*/
public int 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 * @param newBuff
*/ */
@ -67,6 +84,16 @@ public final class StoreByteArrayInputStream extends InputStream implements Data
buf=newBuff; buf=newBuff;
pos=0; 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 * 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.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UTFDataFormatException; import java.io.UTFDataFormatException;
import org.apache.activemq.util.ByteSequence;
/** /**
* Optimized ByteArrayOutputStream * Optimized ByteArrayOutputStream
* *
@ -59,6 +60,14 @@ public final class StoreByteArrayOutputStream extends OutputStream implements Da
buf=new byte[size]; buf=new byte[size];
pos=0; 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. * Writes the specified byte to this byte array output stream.
@ -117,6 +126,8 @@ public final class StoreByteArrayOutputStream extends OutputStream implements Da
public int size(){ public int size(){
return pos; return pos;
} }
public void writeBoolean(boolean v){ public void writeBoolean(boolean v){
ensureEnoughBuffer(1); ensureEnoughBuffer(1);