Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * @(#)AppendableByteArrayInputStream.java  1.0  2011-08-28
   * 
   * Copyright (c) 2011 Werner Randelshofer, Goldau, Switzerland.
   * All rights reserved.
   * 
   * You may not use, copy or modify this file, except in compliance with the
   * license agreement you entered into with Werner Randelshofer.
   * For details see accompanying license terms.
  */
 package org.monte.media.io;
 
A ByteArrayInputStream which allows to replace the byte buffer underneath.

Author(s):
Werner Randelshofer
Version:
1.0 2011-08-28 Created.
 
 
     public AppendableByteArrayInputStream(byte[] bufint offsetint length) {
         super(bufoffsetlength);
         //  System.out.println("AppendableByteArrayInputStream   pos="+pos+" count="+count);
     }
 
     public AppendableByteArrayInputStream(byte[] buf) {
         super(buf);
         // System.out.println("AppendableByteArrayInputStream   pos="+pos+" count="+count);
     }
 
     @Override
     public synchronized int read() {
         int b = super.read();
         /*
         if (b!=-1) {
         String hex="0"+Integer.toHexString(b);
         System.out.println(hex.substring(hex.length()-2));
         }*/
         return b;
     }
 
     @Override
     public synchronized int read(byte[] bint offint len) {
         // System.out.println("AppendableByteArrayInputStream.read...   pos="+pos+" count="+count);
         int count = super.read(bofflen);
         /*
         if (count>0) {
         for (int i=0;i<count;i++) {
         if (i%16==15) System.out.println();
         String hex="0"+Integer.toHexString(b[i]);
         System.out.print(hex.substring(hex.length()-2));
         }
         System.out.println("   pos="+pos+" count="+this.count);
         }*/
         return count;
     }

    
Appends new data to the buffer.

Parameters:
buf Data.
offset Offset in the data.
length Length of the data.
discard True if data which has already been read can be discarded.
 
     public void appendBuffer(byte[] bufint offsetint lengthboolean discard) {
         if (discard) {
             if (this..length >=  -  + length) {
                 // => the buffer has enough space for the existing data and the new data
                 System.arraycopy(this.this., 0,  - );
                 System.arraycopy(bufoffsetthis. - length);
                 this. =  -  + length;
                 this. = 0;
                 this. = 0;
             } else {
                 // => the buffer does not have enough space for the new data
                 byte[] newBuf = new byte[( -  + length + 31) / 32 * 32];
                 System.arraycopy(this.newBuf, 0,  - );
                 System.arraycopy(bufoffsetnewBuf - length);
                 this. = newBuf;
                 this. =  -  + length;
                 this. = 0;
                 this. = 0;
             }
         } else {
             if (this..length >=  + length) {
                 // => the buffer has enough space for the existing data and the new data
                 System.arraycopy(bufoffsetthis.length);
                 this. =  + length;
             } else {
                 // => the buffer does not have enough space for the new data
                 byte[] newBuf = new byte[(this..length + length + 31) / 32 * 32];
                 System.arraycopy(this., 0, newBuf, 0, );
                 System.arraycopy(bufoffsetnewBuflength);
                 this. = newBuf;
                 this. =  + length;
             }
        }
        //System.out.println("AppendableByteArrayInputStream.appendBuffer   pos="+pos+" count="+count);
    }

    
Sets the buffer and resets the stream. This will overwrite the data array in the buffer, if it is large enough. Otherwise it will create a new data array and copy the data into it.

Parameters:
buf Data.
offset Offset in the data.
length Length of the data.
    public void setBuffer(byte[] bufint offsetint length) {
        if (this..length >= length) {
            // => the buffer has enough space for the existing data and the new data
            System.arraycopy(bufoffsetthis., 0, length);
            this. = length;
            this. = 0;
            this. = 0;
        } else {
            // => the buffer may not be overwritten or does not have enough space for the new data
            this. = null;
            this. = new byte[(length + 31) & ~31];
            System.arraycopy(bufoffsetthis., 0, length);
            this. = length;
            this. = 0;
            this. = 0;
        }
    }
    public static void main(String[] args) {
        byte[] b = new byte[5];
        int count = 0;
        for (int i = 0; i < b.lengthi++) {
            b[i] = (bytecount++;
        }
        for (int j = 0; j < 3; j++) {
            ..println(in.read());
        }
        b = new byte[4];
        for (int i = 0; i < b.lengthi++) {
            b[i] = (bytecount++;
        }
        in.appendBuffer(b, 0, b.lengthtrue);
        for (int j = 0; j < 3; j++) {
            ..println(in.read());
        }
        b = new byte[6];
        for (int i = 0; i < b.lengthi++) {
            b[i] = (bytecount++;
        }
        in.appendBuffer(b, 0, b.lengthtrue);
        for (int d = in.read(); d >= 0; d = in.read()) {
            ..println(d);
        }
    }
New to GrepCode? Check out our FAQ X