PendingBuffer.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* java.util.zip.PendingBuffer
  2. Copyright (C) 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.zip;
  32. /**
  33. * This class is general purpose class for writing data to a buffer.
  34. *
  35. * It allows you to write bits as well as bytes
  36. *
  37. * Based on DeflaterPending.java
  38. *
  39. * @author Jochen Hoenicke
  40. * @date Jan 5, 2000
  41. */
  42. class PendingBuffer
  43. {
  44. protected byte[] buf;
  45. int start;
  46. int end;
  47. int bits;
  48. int bitCount;
  49. public PendingBuffer()
  50. {
  51. this( 4096 );
  52. }
  53. public PendingBuffer(int bufsize)
  54. {
  55. buf = new byte[bufsize];
  56. }
  57. public final void reset() {
  58. start = end = bitCount = 0;
  59. }
  60. public final void writeByte(int b)
  61. {
  62. if (DeflaterConstants.DEBUGGING && start != 0)
  63. throw new IllegalStateException();
  64. buf[end++] = (byte) b;
  65. }
  66. public final void writeShort(int s)
  67. {
  68. if (DeflaterConstants.DEBUGGING && start != 0)
  69. throw new IllegalStateException();
  70. buf[end++] = (byte) s;
  71. buf[end++] = (byte) (s >> 8);
  72. }
  73. public final void writeInt(int s)
  74. {
  75. if (DeflaterConstants.DEBUGGING && start != 0)
  76. throw new IllegalStateException();
  77. buf[end++] = (byte) s;
  78. buf[end++] = (byte) (s >> 8);
  79. buf[end++] = (byte) (s >> 16);
  80. buf[end++] = (byte) (s >> 24);
  81. }
  82. public final void writeBlock(byte[] block, int offset, int len)
  83. {
  84. if (DeflaterConstants.DEBUGGING && start != 0)
  85. throw new IllegalStateException();
  86. System.arraycopy(block, offset, buf, end, len);
  87. end += len;
  88. }
  89. public final int getBitCount() {
  90. return bitCount;
  91. }
  92. public final void alignToByte() {
  93. if (DeflaterConstants.DEBUGGING && start != 0)
  94. throw new IllegalStateException();
  95. if (bitCount > 0)
  96. {
  97. buf[end++] = (byte) bits;
  98. if (bitCount > 8)
  99. buf[end++] = (byte) (bits >>> 8);
  100. }
  101. bits = 0;
  102. bitCount = 0;
  103. }
  104. public final void writeBits(int b, int count)
  105. {
  106. if (DeflaterConstants.DEBUGGING && start != 0)
  107. throw new IllegalStateException();
  108. if (DeflaterConstants.DEBUGGING)
  109. System.err.println("writeBits("+Integer.toHexString(b)+","+count+")");
  110. bits |= b << bitCount;
  111. bitCount += count;
  112. if (bitCount >= 16) {
  113. buf[end++] = (byte) bits;
  114. buf[end++] = (byte) (bits >>> 8);
  115. bits >>>= 16;
  116. bitCount -= 16;
  117. }
  118. }
  119. public final void writeShortMSB(int s) {
  120. if (DeflaterConstants.DEBUGGING && start != 0)
  121. throw new IllegalStateException();
  122. buf[end++] = (byte) (s >> 8);
  123. buf[end++] = (byte) s;
  124. }
  125. public final boolean isFlushed() {
  126. return end == 0;
  127. }
  128. /**
  129. * Flushes the pending buffer into the given output array. If the
  130. * output array is to small, only a partial flush is done.
  131. *
  132. * @param output the output array;
  133. * @param offset the offset into output array;
  134. * @param length the maximum number of bytes to store;
  135. * @exception IndexOutOfBoundsException if offset or length are
  136. * invalid.
  137. */
  138. public final int flush(byte[] output, int offset, int length) {
  139. if (bitCount >= 8)
  140. {
  141. buf[end++] = (byte) bits;
  142. bits >>>= 8;
  143. bitCount -= 8;
  144. }
  145. if (length > end - start)
  146. {
  147. length = end - start;
  148. System.arraycopy(buf, start, output, offset, length);
  149. start = 0;
  150. end = 0;
  151. }
  152. else
  153. {
  154. System.arraycopy(buf, start, output, offset, length);
  155. start += length;
  156. }
  157. return length;
  158. }
  159. /**
  160. * Flushes the pending buffer and returns that data in a new array
  161. *
  162. * @return the output stream
  163. */
  164. public final byte[] toByteArray()
  165. {
  166. byte[] ret = new byte[ end - start ];
  167. System.arraycopy(buf, start, ret, 0, ret.length);
  168. start = 0;
  169. end = 0;
  170. return ret;
  171. }
  172. }