--- /dev/null
+// PendingBuffer.cs\r
+// Copyright (C) 2001 Mike Krueger\r
+//\r
+// This file was translated from java, it was part of the GNU Classpath\r
+// Copyright (C) 2001 Free Software Foundation, Inc.\r
+//\r
+// This program is free software; you can redistribute it and/or\r
+// modify it under the terms of the GNU General Public License\r
+// as published by the Free Software Foundation; either version 2\r
+// of the License, or (at your option) any later version.\r
+//\r
+// This program is distributed in the hope that it will be useful,\r
+// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+// GNU General Public License for more details.\r
+//\r
+// You should have received a copy of the GNU General Public License\r
+// along with this program; if not, write to the Free Software\r
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+//\r
+// Linking this library statically or dynamically with other modules is\r
+// making a combined work based on this library. Thus, the terms and\r
+// conditions of the GNU General Public License cover the whole\r
+// combination.\r
+// \r
+// As a special exception, the copyright holders of this library give you\r
+// permission to link this library with independent modules to produce an\r
+// executable, regardless of the license terms of these independent\r
+// modules, and to copy and distribute the resulting executable under\r
+// terms of your choice, provided that you also meet, for each linked\r
+// independent module, the terms and conditions of the license of that\r
+// module. An independent module is a module which is not derived from\r
+// or based on this library. If you modify this library, you may extend\r
+// this exception to your version of the library, but you are not\r
+// obligated to do so. If you do not wish to do so, delete this\r
+// exception statement from your version.\r
+\r
+using System;\r
+\r
+namespace ICSharpCode.SharpZipLib.Zip.Compression \r
+{\r
+ \r
+ /// <summary>\r
+ /// This class is general purpose class for writing data to a buffer.\r
+ /// \r
+ /// It allows you to write bits as well as bytes\r
+ /// Based on DeflaterPending.java\r
+ /// \r
+ /// author of the original java version : Jochen Hoenicke\r
+ /// </summary>\r
+ public class PendingBuffer\r
+ {\r
+ protected byte[] buf;\r
+ int start;\r
+ int end;\r
+ \r
+ uint bits;\r
+ int bitCount;\r
+ \r
+ public PendingBuffer() : this( 4096 )\r
+ {\r
+ \r
+ }\r
+ \r
+ public PendingBuffer(int bufsize)\r
+ {\r
+ buf = new byte[bufsize];\r
+ }\r
+ \r
+ public void Reset() \r
+ {\r
+ start = end = bitCount = 0;\r
+ }\r
+ \r
+ public void WriteByte(int b)\r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ buf[end++] = (byte) b;\r
+ }\r
+ \r
+ public void WriteShort(int s)\r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ buf[end++] = (byte) s;\r
+ buf[end++] = (byte) (s >> 8);\r
+ }\r
+ \r
+ public void WriteInt(int s)\r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ buf[end++] = (byte) s;\r
+ buf[end++] = (byte) (s >> 8);\r
+ buf[end++] = (byte) (s >> 16);\r
+ buf[end++] = (byte) (s >> 24);\r
+ }\r
+ \r
+ public void WriteBlock(byte[] block, int offset, int len)\r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ System.Array.Copy(block, offset, buf, end, len);\r
+ end += len;\r
+ }\r
+ \r
+ public int BitCount {\r
+ get {\r
+ return bitCount;\r
+ }\r
+ }\r
+ \r
+ public void AlignToByte() \r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ if (bitCount > 0) {\r
+ buf[end++] = (byte) bits;\r
+ if (bitCount > 8) {\r
+ buf[end++] = (byte) (bits >> 8);\r
+ }\r
+ }\r
+ bits = 0;\r
+ bitCount = 0;\r
+ }\r
+ \r
+ public void WriteBits(int b, int count)\r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ // if (DeflaterConstants.DEBUGGING) {\r
+ // //Console.WriteLine("writeBits("+b+","+count+")");\r
+ // }\r
+ bits |= (uint)(b << bitCount);\r
+ bitCount += count;\r
+ if (bitCount >= 16) {\r
+ buf[end++] = (byte) bits;\r
+ buf[end++] = (byte) (bits >> 8);\r
+ bits >>= 16;\r
+ bitCount -= 16;\r
+ }\r
+ }\r
+ \r
+ public void WriteShortMSB(int s) \r
+ {\r
+ if (DeflaterConstants.DEBUGGING && start != 0) {\r
+ throw new Exception();\r
+ }\r
+ buf[end++] = (byte) (s >> 8);\r
+ buf[end++] = (byte) s;\r
+ }\r
+ \r
+ public bool IsFlushed {\r
+ get {\r
+ return end == 0;\r
+ }\r
+ }\r
+ \r
+ /// <summary>\r
+ /// Flushes the pending buffer into the given output array. If the\r
+ /// output array is to small, only a partial flush is done.\r
+ /// </summary>\r
+ /// <param name="output">\r
+ /// the output array;\r
+ /// </param>\r
+ /// <param name="offset">\r
+ /// the offset into output array;\r
+ /// </param>\r
+ /// <param name="length"> \r
+ /// length the maximum number of bytes to store;\r
+ /// </param>\r
+ /// <exception name="ArgumentOutOfRangeException">\r
+ /// IndexOutOfBoundsException if offset or length are invalid.\r
+ /// </exception>\r
+ public int Flush(byte[] output, int offset, int length) \r
+ {\r
+ if (bitCount >= 8) {\r
+ buf[end++] = (byte) bits;\r
+ bits >>= 8;\r
+ bitCount -= 8;\r
+ }\r
+ if (length > end - start) {\r
+ length = end - start;\r
+ System.Array.Copy(buf, start, output, offset, length);\r
+ start = 0;\r
+ end = 0;\r
+ } else {\r
+ System.Array.Copy(buf, start, output, offset, length);\r
+ start += length;\r
+ }\r
+ return length;\r
+ }\r
+ \r
+ public byte[] ToByteArray()\r
+ {\r
+ byte[] ret = new byte[end - start];\r
+ System.Array.Copy(buf, start, ret, 0, ret.Length);\r
+ start = 0;\r
+ end = 0;\r
+ return ret;\r
+ }\r
+ }\r
+} \r