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)
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
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
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
;
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
60 public PendingBuffer()
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
);
107 public final int getBitCount() {
111 public final void alignToByte() {
112 if (DeflaterConstants
.DEBUGGING
&& start
!= 0)
113 throw new IllegalStateException();
116 buf
[end
++] = (byte) bits
;
118 buf
[end
++] = (byte) (bits
>>> 8);
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
;
132 if (bitCount
>= 16) {
133 buf
[end
++] = (byte) bits
;
134 buf
[end
++] = (byte) (bits
>>> 8);
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() {
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
161 public final int flush(byte[] output
, int offset
, int length
) {
164 buf
[end
++] = (byte) bits
;
168 if (length
> end
- start
)
170 length
= end
- start
;
171 System
.arraycopy(buf
, start
, output
, offset
, length
);
177 System
.arraycopy(buf
, start
, output
, offset
, 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
);