Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 2009-2011, EzWare
   * All rights reserved.
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   * Redistributions of source code must retain the above copyright notice,
  * this list of conditions and the following disclaimer.Redistributions
  * in binary form must reproduce the above copyright notice, this list of
  * conditions and the following disclaimer in the documentation and/or
  * other materials provided with the distribution.Neither the name of the
  * EzWare nor the names of its contributors may be used to endorse or
  * promote products derived from this software without specific prior
  * written permission.
 package org.oxbow.swingbits.util.copy;
ByteArrayOutputStream implementation that doesn't synchronize methods and doesn't copy the data on toByteArray().
 public class FastByteArrayOutputStream extends OutputStream {
Buffer and size
     protected byte[] buf = null;
     protected int size = 0;

Constructs a stream with buffer capacity size 5K
     public FastByteArrayOutputStream() {
         this(5 * 1024);

Constructs a stream with the given initial size
     public FastByteArrayOutputStream(int initSize) {
         this. = 0;
         this. = new byte[initSize];

Ensures that we have a large enough buffer for the given size.
     private void verifyBufferSize(int sz) {
         if (sz > .) {
             byte[] old = ;
              = new byte[Math.max(sz, 2 * . )];
             System.arraycopy(old, 0, , 0, old.length);
 //            old = null;
     public int getSize() {
         return ;

Returns the byte array containing the written data. Note that this array will almost always be larger than the amount of data actually written.
     public byte[] getByteArray() {
         return ;
     public final void write(byte b[]) {
         verifyBufferSize( + b.length);
         System.arraycopy(b, 0, b.length);
          += b.length;
     public final void write(byte b[], int offint len) {
         verifyBufferSize( + len);
          += len;
    public final void write(int b) {
        verifyBufferSize( + 1);
        [++] = (byteb;
    public void reset() {
         = 0;

Returns a ByteArrayInputStream for reading back the written data
    public InputStream getInputStream() {
        return new FastByteArrayInputStream();
New to GrepCode? Check out our FAQ X