3
// Copyright (C) 2001 Mike Krueger
4
// Copyright (C) 2004 John Reilly
6
// This file was translated from java, it was part of the GNU Classpath
7
// Copyright (C) 2001 Free Software Foundation, Inc.
9
// This program is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
// Linking this library statically or dynamically with other modules is
24
// making a combined work based on this library. Thus, the terms and
25
// conditions of the GNU General Public License cover the whole
28
// As a special exception, the copyright holders of this library give you
29
// permission to link this library with independent modules to produce an
30
// executable, regardless of the license terms of these independent
31
// modules, and to copy and distribute the resulting executable under
32
// terms of your choice, provided that you also meet, for each linked
33
// independent module, the terms and conditions of the license of that
34
// module. An independent module is a module which is not derived from
35
// or based on this library. If you modify this library, you may extend
36
// this exception to your version of the library, but you are not
37
// obligated to do so. If you do not wish to do so, delete this
38
// exception statement from your version.
42
namespace PdfSharp.SharpZipLib.Zip.Compression
46
/// This class is general purpose class for writing data to a buffer.
48
/// It allows you to write bits as well as bytes
49
/// Based on DeflaterPending.java
51
/// author of the original java version : Jochen Hoenicke
53
internal class PendingBuffer
55
/// <summary>Internal work buffer
66
/// construct instance using default buffer size of 4096
68
public PendingBuffer() : this( 4096 )
74
/// construct instance using specified buffer size
76
/// <param name="bufsize">
77
/// size to use for internal buffer
79
public PendingBuffer(int bufsize)
81
buf = new byte[bufsize];
85
/// Clear internal state/buffers
89
start = end = bitCount = 0;
93
/// write a byte to buffer
98
public void WriteByte(int b)
100
if (DeflaterConstants.DEBUGGING && start != 0) {
101
throw new SharpZipBaseException();
103
buf[end++] = (byte) b;
107
/// Write a short value to buffer LSB first
112
public void WriteShort(int s)
114
if (DeflaterConstants.DEBUGGING && start != 0) {
115
throw new SharpZipBaseException();
117
buf[end++] = (byte) s;
118
buf[end++] = (byte) (s >> 8);
122
/// write an integer LSB first
124
/// <param name="s">value to write</param>
125
public void WriteInt(int s)
127
if (DeflaterConstants.DEBUGGING && start != 0) {
128
throw new SharpZipBaseException();
130
buf[end++] = (byte) s;
131
buf[end++] = (byte) (s >> 8);
132
buf[end++] = (byte) (s >> 16);
133
buf[end++] = (byte) (s >> 24);
137
/// Write a block of data to buffer
139
/// <param name="block">data to write</param>
140
/// <param name="offset">offset of first byte to write</param>
141
/// <param name="len">number of bytes to write</param>
142
public void WriteBlock(byte[] block, int offset, int len)
144
if (DeflaterConstants.DEBUGGING && start != 0) {
145
throw new SharpZipBaseException();
147
System.Array.Copy(block, offset, buf, end, len);
152
/// The number of bits written to the buffer
154
public int BitCount {
161
/// Align internal buffer on a byte boundary
163
public void AlignToByte()
165
if (DeflaterConstants.DEBUGGING && start != 0) {
166
throw new SharpZipBaseException();
169
buf[end++] = (byte) bits;
171
buf[end++] = (byte) (bits >> 8);
179
/// Write bits to internal buffer
181
/// <param name="b">source of bits</param>
182
/// <param name="count">number of bits to write</param>
183
public void WriteBits(int b, int count)
185
if (DeflaterConstants.DEBUGGING && start != 0) {
186
throw new SharpZipBaseException();
188
// if (DeflaterConstants.DEBUGGING) {
189
// //Console.WriteLine("writeBits("+b+","+count+")");
191
bits |= (uint)(b << bitCount);
193
if (bitCount >= 16) {
194
buf[end++] = (byte) bits;
195
buf[end++] = (byte) (bits >> 8);
202
/// Write a short value to internal buffer most significant byte first
204
/// <param name="s">value to write</param>
205
public void WriteShortMSB(int s)
207
if (DeflaterConstants.DEBUGGING && start != 0) {
208
throw new SharpZipBaseException();
210
buf[end++] = (byte) (s >> 8);
211
buf[end++] = (byte) s;
215
/// Indicates if buffer has been flushed
217
public bool IsFlushed {
224
/// Flushes the pending buffer into the given output array. If the
225
/// output array is to small, only a partial flush is done.
227
/// <param name="output">
228
/// the output array;
230
/// <param name="offset">
231
/// the offset into output array;
233
/// <param name="length">
234
/// length the maximum number of bytes to store;
236
/// <exception name="ArgumentOutOfRangeException">
237
/// IndexOutOfBoundsException if offset or length are invalid.
239
public int Flush(byte[] output, int offset, int length)
242
buf[end++] = (byte) bits;
246
if (length > end - start) {
247
length = end - start;
248
System.Array.Copy(buf, start, output, offset, length);
252
System.Array.Copy(buf, start, output, offset, length);
259
/// Convert internal buffer to byte array.
260
/// Buffer is empty on completion
263
/// converted buffer contents contents
265
public byte[] ToByteArray()
267
byte[] ret = new byte[end - start];
268
System.Array.Copy(buf, start, ret, 0, ret.Length);