温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:X3BLOG 单用户1.0 build80707(ACCESS)源代码
当前文件路径:x3blogAccessBuild80707/SharpZipLib/BZip2/BZip2InputStream.cs

1// BZip2InputStream.cs 2
// 3
// Copyright (C) 2001 Mike Krueger 4
// 5
// This program is free software; you can redistribute it and/or 6
// modify it under the terms of the GNU General Public License 7
// as published by the Free Software Foundation; either version 2 8
// of the License, or (at your option) any later version. 9
// 10
// This program is distributed in the hope that it will be useful, 11
// but WITHOUT ANY WARRANTY; without even the implied warranty of 12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13
// GNU General Public License for more details. 14
// 15
// You should have received a copy of the GNU General Public License 16
// along with this program; if not, write to the Free Software 17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18
// 19
// Linking this library statically or dynamically with other modules is 20
// making a combined work based on this library. Thus, the terms and 21
// conditions of the GNU General Public License cover the whole 22
// combination. 23
// 24
// As a special exception, the copyright holders of this library give you 25
// permission to link this library with independent modules to produce an 26
// executable, regardless of the license terms of these independent 27
// modules, and to copy and distribute the resulting executable under 28
// terms of your choice, provided that you also meet, for each linked 29
// independent module, the terms and conditions of the license of that 30
// module. An independent module is a module which is not derived from 31
// or based on this library. If you modify this library, you may extend 32
// this exception to your version of the library, but you are not 33
// obligated to do so. If you do not wish to do so, delete this 34
// exception statement from your version. 35
36
using System; 37
using System.IO; 38
39
using ICSharpCode.SharpZipLib.Checksums; 40
41
namespace ICSharpCode.SharpZipLib.BZip2 42
{ 43
44
/// <summary> 45
/// An input stream that decompresses files in the BZip2 format 46
/// </summary> 47
public class BZip2InputStream : Stream 48
{ 49
/// <summary> 50
/// Gets a value indicating if the stream supports reading 51
/// </summary> 52
public override bool CanRead { 53
get { 54
return baseStream.CanRead; 55
} 56
} 57
58
/// <summary> 59
/// Gets a value indicating whether the current stream supports seeking. 60
/// </summary> 61
public override bool CanSeek { 62
get { 63
return baseStream.CanSeek; 64
} 65
} 66
67
/// <summary> 68
/// Gets a value indicating whether the current stream supports writing. 69
/// This property always returns false 70
/// </summary> 71
public override bool CanWrite { 72
get { 73
return false; 74
} 75
} 76
77
/// <summary> 78
/// Gets the length in bytes of the stream. 79
/// </summary> 80
public override long Length { 81
get { 82
return baseStream.Length; 83
} 84
} 85
86
/// <summary> 87
/// Gets or sets the streams position. 88
/// Setting the position is not supported and will throw a NotSupportException 89
/// </summary> 90
/// <exception cref="NotSupportedException">Any attempt to set the position</exception> 91
public override long Position { 92
get { 93
return baseStream.Position; 94
} 95
set { 96
throw new NotSupportedException("BZip2InputStream position cannot be set"); 97
} 98
} 99
100
/// <summary> 101
/// Flushes the stream. 102
/// </summary> 103
public override void Flush() 104
{ 105
if (baseStream != null) { 106
baseStream.Flush(); 107
} 108
} 109
110
/// <summary> 111
/// Set the streams position. This operation is not supported and will throw a NotSupportedException 112
/// </summary> 113
/// <exception cref="NotSupportedException">Any access</exception> 114
public override long Seek(long offset, SeekOrigin origin) 115
{ 116
throw new NotSupportedException("BZip2InputStream Seek not supported"); 117
} 118
119
/// <summary> 120
/// Sets the length of this stream to the given value. 121
/// This operation is not supported and will throw a NotSupportedExceptionortedException 122
/// </summary> 123
/// <exception cref="NotSupportedException">Any access</exception> 124
public override void SetLength(long val) 125
{ 126
throw new NotSupportedException("BZip2InputStream SetLength not supported"); 127
} 128
129
/// <summary> 130
/// Writes a block of bytes to this stream using data from a buffer. 131
/// This operation is not supported and will throw a NotSupportedException 132
/// </summary> 133
/// <exception cref="NotSupportedException">Any access</exception> 134
public override void Write(byte[] array, int offset, int count) 135
{ 136
throw new NotSupportedException("BZip2InputStream Write not supported"); 137
} 138
139
/// <summary> 140
/// Writes a byte to the current position in the file stream. 141
/// This operation is not supported and will throw a NotSupportedException 142
/// </summary> 143
/// <exception cref="NotSupportedException">Any access</exception> 144
public override void WriteByte(byte val) 145
{ 146
throw new NotSupportedException("BZip2InputStream WriteByte not supported"); 147
} 148
149
/// <summary> 150
/// Read a sequence of bytes and advances the read position by one byte. 151
/// </summary> 152
/// <param name="b">Array of bytes to store values in</param> 153
/// <param name="offset">Offset in array to begin storing data</param> 154
/// <param name="count">The maximum number of bytes to read</param> 155
/// <returns>The total number of bytes read into the buffer. This might be less 156
/// than the number of bytes requested if that number of bytes are not 157
/// currently available or zero if the end of the stream is reached. 158
/// </returns> 159
public override int Read(byte[] b, int offset, int count) 160
{ 161
for (int i = 0; i < count; ++i) { 162
int rb = ReadByte(); 163
if (rb == -1) { 164
return i; 165
} 166
b[offset + i] = (byte)rb; 167
} 168
return count; 169
} 170
171
/// <summary> 172
/// Closes the stream, releasing any associated resources. 173
/// </summary> 174
public override void Close() 175
{ 176
if (baseStream != null) { 177
baseStream.Close(); 178
} 179
} 180
181
void MakeMaps() 182
{ 183
nInUse = 0; 184
for (int i = 0; i < 256; ++i) { 185
if (inUse[i]) { 186
seqToUnseq[nInUse] = (byte)i; 187
unseqToSeq[i] = (byte)nInUse; 188
nInUse++; 189
} 190
} 191
} 192
193
/*-- 194
index of the last char in the block, so 195
the block size == last + 1. 196
--*/ 197
int last; 198
199
/*-- 200
index in zptr[] of original string after sorting. 201
--*/ 202
int origPtr; 203
204
/*-- 205
always: in the range 0 .. 9. 206
The current block size is 100000 * this number. 207
--*/ 208
int blockSize100k; 209
210
bool blockRandomised; 211
212
int bsBuff; 213
int bsLive; 214
IChecksum mCrc = new StrangeCRC(); 215
216
bool[] inUse = new bool[256]; 217
int nInUse; 218
219
byte[] seqToUnseq = new byte[256]; 220
byte[] unseqToSeq = new byte[256]; 221
222
byte[] selector = new byte[BZip2Constants.MAX_SELECTORS]; 223
byte[] selectorMtf = new byte[BZip2Constants.MAX_SELECTORS]; 224
225
int[] tt; 226
byte[] ll8; 227
228
/*-- 229
freq table collected to save a pass over the data 230
during decompression. 231
--*/ 232
int[] unzftab = new int[256]; 233
234
int[][] limit = new int[BZip2Constants.N_GROUPS][]; 235
int[][] baseArray = new int[BZip2Constants.N_GROUPS][]; 236
int[][] perm = new int[BZip2Constants.N_GROUPS][]; 237
int[] minLens = new int[BZip2Constants.N_GROUPS]; 238
239
Stream baseStream; 240
bool streamEnd = false; 241
242
int currentChar = -1; 243
244
const int START_BLOCK_STATE = 1; 245
const int RAND_PART_A_STATE = 2; 246
const int RAND_PART_B_STATE = 3; 247
const int RAND_PART_C_STATE = 4; 248
const int NO_RAND_PART_A_STATE = 5; 249
const int NO_RAND_PART_B_STATE = 6; 250
const int NO_RAND_PART_C_STATE = 7; 251
252
int currentState = START_BLOCK_STATE; 253
254
int storedBlockCRC, storedCombinedCRC; 255
int computedBlockCRC; 256
uint computedCombinedCRC; 257
258
int count, chPrev, ch2; 259
int tPos; 260
int rNToGo = 0; 261
int rTPos = 0; 262
int i2, j2; 263
byte z; 264
265
/// <summary> 266
/// Construct instance for reading from stream 267
/// </summary> 268
/// <param name="stream">Data source</param> 269
public BZip2InputStream(Stream stream) 270
{ 271
// init arrays 272
for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) { 273
limit[i] = new int[BZip2Constants.MAX_ALPHA_SIZE]; 274
baseArray[i] = new int[BZip2Constants.MAX_ALPHA_SIZE]; 275
perm[i] = new int[BZip2Constants.MAX_ALPHA_SIZE]; 276
} 277
278
ll8 = null; 279
tt = null; 280
BsSetStream(stream); 281
Initialize(); 282
InitBlock(); 283
SetupBlock(); 284
} 285
286




