StreamManipulator.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* java.util.zip.StreamManipulator
  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 allows us to retrieve a specified amount of bits from
  34. * the input buffer, as well as copy big byte blocks.
  35. *
  36. * It uses an int buffer to store up to 31 bits for direct
  37. * manipulation. This guarantees that we can get at least 16 bits,
  38. * but we only need at most 15, so this is all safe.
  39. *
  40. * There are some optimizations in this class, for example, you must
  41. * never peek more then 8 bits more than needed, and you must first
  42. * peek bits before you may drop them. This is not a general purpose
  43. * class but optimized for the behaviour of the Inflater.
  44. *
  45. * @author John Leuner, Jochen Hoenicke
  46. */
  47. class StreamManipulator
  48. {
  49. private byte[] window;
  50. private int window_start = 0;
  51. private int window_end = 0;
  52. private int buffer = 0;
  53. private int bits_in_buffer = 0;
  54. /**
  55. * Get the next n bits but don't increase input pointer. n must be
  56. * less or equal 16 and if you if this call succeeds, you must drop
  57. * at least n-8 bits in the next call.
  58. *
  59. * @return the value of the bits, or -1 if not enough bits available. */
  60. public final int peekBits(int n)
  61. {
  62. if (bits_in_buffer < n)
  63. {
  64. if (window_start == window_end)
  65. return -1;
  66. buffer |= (window[window_start++] & 0xff
  67. | (window[window_start++] & 0xff) << 8) << bits_in_buffer;
  68. bits_in_buffer += 16;
  69. }
  70. return buffer & ((1 << n) - 1);
  71. }
  72. /* Drops the next n bits from the input. You should have called peekBits
  73. * with a bigger or equal n before, to make sure that enough bits are in
  74. * the bit buffer.
  75. */
  76. public final void dropBits(int n)
  77. {
  78. buffer >>>= n;
  79. bits_in_buffer -= n;
  80. }
  81. /**
  82. * Gets the next n bits and increases input pointer. This is equivalent
  83. * to peekBits followed by dropBits, except for correct error handling.
  84. * @return the value of the bits, or -1 if not enough bits available.
  85. */
  86. public final int getBits(int n)
  87. {
  88. int bits = peekBits(n);
  89. if (bits >= 0)
  90. dropBits(n);
  91. return bits;
  92. }
  93. /**
  94. * Gets the number of bits available in the bit buffer. This must be
  95. * only called when a previous peekBits() returned -1.
  96. * @return the number of bits available.
  97. */
  98. public final int getAvailableBits()
  99. {
  100. return bits_in_buffer;
  101. }
  102. /**
  103. * Gets the number of bytes available.
  104. * @return the number of bytes available.
  105. */
  106. public final int getAvailableBytes()
  107. {
  108. return window_end - window_start + (bits_in_buffer >> 3);
  109. }
  110. /**
  111. * Skips to the next byte boundary.
  112. */
  113. public void skipToByteBoundary()
  114. {
  115. buffer >>= (bits_in_buffer & 7);
  116. bits_in_buffer &= ~7;
  117. }
  118. public final boolean needsInput() {
  119. return window_start == window_end;
  120. }
  121. /* Copies length bytes from input buffer to output buffer starting
  122. * at output[offset]. You have to make sure, that the buffer is
  123. * byte aligned. If not enough bytes are available, copies fewer
  124. * bytes.
  125. * @param length the length to copy, 0 is allowed.
  126. * @return the number of bytes copied, 0 if no byte is available.
  127. */
  128. public int copyBytes(byte[] output, int offset, int length)
  129. {
  130. if (length < 0)
  131. throw new IllegalArgumentException("length negative");
  132. if ((bits_in_buffer & 7) != 0)
  133. /* bits_in_buffer may only be 0 or 8 */
  134. throw new IllegalStateException("Bit buffer is not aligned!");
  135. int count = 0;
  136. while (bits_in_buffer > 0 && length > 0)
  137. {
  138. output[offset++] = (byte) buffer;
  139. buffer >>>= 8;
  140. bits_in_buffer -= 8;
  141. length--;
  142. count++;
  143. }
  144. if (length == 0)
  145. return count;
  146. int avail = window_end - window_start;
  147. if (length > avail)
  148. length = avail;
  149. System.arraycopy(window, window_start, output, offset, length);
  150. window_start += length;
  151. if (((window_start - window_end) & 1) != 0)
  152. {
  153. /* We always want an even number of bytes in input, see peekBits */
  154. buffer = (window[window_start++] & 0xff);
  155. bits_in_buffer = 8;
  156. }
  157. return count + length;
  158. }
  159. public StreamManipulator()
  160. {
  161. }
  162. public void reset()
  163. {
  164. window_start = window_end = buffer = bits_in_buffer = 0;
  165. }
  166. public void setInput(byte[] buf, int off, int len)
  167. {
  168. if (window_start < window_end)
  169. throw new IllegalStateException
  170. ("Old input was not completely processed");
  171. int end = off + len;
  172. /* We want to throw an ArrayIndexOutOfBoundsException early. The
  173. * check is very tricky: it also handles integer wrap around.
  174. */
  175. if (0 > off || off > end || end > buf.length)
  176. throw new ArrayIndexOutOfBoundsException();
  177. if ((len & 1) != 0)
  178. {
  179. /* We always want an even number of bytes in input, see peekBits */
  180. buffer |= (buf[off++] & 0xff) << bits_in_buffer;
  181. bits_in_buffer += 8;
  182. }
  183. window = buf;
  184. window_start = off;
  185. window_end = end;
  186. }
  187. }