Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.io;
  
  
  
Extends ByteArrayOutputStream, but exposes the internal buffer. Using this, callers don't need to call toByteArray() which copies the internal buffer.

Also overrides the superclass' behavior of always doubling the size of the internal buffer any time more capacity is needed. This class doubles the size until the internal buffer reaches a configurable max size (default is 4MB), after which it begins growing the buffer in 25% increments. This is intended to help prevent an OutOfMemoryError during a resize of a large buffer.

A version of this class was originally created by Bela Ban as part of the JGroups library.

This class is not threadsafe as it will not support concurrent readers and writers.

Author(s):
Brian Stansberry
Since:
4.0
 
 public final class ExposedByteArrayOutputStream extends ByteArrayOutputStream implements ByteOutput {
   
Default buffer size after which if more buffer capacity is needed the buffer will grow by 25% rather than 100%
 
    public static final int DEFAULT_DOUBLING_SIZE = 4 * 1024 * 1024; // 4MB
 
    private int maxDoublingSize = ;
 
    public ExposedByteArrayOutputStream() {
       super();
    }
 
    public ExposedByteArrayOutputStream(int size) {
       super(size);
    }

   
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Parameters:
size the initial size.
maxDoublingSize the buffer size, after which if more capacity is needed the buffer will grow by 25% rather than 100%
Throws:
java.lang.IllegalArgumentException if size is negative.
 
    public ExposedByteArrayOutputStream(int sizeint maxDoublingSize) {
       super(size);
       this. = maxDoublingSize;
    }

   
Gets the internal buffer array. Note that the length of this array will almost certainly be longer than the data written to it; call size() to get the number of bytes of actual data.
 
    public final byte[] getRawBuffer() {
       return ;
    }
 
    @Override
    public final void write(byte[] bint offint len) {
       if ((off < 0) || (off > b.length) || (len < 0) ||
             ((off + len) > b.length) || ((off + len) < 0)) {
          throw new IndexOutOfBoundsException();
       } else if (len == 0) {
          return;
       }
 
       int newcount =  + len;
       if (newcount > .) {
          byte newbuf[] = new byte[getNewBufferSize(.newcount)];
          System.arraycopy(, 0, newbuf, 0, );
           = newbuf;
       }
 
       System.arraycopy(bofflen);
        = newcount;
    }
 
    @Override
    public final void write(int b) {
       int newcount =  + 1;
       if (newcount > .) {
          byte newbuf[] = new byte[getNewBufferSize(.newcount)];
          System.arraycopy(, 0, newbuf, 0, );
           = newbuf;
       }
       [] = (byteb;
        = newcount;
    }

   
Gets the highest internal buffer size after which if more capacity is needed the buffer will grow in 25% increments rather than 100%.
 
    public final int getMaxDoublingSize() {
       return ;
    }

   
Gets the number of bytes to which the internal buffer should be resized.

Parameters:
curSize the current number of bytes
minNewSize the minimum number of bytes required
Returns:
the size to which the internal buffer should be resized
   public final int getNewBufferSize(int curSizeint minNewSize) {
      if (curSize <= )
         return Math.max(curSize << 1, minNewSize);
      else
         return Math.max(curSize + (curSize >> 2), minNewSize);
   }
   
   
Overriden only to avoid unneeded synchronization
   public final int size() {
      return ;
   }
New to GrepCode? Check out our FAQ X