Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
 /*
  * Created on Oct 15, 2005
  */
  
 package org.jboss.remoting.transport.multiplex.utility;
 
ShrinkableByteArrayOutputStream extends java.io.ByteArrayOutputStream and adds the following features:

  1. Rather than creating a new byte array with each call to toByteArray(), it returns a reference to its internal byte array. start() returns the position of the next available byte and available() returns the number of bytes of content, starting at start(), are available.
  2. It reuses its capacity, treating its byte array as a circular queue. When write() is called, if there is too little space at the end of the buffer, and less than half of the capacity is currently in use, it will shift the current contents of its buffer to position 0.
  3. When toByteArray() is called, if less than a quarter of the current capacity is in use and the current capacity is greater than MIN_LENGTH (currently defined as 1024), it will copy the current contents to a byte array one half the size of the current byte array.

Deprecated:
As of release 2.4.0 the multiplex transport will no longer be actively supported.
Author(s):
Ron Sigal
Version:
$Revision: 3443 $

Copyright (c) 2005

 
 
 {
    static private int MIN_LENGTH = 1024;
    private int used = 0;
    private int nextUsed = 0;
    private int bytesReturned = 0;
   
   
   
Create a new ShrinkableByteArrayOutputStream.
 
    {
       super();
    }
   
   
   
Create a new ShrinkableByteArrayOutputStream.
 
    public ShrinkableByteArrayOutputStream(int size)
    {
       super(size);
    }
   
   
   
Returns number of bytes of content which can be retrieved.

Returns:
number of bytes of content which can be retrieved
 
    public int available()
    {
       return  - ;
    }
   
   
   
Returns number of bytes of content returned by last call to toByteArray().

Returns:
number of bytes of content returned by last call to toByteArray()
   public int bytesReturned()
   {
      return ;
   }
   
   
   
Returns position of next available byte of content in byte array returned by toByteArray().

Returns:
position of next available byte of content in byte array returned by toByteArray()
   public int start()
   {
      return ;
   }
   
   
   
Returns reference to internal byte array.

Parameters:
length number of bytes desired
Returns:
reference to internal byte array
   public byte[] toByteArray(int length)
   {
       = ;
      int currentLength = .;
      int quarterLength = currentLength >> 2;
      
      if (currentLength >  && currentLength -  <= quarterLength)
      {
         byte newbuf[] = new byte[currentLength >> 1];
         System.arraycopy(newbuf, 0, available());
          = newbuf;
          -= ;
          = 0;
      }
      
       = Math.min( - length);
       =  + ;
      return ;
   }

   
   
Writes the specified byte to this byte array output stream.

Parameters:
b the byte to be written.
   public void write(int b
   {
      if ( == . &&  > . >> 1)
      {
         System.arraycopy(, 0,  - );
          -= ;
          -= ;
          = 0;
      }
      
      super.write(b);
   }
   
   
   
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

Parameters:
b the data.
off the start offset in the data.
len the number of bytes to write.
   public void write(byte b[], int offint len)
   {
       if ( + len > . &&  > . >> 1)
       {
          System.arraycopy(, 0,  - );
           -= ;
           -= ;
           = 0;
       }
       
       super.write(bofflen);
   }
   
   
   protected int getCount()
   {
      return super.size();
   }
   
   
   protected int getLength()
   {
      return .;
   }
   
   
   protected int getNextUsed()
   {
      return ;
   }
   
   
   protected int getUsed()
   {
      return ;
   }
New to GrepCode? Check out our FAQ X