您目前尚未登陆,请选择【登陆】或【注册
首页->博客论坛->X3BLOG 单用户1.0 build80707(ACCESS)源代码>>SharpZipLib/BZip2/BZip2OutputStream.cs>>代码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:X3BLOG 单用户1.0 build80707(ACCESS)源代码


当前文件路径:x3blogAccessBuild80707/SharpZipLib/BZip2/BZip2OutputStream.cs 文件类型
普通视图
		            
1// BZip2OutputStream.cs 2// Copyright (C) 2001 Mike Krueger 3// 4// This program is free software; you can redistribute it and/or 5// modify it under the terms of the GNU General Public License 6// as published by the Free Software Foundation; either version 2 7// of the License, or (at your option) any later version. 8// 9// This program is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU General Public License for more details. 13// 14// You should have received a copy of the GNU General Public License 15// along with this program; if not, write to the Free Software 16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17// 18// Linking this library statically or dynamically with other modules is 19// making a combined work based on this library. Thus, the terms and 20// conditions of the GNU General Public License cover the whole 21// combination. 22// 23// As a special exception, the copyright holders of this library give you 24// permission to link this library with independent modules to produce an 25// executable, regardless of the license terms of these independent 26// modules, and to copy and distribute the resulting executable under 27// terms of your choice, provided that you also meet, for each linked 28// independent module, the terms and conditions of the license of that 29// module. An independent module is a module which is not derived from 30// or based on this library. If you modify this library, you may extend 31// this exception to your version of the library, but you are not 32// obligated to do so. If you do not wish to do so, delete this 33// exception statement from your version. 34 35using System; 36using System.IO; 37 38using ICSharpCode.SharpZipLib.Checksums; 39 40namespace ICSharpCode.SharpZipLib.BZip2 41{ 42 43 // TODO: Update to BZip2 1.0.1, 1.0.2 44 45 /// <summary> 46 /// An output stream that compresses into the BZip2 format 47 /// including file header chars into another stream. 48 /// </summary> 49 public class BZip2OutputStream : Stream 50 { 51 /// <summary> 52 /// Gets a value indicating whether the current stream supports reading 53 /// </summary> 54 public override bool CanRead { 55 get { 56 return false; 57 } 58 } 59 60 /// <summary> 61 /// Gets a value indicating whether the current stream supports seeking 62 /// </summary> 63 public override bool CanSeek { 64 get { 65 return false; 66 } 67 } 68 69 /// <summary> 70 /// Gets a value indicating whether the current stream supports writing 71 /// </summary> 72 public override bool CanWrite { 73 get { 74 return baseStream.CanWrite; 75 } 76 } 77 78 /// <summary> 79 /// Gets the length in bytes of the stream 80 /// </summary> 81 public override long Length { 82 get { 83 return baseStream.Length; 84 } 85 } 86 87 /// <summary> 88 /// Gets or sets the current position of this stream. 89 /// </summary> 90 public override long Position { 91 get { 92 return baseStream.Position; 93 } 94 set { 95 throw new NotSupportedException("BZip2OutputStream position cannot be set"); 96 } 97 } 98 99 /// <summary> 100 /// Sets the current position of this stream to the given value. 101 /// </summary> 102 public override long Seek(long offset, SeekOrigin origin) 103 { 104 throw new NotSupportedException("BZip2OutputStream Seek not supported"); 105 } 106 107 /// <summary> 108 /// Sets the length of this stream to the given value. 109 /// </summary> 110 public override void SetLength(long val) 111 { 112 throw new NotSupportedException("BZip2OutputStream SetLength not supported"); 113 } 114 115 /// <summary> 116 /// Read a byte from the stream advancing the position. 117 /// </summary> 118 public override int ReadByte() 119 { 120 throw new NotSupportedException("BZip2OutputStream ReadByte not supported"); 121 } 122 123 /// <summary> 124 /// Read a block of bytes 125 /// </summary> 126 public override int Read(byte[] b, int off, int len) 127 { 128 throw new NotSupportedException("BZip2OutputStream Read not supported"); 129 } 130 131 /// <summary> 132 /// Write a block of bytes to the stream 133 /// </summary> 134 public override void Write(byte[] buf, int off, int len) 135 { 136 for (int i = 0; i < len; ++i) { 137 WriteByte(buf[off + i]); 138 } 139 } 140 141 readonly static int SETMASK = (1 << 21); 142 readonly static int CLEARMASK = (~SETMASK); 143 readonly static int GREATER_ICOST = 15; 144 readonly static int LESSER_ICOST = 0; 145 readonly static int SMALL_THRESH = 20; 146 readonly static int DEPTH_THRESH = 10; 147 148 /*-- 149 If you are ever unlucky/improbable enough 150 to get a stack overflow whilst sorting, 151 increase the following constant and try 152 again. In practice I have never seen the 153 stack go above 27 elems, so the following 154 limit seems very generous. 155 --*/ 156 readonly static int QSORT_STACK_SIZE = 1000; 157 158 static void Panic() 159 { 160 throw new BZip2Exception("BZip2 output stream panic"); 161 } 162 163 void MakeMaps() 164 { 165 int i; 166 nInUse = 0; 167 for (i = 0; i < 256; i++) { 168 if (inUse[i]) { 169 seqToUnseq[nInUse] = (char)i; 170 unseqToSeq[i] = (char)nInUse; 171 nInUse++; 172 } 173 } 174 } 175 176 static void HbMakeCodeLengths(char[] len, int[] freq, int alphaSize, int maxLen) 177 { 178 /*-- 179 Nodes and heap entries run from 1. Entry 0 180 for both the heap and nodes is a sentinel. 181 --*/ 182 int nNodes, nHeap, n1, n2, j, k; 183 bool tooLong; 184 185 int[] heap = new int[BZip2Constants.MAX_ALPHA_SIZE + 2]; 186 int[] weight = new int[BZip2Constants.MAX_ALPHA_SIZE * 2]; 187 int[] parent = new int[BZip2Constants.MAX_ALPHA_SIZE * 2]; 188 189 for (int i = 0; i < alphaSize; ++i) { 190 weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8; 191 } 192 193 while (true) { 194 nNodes = alphaSize; 195 nHeap = 0; 196 197 heap[0] = 0; 198 weight[0] = 0; 199 parent[0] = -2; 200 201 for (int i = 1; i <= alphaSize; ++i) { 202 parent[i] = -1; 203 nHeap++; 204 heap[nHeap] = i; 205 int zz = nHeap; 206 int tmp = heap[zz]; 207 while (weight[tmp] < weight[heap[zz >> 1]]) { 208 heap[zz] = heap[zz >> 1]; 209 zz >>= 1; 210 } 211 heap[zz] = tmp; 212 } 213 if (!(nHeap < (BZip2Constants.MAX_ALPHA_SIZE+2))) { 214 Panic(); 215 } 216 217 while (nHeap > 1) { 218 n1 = heap[1]; 219 heap[1] = heap[nHeap]; 220 nHeap--; 221 int zz = 1; 222 int yy = 0; 223 int tmp = heap[zz]; 224 while (true) { 225 yy = zz << 1; 226 if (yy > nHeap) { 227 break; 228 } 229 if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) { 230 yy++; 231 } 232 if (weight[tmp] < weight[heap[yy]]) { 233 break; 234 } 235 236 heap[zz] = heap[yy]; 237 zz = yy; 238 } 239 heap[zz] = tmp; 240 n2 = heap[1]; 241 heap[1] = heap[nHeap]; 242 nHeap--; 243 244 zz = 1; 245 yy = 0; 246 tmp = heap[zz]; 247 while (true) { 248 yy = zz << 1; 249 if (yy > nHeap) { 250 break; 251 } 252 if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) { 253 yy++; 254 } 255 if (weight[tmp] < weight[heap[yy]]) { 256 break; 257 } 258 heap[zz] = heap[yy]; 259 zz = yy; 260 } 261 heap[zz] = tmp; 262 nNodes++; 263 parent[n1] = parent[n2] = nNodes; 264 265 weight[nNodes] =