StoreByteArrayInputStream
.
+ *
+ * @param buf the input buffer.
+ */
+ public DataByteArrayInputStream(byte buf[]){
+ this.buf=buf;
+ this.pos=0;
+ this.offset = 0;
+ }
+
+ /**
+ * Creates a StoreByteArrayInputStream
.
+ *
+ * @param sequence the input buffer.
+ */
+ public DataByteArrayInputStream(ByteSequence sequence){
+ this.buf=sequence.getData();
+ this.offset=this.pos=sequence.getOffset();
+ }
+
+ /**
+ * Creates WireByteArrayInputStream
with a minmalist byte array
+ */
+ public DataByteArrayInputStream(){
+ this(new byte[0]);
+ }
+
+ /**
+ *
+ * @return the size
+ */
+ public int size(){
+ return pos-offset;
+ }
+
+ /**
+ * @return the underlying data array
+ */
+ public byte[] getRawData(){
+ return buf;
+ }
+
+ /**
+ * reset the StoreByteArrayInputStream
to use an new byte array
+ *
+ * @param newBuff
+ */
+ public void restart(byte[] newBuff){
+ buf=newBuff;
+ pos=0;
+ }
+
+ /**
+ * reset the StoreByteArrayInputStream
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
+ *
+ * @param size
+ */
+ public void restart(int size){
+ if(buf==null||buf.length0
to 255
. If no byte is available because the end of the stream has been
+ * reached, the value -1
is returned.
+ *
+ * This read
method cannot block.
+ *
+ * @return the next byte of data, or -1
if the end of the stream has been reached.
+ */
+ public int read(){
+ return (pos-1
if there is no more data because the
+ * end of the stream has been reached.
+ */
+ public int read(byte b[],int off,int len){
+ if(b==null){
+ throw new NullPointerException();
+ }
+ if(pos>=buf.length){
+ return -1;
+ }
+ if(pos+len>buf.length){
+ len=buf.length-pos;
+ }
+ if(len<=0){
+ return 0;
+ }
+ System.arraycopy(buf,pos,b,off,len);
+ pos+=len;
+ return len;
+ }
+
+ /**
+ * @return the number of bytes that can be read from the input stream without blocking.
+ */
+ public int available(){
+ return buf.length-pos;
+ }
+
+ public void readFully(byte[] b){
+ read(b,0,b.length);
+ }
+
+ public void readFully(byte[] b,int off,int len){
+ read(b,off,len);
+ }
+
+ public int skipBytes(int n){
+ if(pos+n>buf.length){
+ n=buf.length-pos;
+ }
+ if(n<0){
+ return 0;
+ }
+ pos+=n;
+ return n;
+ }
+
+ public boolean readBoolean(){
+ return read()!=0;
+ }
+
+ public byte readByte(){
+ return (byte) read();
+ }
+
+ public int readUnsignedByte(){
+ return read();
+ }
+
+ public short readShort(){
+ int ch1=read();
+ int ch2=read();
+ return (short) ((ch1<<8)+(ch2<<0));
+ }
+
+ public int readUnsignedShort(){
+ int ch1=read();
+ int ch2=read();
+ return ((ch1<<8)+(ch2<<0));
+ }
+
+ public char readChar(){
+ int ch1=read();
+ int ch2=read();
+ return (char) ((ch1<<8)+(ch2<<0));
+ }
+
+ public int readInt(){
+ int ch1=read();
+ int ch2=read();
+ int ch3=read();
+ int ch4=read();
+ return ((ch1<<24)+(ch2<<16)+(ch3<<8)+(ch4<<0));
+ }
+
+ public long readLong(){
+ return (((long) buf[pos++]<<56)+((long) (buf[pos++]&255)<<48)+((long) (buf[pos++]&255)<<40)
+ +((long) (buf[pos++]&255)<<32)+((long) (buf[pos++]&255)<<24)+((buf[pos++]&255)<<16)
+ +((buf[pos++]&255)<<8)+((buf[pos++]&255)<<0));
+ }
+
+ public float readFloat() throws IOException{
+ return Float.intBitsToFloat(readInt());
+ }
+
+ public double readDouble() throws IOException{
+ return Double.longBitsToDouble(readLong());
+ }
+
+ public String readLine(){
+ int start=pos;
+ while(poslen
bytes from the specified byte array starting at offset off
to this byte
+ * array output stream.
+ *
+ * @param b the data.
+ * @param off the start offset in the data.
+ * @param len the number of bytes to write.
+ */
+ public void write(byte b[],int off,int len){
+ if(len==0){
+ return;
+ }
+ int newcount=pos+len;
+ ensureEnoughBuffer(newcount);
+ System.arraycopy(b,off,buf,pos,len);
+ pos=newcount;
+ }
+
+ /**
+ * @return the underlying byte[] buffer
+ */
+ public byte[] getData(){
+ return buf;
+ }
+
+ /**
+ * reset the output stream
+ */
+ public void reset(){
+ pos=0;
+ }
+
+ /**
+ * Set the current position for writing
+ *
+ * @param offset
+ */
+ public void position(int offset){
+ ensureEnoughBuffer(offset);
+ pos=offset;
+ }
+
+ public int size(){
+ return pos;
+ }
+
+
+
+ public void writeBoolean(boolean v){
+ ensureEnoughBuffer(1);
+ buf[pos++]=(byte) (v?1:0);
+ }
+
+ public void writeByte(int v){
+ ensureEnoughBuffer(1);
+ buf[pos++]=(byte) (v>>>0);
+ }
+
+ public void writeShort(int v){
+ ensureEnoughBuffer(2);
+ buf[pos++]=(byte) (v>>>8);
+ buf[pos++]=(byte) (v>>>0);
+ }
+
+ public void writeChar(int v){
+ ensureEnoughBuffer(2);
+ buf[pos++]=(byte) (v>>>8);
+ buf[pos++]=(byte) (v>>>0);
+ }
+
+ public void writeInt(int v){
+ ensureEnoughBuffer(4);
+ buf[pos++]=(byte) (v>>>24);
+ buf[pos++]=(byte) (v>>>16);
+ buf[pos++]=(byte) (v>>>8);
+ buf[pos++]=(byte) (v>>>0);
+ }
+
+ public void writeLong(long v){
+ ensureEnoughBuffer(8);
+ buf[pos++]=(byte) (v>>>56);
+ buf[pos++]=(byte) (v>>>48);
+ buf[pos++]=(byte) (v>>>40);
+ buf[pos++]=(byte) (v>>>32);
+ buf[pos++]=(byte) (v>>>24);
+ buf[pos++]=(byte) (v>>>16);
+ buf[pos++]=(byte) (v>>>8);
+ buf[pos++]=(byte) (v>>>0);
+ }
+
+ public void writeFloat(float v) throws IOException{
+ writeInt(Float.floatToIntBits(v));
+ }
+
+ public void writeDouble(double v) throws IOException{
+ writeLong(Double.doubleToLongBits(v));
+ }
+
+ public void writeBytes(String s){
+ int length=s.length();
+ for(int i=0;i