~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.SharpZipLib/Zip/Compression/PendingBuffer.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// PendingBuffer.cs
 
2
//
 
3
// Copyright (C) 2001 Mike Krueger
 
4
// Copyright (C) 2004 John Reilly
 
5
//
 
6
// This file was translated from java, it was part of the GNU Classpath
 
7
// Copyright (C) 2001 Free Software Foundation, Inc.
 
8
//
 
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.
 
13
//
 
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.
 
18
//
 
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.
 
22
//
 
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
 
26
// combination.
 
27
// 
 
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.
 
39
 
 
40
using System;
 
41
 
 
42
namespace PdfSharp.SharpZipLib.Zip.Compression 
 
43
{
 
44
        
 
45
        /// <summary>
 
46
        /// This class is general purpose class for writing data to a buffer.
 
47
        /// 
 
48
        /// It allows you to write bits as well as bytes
 
49
        /// Based on DeflaterPending.java
 
50
        /// 
 
51
        /// author of the original java version : Jochen Hoenicke
 
52
        /// </summary>
 
53
        internal class PendingBuffer
 
54
        {
 
55
                /// <summary>Internal work buffer
 
56
                /// </summary>
 
57
                protected byte[] buf;
 
58
                
 
59
                int    start;
 
60
                int    end;
 
61
                
 
62
                uint    bits;
 
63
                int    bitCount;
 
64
 
 
65
                /// <summary>
 
66
                /// construct instance using default buffer size of 4096
 
67
                /// </summary>
 
68
                public PendingBuffer() : this( 4096 )
 
69
                {
 
70
                        
 
71
                }
 
72
                
 
73
                /// <summary>
 
74
                /// construct instance using specified buffer size
 
75
                /// </summary>
 
76
                /// <param name="bufsize">
 
77
                /// size to use for internal buffer
 
78
                /// </param>
 
79
                public PendingBuffer(int bufsize)
 
80
                {
 
81
                        buf = new byte[bufsize];
 
82
                }
 
83
 
 
84
                /// <summary>
 
85
                /// Clear internal state/buffers
 
86
                /// </summary>
 
87
                public void Reset() 
 
88
                {
 
89
                        start = end = bitCount = 0;
 
90
                }
 
91
 
 
92
                /// <summary>
 
93
                /// write a byte to buffer
 
94
                /// </summary>
 
95
                /// <param name="b">
 
96
                /// value to write
 
97
                /// </param>
 
98
                public void WriteByte(int b)
 
99
                {
 
100
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
101
                                throw new SharpZipBaseException();
 
102
                        }
 
103
                        buf[end++] = (byte) b;
 
104
                }
 
105
 
 
106
                /// <summary>
 
107
                /// Write a short value to buffer LSB first
 
108
                /// </summary>
 
109
                /// <param name="s">
 
110
                /// value to write
 
111
                /// </param>
 
112
                public void WriteShort(int s)
 
113
                {
 
114
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
115
                                throw new SharpZipBaseException();
 
116
                        }
 
117
                        buf[end++] = (byte) s;
 
118
                        buf[end++] = (byte) (s >> 8);
 
119
                }
 
120
 
 
121
                /// <summary>
 
122
                /// write an integer LSB first
 
123
                /// </summary>
 
124
                /// <param name="s">value to write</param>
 
125
                public void WriteInt(int s)
 
126
                {
 
127
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
128
                                throw new SharpZipBaseException();
 
129
                        }
 
130
                        buf[end++] = (byte) s;
 
131
                        buf[end++] = (byte) (s >> 8);
 
132
                        buf[end++] = (byte) (s >> 16);
 
133
                        buf[end++] = (byte) (s >> 24);
 
134
                }
 
135
                
 
136
                /// <summary>
 
137
                /// Write a block of data to buffer
 
138
                /// </summary>
 
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)
 
143
                {
 
144
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
145
                                throw new SharpZipBaseException();
 
146
                        }
 
147
                        System.Array.Copy(block, offset, buf, end, len);
 
148
                        end += len;
 
149
                }
 
150
 
 
151
                /// <summary>
 
152
                /// The number of bits written to the buffer
 
153
                /// </summary>
 
154
                public int BitCount {
 
155
                        get {
 
156
                                return bitCount;
 
157
                        }
 
158
                }
 
159
                
 
160
                /// <summary>
 
161
                /// Align internal buffer on a byte boundary
 
162
                /// </summary>
 
163
                public void AlignToByte() 
 
164
                {
 
165
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
166
                                throw new SharpZipBaseException();
 
167
                        }
 
168
                        if (bitCount > 0) {
 
169
                                buf[end++] = (byte) bits;
 
170
                                if (bitCount > 8) {
 
171
                                        buf[end++] = (byte) (bits >> 8);
 
172
                                }
 
173
                        }
 
174
                        bits = 0;
 
175
                        bitCount = 0;
 
176
                }
 
177
 
 
178
                /// <summary>
 
179
                /// Write bits to internal buffer
 
180
                /// </summary>
 
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)
 
184
                {
 
185
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
186
                                throw new SharpZipBaseException();
 
187
                        }
 
188
                        //                      if (DeflaterConstants.DEBUGGING) {
 
189
                        //                              //Console.WriteLine("writeBits("+b+","+count+")");
 
190
                        //                      }
 
191
                        bits |= (uint)(b << bitCount);
 
192
                        bitCount += count;
 
193
                        if (bitCount >= 16) {
 
194
                                buf[end++] = (byte) bits;
 
195
                                buf[end++] = (byte) (bits >> 8);
 
196
                                bits >>= 16;
 
197
                                bitCount -= 16;
 
198
                        }
 
199
                }
 
200
 
 
201
                /// <summary>
 
202
                /// Write a short value to internal buffer most significant byte first
 
203
                /// </summary>
 
204
                /// <param name="s">value to write</param>
 
205
                public void WriteShortMSB(int s) 
 
206
                {
 
207
                        if (DeflaterConstants.DEBUGGING && start != 0) {
 
208
                                throw new SharpZipBaseException();
 
209
                        }
 
210
                        buf[end++] = (byte) (s >> 8);
 
211
                        buf[end++] = (byte) s;
 
212
                }
 
213
                
 
214
                /// <summary>
 
215
                /// Indicates if buffer has been flushed
 
216
                /// </summary>
 
217
                public bool IsFlushed {
 
218
                        get {
 
219
                                return end == 0;
 
220
                        }
 
221
                }
 
222
                
 
223
                /// <summary>
 
224
                /// Flushes the pending buffer into the given output array.  If the
 
225
                /// output array is to small, only a partial flush is done.
 
226
                /// </summary>
 
227
                /// <param name="output">
 
228
                /// the output array;
 
229
                /// </param>
 
230
                /// <param name="offset">
 
231
                /// the offset into output array;
 
232
                /// </param>
 
233
                /// <param name="length">               
 
234
                /// length the maximum number of bytes to store;
 
235
                /// </param>
 
236
                /// <exception name="ArgumentOutOfRangeException">
 
237
                /// IndexOutOfBoundsException if offset or length are invalid.
 
238
                /// </exception>
 
239
                public int Flush(byte[] output, int offset, int length) 
 
240
                {
 
241
                        if (bitCount >= 8) {
 
242
                                buf[end++] = (byte) bits;
 
243
                                bits >>= 8;
 
244
                                bitCount -= 8;
 
245
                        }
 
246
                        if (length > end - start) {
 
247
                                length = end - start;
 
248
                                System.Array.Copy(buf, start, output, offset, length);
 
249
                                start = 0;
 
250
                                end = 0;
 
251
                        } else {
 
252
                                System.Array.Copy(buf, start, output, offset, length);
 
253
                                start += length;
 
254
                        }
 
255
                        return length;
 
256
                }
 
257
 
 
258
                /// <summary>
 
259
                /// Convert internal buffer to byte array.
 
260
                /// Buffer is empty on completion
 
261
                /// </summary>
 
262
                /// <returns>
 
263
                /// converted buffer contents contents
 
264
                /// </returns>
 
265
                public byte[] ToByteArray()
 
266
                {
 
267
                        byte[] ret = new byte[end - start];
 
268
                        System.Array.Copy(buf, start, ret, 0, ret.Length);
 
269
                        start = 0;
 
270
                        end = 0;
 
271
                        return ret;
 
272
                }
 
273
        }
 
274
}