Import TechBot
[reactos.git] / irc / TechBot / Compression / PendingBuffer.cs
1 // PendingBuffer.cs
2 // Copyright (C) 2001 Mike Krueger
3 //
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
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.
25 //
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.
37
38 using System;
39
40 namespace ICSharpCode.SharpZipLib.Zip.Compression
41 {
42
43 /// <summary>
44 /// This class is general purpose class for writing data to a buffer.
45 ///
46 /// It allows you to write bits as well as bytes
47 /// Based on DeflaterPending.java
48 ///
49 /// author of the original java version : Jochen Hoenicke
50 /// </summary>
51 public class PendingBuffer
52 {
53 protected byte[] buf;
54 int start;
55 int end;
56
57 uint bits;
58 int bitCount;
59
60 public PendingBuffer() : this( 4096 )
61 {
62
63 }
64
65 public PendingBuffer(int bufsize)
66 {
67 buf = new byte[bufsize];
68 }
69
70 public void Reset()
71 {
72 start = end = bitCount = 0;
73 }
74
75 public void WriteByte(int b)
76 {
77 if (DeflaterConstants.DEBUGGING && start != 0) {
78 throw new Exception();
79 }
80 buf[end++] = (byte) b;
81 }
82
83 public void WriteShort(int s)
84 {
85 if (DeflaterConstants.DEBUGGING && start != 0) {
86 throw new Exception();
87 }
88 buf[end++] = (byte) s;
89 buf[end++] = (byte) (s >> 8);
90 }
91
92 public void WriteInt(int s)
93 {
94 if (DeflaterConstants.DEBUGGING && start != 0) {
95 throw new Exception();
96 }
97 buf[end++] = (byte) s;
98 buf[end++] = (byte) (s >> 8);
99 buf[end++] = (byte) (s >> 16);
100 buf[end++] = (byte) (s >> 24);
101 }
102
103 public void WriteBlock(byte[] block, int offset, int len)
104 {
105 if (DeflaterConstants.DEBUGGING && start != 0) {
106 throw new Exception();
107 }
108 System.Array.Copy(block, offset, buf, end, len);
109 end += len;
110 }
111
112 public int BitCount {
113 get {
114 return bitCount;
115 }
116 }
117
118 public void AlignToByte()
119 {
120 if (DeflaterConstants.DEBUGGING && start != 0) {
121 throw new Exception();
122 }
123 if (bitCount > 0) {
124 buf[end++] = (byte) bits;
125 if (bitCount > 8) {
126 buf[end++] = (byte) (bits >> 8);
127 }
128 }
129 bits = 0;
130 bitCount = 0;
131 }
132
133 public void WriteBits(int b, int count)
134 {
135 if (DeflaterConstants.DEBUGGING && start != 0) {
136 throw new Exception();
137 }
138 // if (DeflaterConstants.DEBUGGING) {
139 // //Console.WriteLine("writeBits("+b+","+count+")");
140 // }
141 bits |= (uint)(b << bitCount);
142 bitCount += count;
143 if (bitCount >= 16) {
144 buf[end++] = (byte) bits;
145 buf[end++] = (byte) (bits >> 8);
146 bits >>= 16;
147 bitCount -= 16;
148 }
149 }
150
151 public void WriteShortMSB(int s)
152 {
153 if (DeflaterConstants.DEBUGGING && start != 0) {
154 throw new Exception();
155 }
156 buf[end++] = (byte) (s >> 8);
157 buf[end++] = (byte) s;
158 }
159
160 public bool IsFlushed {
161 get {
162 return end == 0;
163 }
164 }
165
166 /// <summary>
167 /// Flushes the pending buffer into the given output array. If the
168 /// output array is to small, only a partial flush is done.
169 /// </summary>
170 /// <param name="output">
171 /// the output array;
172 /// </param>
173 /// <param name="offset">
174 /// the offset into output array;
175 /// </param>
176 /// <param name="length">
177 /// length the maximum number of bytes to store;
178 /// </param>
179 /// <exception name="ArgumentOutOfRangeException">
180 /// IndexOutOfBoundsException if offset or length are invalid.
181 /// </exception>
182 public int Flush(byte[] output, int offset, int length)
183 {
184 if (bitCount >= 8) {
185 buf[end++] = (byte) bits;
186 bits >>= 8;
187 bitCount -= 8;
188 }
189 if (length > end - start) {
190 length = end - start;
191 System.Array.Copy(buf, start, output, offset, length);
192 start = 0;
193 end = 0;
194 } else {
195 System.Array.Copy(buf, start, output, offset, length);
196 start += length;
197 }
198 return length;
199 }
200
201 public byte[] ToByteArray()
202 {
203 byte[] ret = new byte[end - start];
204 System.Array.Copy(buf, start, ret, 0, ret.Length);
205 start = 0;
206 end = 0;
207 return ret;
208 }
209 }
210 }