Mes documents/Visual Studio 2005/Projects/TES4ModTranslator/TES4ModTranslator/Zip/Compression/PendingBuffer.cs

Go to the documentation of this file.
00001 // PendingBuffer.cs
00002 //
00003 // Copyright (C) 2001 Mike Krueger
00004 // Copyright (C) 2004 John Reilly
00005 //
00006 // This file was translated from java, it was part of the GNU Classpath
00007 // Copyright (C) 2001 Free Software Foundation, Inc.
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 //
00023 // Linking this library statically or dynamically with other modules is
00024 // making a combined work based on this library.  Thus, the terms and
00025 // conditions of the GNU General Public License cover the whole
00026 // combination.
00027 // 
00028 // As a special exception, the copyright holders of this library give you
00029 // permission to link this library with independent modules to produce an
00030 // executable, regardless of the license terms of these independent
00031 // modules, and to copy and distribute the resulting executable under
00032 // terms of your choice, provided that you also meet, for each linked
00033 // independent module, the terms and conditions of the license of that
00034 // module.  An independent module is a module which is not derived from
00035 // or based on this library.  If you modify this library, you may extend
00036 // this exception to your version of the library, but you are not
00037 // obligated to do so.  If you do not wish to do so, delete this
00038 // exception statement from your version.
00039 
00040 using System;
00041 
00042 namespace ICSharpCode.SharpZipLib.Zip.Compression 
00043 {
00044         
00053         public class PendingBuffer
00054         {
00057                 protected byte[] buf;
00058                 
00059                 int    start;
00060                 int    end;
00061                 
00062                 uint    bits;
00063                 int    bitCount;
00064 
00068                 public PendingBuffer() : this( 4096 )
00069                 {
00070                         
00071                 }
00072                 
00079                 public PendingBuffer(int bufsize)
00080                 {
00081                         buf = new byte[bufsize];
00082                 }
00083 
00087                 public void Reset() 
00088                 {
00089                         start = end = bitCount = 0;
00090                 }
00091 
00098                 public void WriteByte(int b)
00099                 {
00100                         if (DeflaterConstants.DEBUGGING && start != 0) {
00101                                 throw new SharpZipBaseException();
00102                         }
00103                         buf[end++] = (byte) b;
00104                 }
00105 
00112                 public void WriteShort(int s)
00113                 {
00114                         if (DeflaterConstants.DEBUGGING && start != 0) {
00115                                 throw new SharpZipBaseException();
00116                         }
00117                         buf[end++] = (byte) s;
00118                         buf[end++] = (byte) (s >> 8);
00119                 }
00120 
00125                 public void WriteInt(int s)
00126                 {
00127                         if (DeflaterConstants.DEBUGGING && start != 0) {
00128                                 throw new SharpZipBaseException();
00129                         }
00130                         buf[end++] = (byte) s;
00131                         buf[end++] = (byte) (s >> 8);
00132                         buf[end++] = (byte) (s >> 16);
00133                         buf[end++] = (byte) (s >> 24);
00134                 }
00135                 
00142                 public void WriteBlock(byte[] block, int offset, int len)
00143                 {
00144                         if (DeflaterConstants.DEBUGGING && start != 0) {
00145                                 throw new SharpZipBaseException();
00146                         }
00147                         System.Array.Copy(block, offset, buf, end, len);
00148                         end += len;
00149                 }
00150 
00154                 public int BitCount {
00155                         get {
00156                                 return bitCount;
00157                         }
00158                 }
00159                 
00163                 public void AlignToByte() 
00164                 {
00165                         if (DeflaterConstants.DEBUGGING && start != 0) {
00166                                 throw new SharpZipBaseException();
00167                         }
00168                         if (bitCount > 0) {
00169                                 buf[end++] = (byte) bits;
00170                                 if (bitCount > 8) {
00171                                         buf[end++] = (byte) (bits >> 8);
00172                                 }
00173                         }
00174                         bits = 0;
00175                         bitCount = 0;
00176                 }
00177 
00183                 public void WriteBits(int b, int count)
00184                 {
00185                         if (DeflaterConstants.DEBUGGING && start != 0) {
00186                                 throw new SharpZipBaseException();
00187                         }
00188                         //                      if (DeflaterConstants.DEBUGGING) {
00189                         //                              //Console.WriteLine("writeBits("+b+","+count+")");
00190                         //                      }
00191                         bits |= (uint)(b << bitCount);
00192                         bitCount += count;
00193                         if (bitCount >= 16) {
00194                                 buf[end++] = (byte) bits;
00195                                 buf[end++] = (byte) (bits >> 8);
00196                                 bits >>= 16;
00197                                 bitCount -= 16;
00198                         }
00199                 }
00200 
00205                 public void WriteShortMSB(int s) 
00206                 {
00207                         if (DeflaterConstants.DEBUGGING && start != 0) {
00208                                 throw new SharpZipBaseException();
00209                         }
00210                         buf[end++] = (byte) (s >> 8);
00211                         buf[end++] = (byte) s;
00212                 }
00213                 
00217                 public bool IsFlushed {
00218                         get {
00219                                 return end == 0;
00220                         }
00221                 }
00222                 
00239                 public int Flush(byte[] output, int offset, int length) 
00240                 {
00241                         if (bitCount >= 8) {
00242                                 buf[end++] = (byte) bits;
00243                                 bits >>= 8;
00244                                 bitCount -= 8;
00245                         }
00246                         if (length > end - start) {
00247                                 length = end - start;
00248                                 System.Array.Copy(buf, start, output, offset, length);
00249                                 start = 0;
00250                                 end = 0;
00251                         } else {
00252                                 System.Array.Copy(buf, start, output, offset, length);
00253                                 start += length;
00254                         }
00255                         return length;
00256                 }
00257 
00265                 public byte[] ToByteArray()
00266                 {
00267                         byte[] ret = new byte[end - start];
00268                         System.Array.Copy(buf, start, ret, 0, ret.Length);
00269                         start = 0;
00270                         end = 0;
00271                         return ret;
00272                 }
00273         }
00274 }       

Generated on Fri Jun 23 21:50:05 2006 for OblivionModTranslator by  doxygen 1.4.6-NO