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: package jj2000.j2k.entropy.encoder;
052:
053: import java.io.*;
054:
055: /**
056: * This class provides a buffering output stream similar to
057: * ByteArrayOutputStream, with some additional methods.
058: *
059: * <P>Once an array has been written to an output stream or to a byte
060: * array, the object can be reused as a new stream if the reset()
061: * method is called.
062: *
063: * <P>Unlike the ByteArrayOutputStream class, this class is not thread safe.
064: *
065: * @see #reset
066: * */
067: public class ByteOutputBuffer {
068:
069: /** The buffer where the data is stored */
070: byte buf[];
071:
072: /** The number of valid bytes in the buffer */
073: int count;
074:
075: /** The buffer increase size */
076: public final static int BUF_INC = 512;
077:
078: /** The default initial buffer size */
079: public final static int BUF_DEF_LEN = 256;
080:
081: /**
082: * Creates a new byte array output stream. The buffer capacity is
083: * initially BUF_DEF_LEN bytes, though its size increases if necessary.
084: *
085: *
086: * */
087: public ByteOutputBuffer() {
088: buf = new byte[BUF_DEF_LEN];
089: }
090:
091: /**
092: * Creates a new byte array output stream, with a buffer capacity
093: * of the specified size, in bytes.
094: *
095: * @param size the initial size.
096: *
097: *
098: * */
099: public ByteOutputBuffer(int size) {
100: buf = new byte[size];
101: }
102:
103: /**
104: * Writes the specified byte to this byte array output stream. The
105: * functionality provided by this implementation is the same as for the
106: * one in the superclass, however this method is not synchronized and
107: * therefore not safe thread, but faster.
108: *
109: * @param b The byte to write
110: *
111: *
112: * */
113: public final void write(int b) {
114: if (count == buf.length) { // Resize buffer
115: byte tmpbuf[] = buf;
116: buf = new byte[buf.length + BUF_INC];
117: System.arraycopy(tmpbuf, 0, buf, 0, count);
118: }
119: buf[count++] = (byte) b;
120: }
121:
122: /**
123: * Copies the specified part of the stream to the 'outbuf' byte
124: * array.
125: *
126: * @param off The index of the first element in the stream to
127: * copy.
128: *
129: * @param len The number of elements of the array to copy
130: *
131: * @param outbuf The destination array
132: *
133: * @param outoff The index of the first element in 'outbuf' where
134: * to write the data.
135: *
136: *
137: * */
138: public void toByteArray(int off, int len, byte outbuf[], int outoff) {
139: // Copy the data
140: System.arraycopy(buf, off, outbuf, outoff, len);
141: }
142:
143: /**
144: * Returns the number of valid bytes in the output buffer (count class
145: * variable).
146: *
147: * @return The number of bytes written to the buffer
148: *
149: * */
150: public int size() {
151: return count;
152: }
153:
154: /**
155: * Discards all the buffered data, by resetting the counter of written
156: * bytes to 0.
157: *
158: * */
159: public void reset() {
160: count = 0;
161: }
162:
163: /**
164: * Returns the byte buffered at the given position in the buffer. The
165: * position in the buffer is the index of the 'write()' method call after
166: * the last call to 'reset()'.
167: *
168: * @param pos The position of the byte to return
169: *
170: * @return The value (betweeb 0-255) of the byte at position 'pos'.
171: *
172: * */
173: public int getByte(int pos) {
174: if (pos >= count) {
175: throw new IllegalArgumentException();
176: }
177: return buf[pos] & 0xFF;
178: }
179: }
|