Save This Page
Home » openjdk-7 » java » util » zip » [javadoc | source]
    1   /* java.util.zip.PendingBuffer
    2      Copyright (C) 2001 Free Software Foundation, Inc.
    3   
    4   This file is part of GNU Classpath.
    5   
    6   GNU Classpath is free software; you can redistribute it and/or modify
    7   it under the terms of the GNU General Public License as published by
    8   the Free Software Foundation; either version 2, or (at your option)
    9   any later version.
   10   
   11   GNU Classpath is distributed in the hope that it will be useful, but
   12   WITHOUT ANY WARRANTY; without even the implied warranty of
   13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14   General Public License for more details.
   15   
   16   You should have received a copy of the GNU General Public License
   17   along with GNU Classpath; see the file COPYING.  If not, write to the
   18   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   19   02110-1301 USA.
   20   
   21   Linking this library statically or dynamically with other modules is
   22   making a combined work based on this library.  Thus, the terms and
   23   conditions of the GNU General Public License cover the whole
   24   combination.
   25   
   26   As a special exception, the copyright holders of this library give you
   27   permission to link this library with independent modules to produce an
   28   executable, regardless of the license terms of these independent
   29   modules, and to copy and distribute the resulting executable under
   30   terms of your choice, provided that you also meet, for each linked
   31   independent module, the terms and conditions of the license of that
   32   module.  An independent module is a module which is not derived from
   33   or based on this library.  If you modify this library, you may extend
   34   this exception to your version of the library, but you are not
   35   obligated to do so.  If you do not wish to do so, delete this
   36   exception statement from your version. */
   37   
   38   package java.util.zip;
   39   
   40   /**
   41    * This class is general purpose class for writing data to a buffer.
   42    *
   43    * It allows you to write bits as well as bytes 
   44    *
   45    * Based on DeflaterPending.java
   46    *
   47    * @author Jochen Hoenicke
   48    * @date Jan 5, 2000 
   49    */
   50   
   51   class PendingBuffer
   52   {
   53     protected byte[] buf;
   54     int    start;
   55     int    end;
   56   
   57     int    bits;
   58     int    bitCount;
   59   
   60     public PendingBuffer()
   61     {
   62       this( 4096 );
   63     }
   64   
   65     public PendingBuffer(int bufsize)
   66     {
   67       buf = new byte[bufsize];
   68     }
   69   
   70     public final void reset() {
   71       start = end = bitCount = 0;
   72     }
   73   
   74     public final void writeByte(int b) 
   75     {
   76       if (DeflaterConstants.DEBUGGING && start != 0)
   77         throw new IllegalStateException();
   78       buf[end++] = (byte) b;
   79     }
   80   
   81     public final void writeShort(int s) 
   82     {
   83       if (DeflaterConstants.DEBUGGING && start != 0)
   84         throw new IllegalStateException();
   85       buf[end++] = (byte) s;
   86       buf[end++] = (byte) (s >> 8);
   87     }
   88   
   89     public final void writeInt(int s) 
   90     {
   91       if (DeflaterConstants.DEBUGGING && start != 0)
   92         throw new IllegalStateException();
   93       buf[end++] = (byte) s;
   94       buf[end++] = (byte) (s >> 8);
   95       buf[end++] = (byte) (s >> 16);
   96       buf[end++] = (byte) (s >> 24);
   97     }
   98   
   99     public final void writeBlock(byte[] block, int offset, int len) 
  100     {
  101       if (DeflaterConstants.DEBUGGING && start != 0)
  102         throw new IllegalStateException();
  103       System.arraycopy(block, offset, buf, end, len);
  104       end += len;
  105     }
  106   
  107     public final int getBitCount() {
  108       return bitCount;
  109     }
  110   
  111     public final void alignToByte() {
  112       if (DeflaterConstants.DEBUGGING && start != 0)
  113         throw new IllegalStateException();
  114       if (bitCount > 0)
  115         {
  116   	buf[end++] = (byte) bits;
  117   	if (bitCount > 8)
  118   	  buf[end++] = (byte) (bits >>> 8);
  119         }
  120       bits = 0;
  121       bitCount = 0;
  122     }
  123   
  124     public final void writeBits(int b, int count)
  125     {
  126        if (DeflaterConstants.DEBUGGING && start != 0)
  127          throw new IllegalStateException();
  128        if (DeflaterConstants.DEBUGGING)
  129          System.err.println("writeBits("+Integer.toHexString(b)+","+count+")");
  130       bits |= b << bitCount;
  131       bitCount += count;
  132       if (bitCount >= 16) {
  133         buf[end++] = (byte) bits;
  134         buf[end++] = (byte) (bits >>> 8);
  135         bits >>>= 16;
  136         bitCount -= 16;
  137       }
  138     }
  139   
  140     public final void writeShortMSB(int s) {
  141       if (DeflaterConstants.DEBUGGING && start != 0)
  142         throw new IllegalStateException();
  143       buf[end++] = (byte) (s >> 8);
  144       buf[end++] = (byte) s;
  145     }
  146   
  147     public final boolean isFlushed() {
  148       return end == 0;
  149     }
  150   
  151     /**
  152      * Flushes the pending buffer into the given output array.  If the
  153      * output array is to small, only a partial flush is done.
  154      *
  155      * @param output the output array;
  156      * @param offset the offset into output array;
  157      * @param length the maximum number of bytes to store;
  158      * @exception IndexOutOfBoundsException if offset or length are
  159      * invalid.
  160      */
  161     public final int flush(byte[] output, int offset, int length) {
  162       if (bitCount >= 8)
  163         {
  164   	buf[end++] = (byte) bits;
  165   	bits >>>= 8;
  166   	bitCount -= 8;
  167         }
  168       if (length > end - start)
  169         {
  170   	length = end - start;
  171   	System.arraycopy(buf, start, output, offset, length);
  172   	start = 0;
  173   	end = 0;
  174         }
  175       else
  176         {
  177   	System.arraycopy(buf, start, output, offset, length);
  178   	start += length;
  179         }
  180       return length;
  181     }
  182   
  183     /**
  184      * Flushes the pending buffer and returns that data in a new array
  185      * 
  186      * @return the output stream
  187      */
  188   
  189     public final byte[] toByteArray()
  190     {
  191       byte[] ret = new byte[ end - start ];
  192       System.arraycopy(buf, start, ret, 0, ret.length);
  193       start = 0;
  194       end = 0;
  195       return ret;
  196     }
  197   
  198   
  199   }
  200   

Save This Page
Home » openjdk-7 » java » util » zip » [javadoc | source]