OutputWindow.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* OutputWindow.java --
  2. Copyright (C) 2001, 2004 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. * Contains the output from the Inflation process.
  34. *
  35. * We need to have a window so that we can refer backwards into the output stream
  36. * to repeat stuff.
  37. *
  38. * @author John Leuner
  39. * @since 1.1
  40. */
  41. class OutputWindow
  42. {
  43. private static final int WINDOW_SIZE = 1 << 15;
  44. private static final int WINDOW_MASK = WINDOW_SIZE - 1;
  45. private byte[] window = new byte[WINDOW_SIZE]; //The window is 2^15 bytes
  46. private int window_end = 0;
  47. private int window_filled = 0;
  48. public void write(int abyte)
  49. {
  50. if (window_filled++ == WINDOW_SIZE)
  51. throw new IllegalStateException("Window full");
  52. window[window_end++] = (byte) abyte;
  53. window_end &= WINDOW_MASK;
  54. }
  55. private void slowRepeat(int rep_start, int len, int dist)
  56. {
  57. while (len-- > 0)
  58. {
  59. window[window_end++] = window[rep_start++];
  60. window_end &= WINDOW_MASK;
  61. rep_start &= WINDOW_MASK;
  62. }
  63. }
  64. public void repeat(int len, int dist)
  65. {
  66. if ((window_filled += len) > WINDOW_SIZE)
  67. throw new IllegalStateException("Window full");
  68. int rep_start = (window_end - dist) & WINDOW_MASK;
  69. int border = WINDOW_SIZE - len;
  70. if (rep_start <= border && window_end < border)
  71. {
  72. if (len <= dist)
  73. {
  74. System.arraycopy(window, rep_start, window, window_end, len);
  75. window_end += len;
  76. }
  77. else
  78. {
  79. /* We have to copy manually, since the repeat pattern overlaps.
  80. */
  81. while (len-- > 0)
  82. window[window_end++] = window[rep_start++];
  83. }
  84. }
  85. else
  86. slowRepeat(rep_start, len, dist);
  87. }
  88. public int copyStored(StreamManipulator input, int len)
  89. {
  90. len = Math.min(Math.min(len, WINDOW_SIZE - window_filled),
  91. input.getAvailableBytes());
  92. int copied;
  93. int tailLen = WINDOW_SIZE - window_end;
  94. if (len > tailLen)
  95. {
  96. copied = input.copyBytes(window, window_end, tailLen);
  97. if (copied == tailLen)
  98. copied += input.copyBytes(window, 0, len - tailLen);
  99. }
  100. else
  101. copied = input.copyBytes(window, window_end, len);
  102. window_end = (window_end + copied) & WINDOW_MASK;
  103. window_filled += copied;
  104. return copied;
  105. }
  106. public void copyDict(byte[] dict, int offset, int len)
  107. {
  108. if (window_filled > 0)
  109. throw new IllegalStateException();
  110. if (len > WINDOW_SIZE)
  111. {
  112. offset += len - WINDOW_SIZE;
  113. len = WINDOW_SIZE;
  114. }
  115. System.arraycopy(dict, offset, window, 0, len);
  116. window_end = len & WINDOW_MASK;
  117. }
  118. public int getFreeSpace()
  119. {
  120. return WINDOW_SIZE - window_filled;
  121. }
  122. public int getAvailable()
  123. {
  124. return window_filled;
  125. }
  126. public int copyOutput(byte[] output, int offset, int len)
  127. {
  128. int copy_end = window_end;
  129. if (len > window_filled)
  130. len = window_filled;
  131. else
  132. copy_end = (window_end - window_filled + len) & WINDOW_MASK;
  133. int copied = len;
  134. int tailLen = len - copy_end;
  135. if (tailLen > 0)
  136. {
  137. System.arraycopy(window, WINDOW_SIZE - tailLen,
  138. output, offset, tailLen);
  139. offset += tailLen;
  140. len = copy_end;
  141. }
  142. System.arraycopy(window, copy_end - len, output, offset, len);
  143. window_filled -= copied;
  144. if (window_filled < 0)
  145. throw new IllegalStateException();
  146. return copied;
  147. }
  148. public void reset() {
  149. window_filled = window_end = 0;
  150. }
  151. }