Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / java / util / zip / PendingBuffer.java
blob9079b9804b71138d62b5ed654d603d9dfc3ba3d6
1 /* java.util.zip.PendingBuffer
2 Copyright (C) 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package java.util.zip;
40 /**
41 * This class is general purpose class for writing data to a buffer.
43 * It allows you to write bits as well as bytes
45 * Based on DeflaterPending.java
47 * @author Jochen Hoenicke
48 * @date Jan 5, 2000
51 class PendingBuffer
53 protected byte[] buf;
54 int start;
55 int end;
57 int bits;
58 int bitCount;
60 public PendingBuffer()
62 this( 4096 );
65 public PendingBuffer(int bufsize)
67 buf = new byte[bufsize];
70 public final void reset() {
71 start = end = bitCount = 0;
74 public final void writeByte(int b)
76 if (DeflaterConstants.DEBUGGING && start != 0)
77 throw new IllegalStateException();
78 buf[end++] = (byte) b;
81 public final void writeShort(int s)
83 if (DeflaterConstants.DEBUGGING && start != 0)
84 throw new IllegalStateException();
85 buf[end++] = (byte) s;
86 buf[end++] = (byte) (s >> 8);
89 public final void writeInt(int s)
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);
99 public final void writeBlock(byte[] block, int offset, int len)
101 if (DeflaterConstants.DEBUGGING && start != 0)
102 throw new IllegalStateException();
103 System.arraycopy(block, offset, buf, end, len);
104 end += len;
107 public final int getBitCount() {
108 return bitCount;
111 public final void alignToByte() {
112 if (DeflaterConstants.DEBUGGING && start != 0)
113 throw new IllegalStateException();
114 if (bitCount > 0)
116 buf[end++] = (byte) bits;
117 if (bitCount > 8)
118 buf[end++] = (byte) (bits >>> 8);
120 bits = 0;
121 bitCount = 0;
124 public final void writeBits(int b, int count)
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;
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;
147 public final boolean isFlushed() {
148 return end == 0;
152 * Flushes the pending buffer into the given output array. If the
153 * output array is to small, only a partial flush is done.
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.
161 public final int flush(byte[] output, int offset, int length) {
162 if (bitCount >= 8)
164 buf[end++] = (byte) bits;
165 bits >>>= 8;
166 bitCount -= 8;
168 if (length > end - start)
170 length = end - start;
171 System.arraycopy(buf, start, output, offset, length);
172 start = 0;
173 end = 0;
175 else
177 System.arraycopy(buf, start, output, offset, length);
178 start += length;
180 return length;
184 * Flushes the pending buffer and returns that data in a new array
186 * @return the output stream
189 public final byte[] toByteArray()
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;