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 Plupload.PngEncoder {
45
/// This class is general purpose class for writing data to a buffer.
47
/// It allows you to write bits as well as bytes
48
/// Based on DeflaterPending.java
50
/// author of the original java version : Jochen Hoenicke
52
public class PendingBuffer {
53
#region Instance Fields
55
/// Internal work buffer
68
/// construct instance using default buffer size of 4096
70
public PendingBuffer()
75
/// construct instance using specified buffer size
77
/// <param name="bufferSize">
78
/// size to use for internal buffer
80
public PendingBuffer(int bufferSize) {
81
buffer_ = new byte[bufferSize];
87
/// Clear internal state/buffers
90
start = end = bitCount = 0;
94
/// Write a byte to buffer
96
/// <param name="value">
97
/// The value to write
99
public void WriteByte(int value) {
101
if (DeflaterConstants.DEBUGGING && (start != 0) )
103
throw new SharpZipBaseException("Debug check: start != 0");
106
buffer_[end++] = unchecked((byte) value);
110
/// Write a short value to buffer LSB first
112
/// <param name="value">
113
/// The value to write.
115
public void WriteShort(int value) {
117
if (DeflaterConstants.DEBUGGING && (start != 0) )
119
throw new SharpZipBaseException("Debug check: start != 0");
122
buffer_[end++] = unchecked((byte) value);
123
buffer_[end++] = unchecked((byte) (value >> 8));
127
/// write an integer LSB first
129
/// <param name="value">The value to write.</param>
130
public void WriteInt(int value) {
132
if (DeflaterConstants.DEBUGGING && (start != 0) )
134
throw new SharpZipBaseException("Debug check: start != 0");
137
buffer_[end++] = unchecked((byte) value);
138
buffer_[end++] = unchecked((byte) (value >> 8));
139
buffer_[end++] = unchecked((byte) (value >> 16));
140
buffer_[end++] = unchecked((byte) (value >> 24));
144
/// Write a block of data to buffer
146
/// <param name="block">data to write</param>
147
/// <param name="offset">offset of first byte to write</param>
148
/// <param name="length">number of bytes to write</param>
149
public void WriteBlock(byte[] block, int offset, int length) {
151
if (DeflaterConstants.DEBUGGING && (start != 0) )
153
throw new SharpZipBaseException("Debug check: start != 0");
156
System.Array.Copy(block, offset, buffer_, end, length);
161
/// The number of bits written to the buffer
163
public int BitCount {
170
/// Align internal buffer on a byte boundary
172
public void AlignToByte() {
174
if (DeflaterConstants.DEBUGGING && (start != 0) )
176
throw new SharpZipBaseException("Debug check: start != 0");
180
buffer_[end++] = unchecked((byte) bits);
182
buffer_[end++] = unchecked((byte) (bits >> 8));
190
/// Write bits to internal buffer
192
/// <param name="b">source of bits</param>
193
/// <param name="count">number of bits to write</param>
194
public void WriteBits(int b, int count) {
196
if (DeflaterConstants.DEBUGGING && (start != 0) )
198
throw new SharpZipBaseException("Debug check: start != 0");
201
// if (DeflaterConstants.DEBUGGING) {
202
// //Console.WriteLine("writeBits("+b+","+count+")");
205
bits |= (uint) (b << bitCount);
207
if (bitCount >= 16) {
208
buffer_[end++] = unchecked((byte) bits);
209
buffer_[end++] = unchecked((byte) (bits >> 8));
216
/// Write a short value to internal buffer most significant byte first
218
/// <param name="s">value to write</param>
219
public void WriteShortMSB(int s) {
221
if (DeflaterConstants.DEBUGGING && (start != 0) )
223
throw new SharpZipBaseException("Debug check: start != 0");
226
buffer_[end++] = unchecked((byte) (s >> 8));
227
buffer_[end++] = unchecked((byte) s);
231
/// Indicates if buffer has been flushed
233
public bool IsFlushed {
240
/// Flushes the pending buffer into the given output array. If the
241
/// output array is to small, only a partial flush is done.
243
/// <param name="output">The output array.</param>
244
/// <param name="offset">The offset into output array.</param>
245
/// <param name="length">The maximum number of bytes to store.</param>
246
/// <returns>The number of bytes flushed.</returns>
247
public int Flush(byte[] output, int offset, int length) {
249
buffer_[end++] = unchecked((byte) bits);
254
if (length > end - start) {
255
length = end - start;
256
System.Array.Copy(buffer_, start, output, offset, length);
260
System.Array.Copy(buffer_, start, output, offset, length);
267
/// Convert internal buffer to byte array.
268
/// Buffer is empty on completion
271
/// The internal buffer contents converted to a byte array.
273
public byte[] ToByteArray() {
274
byte[] result = new byte[end - start];
275
System.Array.Copy(buffer_, start, result, 0, result.Length);