001/*
002 * $RCSfile: ByteOutputBuffer.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:07 $
005 * $State: Exp $
006 *
007 * Class:                   ByteOutputBuffer
008 *
009 * Description:             Provides buffering for byte based output, similar
010 *                          to the standard class ByteArrayOutputStream
011 *
012 *                          the old jj2000.j2k.io.ByteArrayOutput class by
013 *                          Diego SANTA CRUZ, Apr-26-1999
014 *
015 *
016 * COPYRIGHT:
017 *
018 * This software module was originally developed by Raphaël Grosbois and
019 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
020 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
021 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
022 * Centre France S.A) in the course of development of the JPEG2000
023 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
024 * software module is an implementation of a part of the JPEG 2000
025 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
026 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
027 * Partners) agree not to assert against ISO/IEC and users of the JPEG
028 * 2000 Standard (Users) any of their rights under the copyright, not
029 * including other intellectual property rights, for this software module
030 * with respect to the usage by ISO/IEC and Users of this software module
031 * or modifications thereof for use in hardware or software products
032 * claiming conformance to the JPEG 2000 Standard. Those intending to use
033 * this software module in hardware or software products are advised that
034 * their use may infringe existing patents. The original developers of
035 * this software module, JJ2000 Partners and ISO/IEC assume no liability
036 * for use of this software module or modifications thereof. No license
037 * or right to this software module is granted for non JPEG 2000 Standard
038 * conforming products. JJ2000 Partners have full right to use this
039 * software module for his/her own purpose, assign or donate this
040 * software module to any third party and to inhibit third parties from
041 * using this software module for non JPEG 2000 Standard conforming
042 * products. This copyright notice must be included in all copies or
043 * derivative works of this software module.
044 *
045 * Copyright (c) 1999/2000 JJ2000 Partners.
046 *
047 *
048 *
049 */
050
051
052package jj2000.j2k.entropy.encoder;
053
054import java.io.*;
055
056/**
057 * This class provides a buffering output stream similar to
058 * ByteArrayOutputStream, with some additional methods.
059 *
060 * <P>Once an array has been written to an output stream or to a byte
061 * array, the object can be reused as a new stream if the reset()
062 * method is called.
063 *
064 * <P>Unlike the ByteArrayOutputStream class, this class is not thread safe.
065 *
066 * @see #reset
067 * */
068public class ByteOutputBuffer {
069
070    /** The buffer where the data is stored */
071    byte buf[];
072
073    /** The number of valid bytes in the buffer */
074    int count;
075
076    /** The buffer increase size */
077    public final static int BUF_INC = 512;
078
079    /** The default initial buffer size */
080    public final static int BUF_DEF_LEN = 256;
081
082    /**
083     * Creates a new byte array output stream. The buffer capacity is
084     * initially BUF_DEF_LEN bytes, though its size increases if necessary.
085     *
086     *
087     * */
088    public ByteOutputBuffer() {
089        buf = new byte[BUF_DEF_LEN];
090    }
091
092    /**
093     * Creates a new byte array output stream, with a buffer capacity
094     * of the specified size, in bytes.
095     *
096     * @param size the initial size.
097     *
098     *
099     * */
100    public ByteOutputBuffer(int size) {
101        buf = new byte[size];
102    }
103
104    /**
105     * Writes the specified byte to this byte array output stream. The
106     * functionality provided by this implementation is the same as for the
107     * one in the superclass, however this method is not synchronized and
108     * therefore not safe thread, but faster.
109     *
110     * @param b The byte to write
111     *
112     *
113     * */
114    public final void write(int b) {
115        if (count == buf.length) { // Resize buffer
116            byte tmpbuf[] = buf;
117            buf = new byte[buf.length+BUF_INC];
118            System.arraycopy(tmpbuf,0,buf,0,count);
119        }
120        buf[count++] = (byte) b;
121    }
122
123    /**
124     * Copies the specified part of the stream to the 'outbuf' byte
125     * array.
126     *
127     * @param off The index of the first element in the stream to
128     * copy.
129     *
130     * @param len The number of elements of the array to copy
131     *
132     * @param outbuf The destination array
133     *
134     * @param outoff The index of the first element in 'outbuf' where
135     * to write the data.
136     *
137     *
138     * */
139    public void toByteArray(int off, int len, byte outbuf[], int outoff) {
140        // Copy the data
141        System.arraycopy(buf,off,outbuf,outoff,len);
142    }
143
144    /**
145     * Returns the number of valid bytes in the output buffer (count class
146     * variable).
147     *
148     * @return The number of bytes written to the buffer
149     *
150     * */
151    public int size() {
152        return count;
153    }
154
155    /**
156     * Discards all the buffered data, by resetting the counter of written
157     * bytes to 0.
158     *
159     * */
160    public void reset() {
161        count = 0;
162    }
163
164    /**
165     * Returns the byte buffered at the given position in the buffer. The
166     * position in the buffer is the index of the 'write()' method call after
167     * the last call to 'reset()'.
168     *
169     * @param pos The position of the byte to return
170     *
171     * @return The value (betweeb 0-255) of the byte at position 'pos'.
172     *
173     * */
174    public int getByte(int pos) {
175        if (pos >= count) {
176            throw new IllegalArgumentException();
177        }
178        return buf[pos]&0xFF;
179    }
180}