You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteWindow.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.util.zip.DataFormatException;
  46. import java.util.zip.Inflater;
  47. /**
  48. * A window of data currently stored within a cache.
  49. * <p>
  50. * All bytes in the window can be assumed to be "immediately available", that is
  51. * they are very likely already in memory, unless the operating system's memory
  52. * is very low and has paged part of this process out to disk. Therefore copying
  53. * bytes from a window is very inexpensive.
  54. * </p>
  55. */
  56. abstract class ByteWindow {
  57. protected final PackFile pack;
  58. protected final long start;
  59. protected final long end;
  60. protected ByteWindow(final PackFile p, final long s, final int n) {
  61. pack = p;
  62. start = s;
  63. end = start + n;
  64. }
  65. final int size() {
  66. return (int) (end - start);
  67. }
  68. final boolean contains(final PackFile neededFile, final long neededPos) {
  69. return pack == neededFile && start <= neededPos && neededPos < end;
  70. }
  71. /**
  72. * Copy bytes from the window to a caller supplied buffer.
  73. *
  74. * @param pos
  75. * offset within the file to start copying from.
  76. * @param dstbuf
  77. * destination buffer to copy into.
  78. * @param dstoff
  79. * offset within <code>dstbuf</code> to start copying into.
  80. * @param cnt
  81. * number of bytes to copy. This value may exceed the number of
  82. * bytes remaining in the window starting at offset
  83. * <code>pos</code>.
  84. * @return number of bytes actually copied; this may be less than
  85. * <code>cnt</code> if <code>cnt</code> exceeded the number of
  86. * bytes available.
  87. */
  88. final int copy(long pos, byte[] dstbuf, int dstoff, int cnt) {
  89. return copy((int) (pos - start), dstbuf, dstoff, cnt);
  90. }
  91. /**
  92. * Copy bytes from the window to a caller supplied buffer.
  93. *
  94. * @param pos
  95. * offset within the window to start copying from.
  96. * @param dstbuf
  97. * destination buffer to copy into.
  98. * @param dstoff
  99. * offset within <code>dstbuf</code> to start copying into.
  100. * @param cnt
  101. * number of bytes to copy. This value may exceed the number of
  102. * bytes remaining in the window starting at offset
  103. * <code>pos</code>.
  104. * @return number of bytes actually copied; this may be less than
  105. * <code>cnt</code> if <code>cnt</code> exceeded the number of
  106. * bytes available.
  107. */
  108. protected abstract int copy(int pos, byte[] dstbuf, int dstoff, int cnt);
  109. /**
  110. * Pump bytes into the supplied inflater as input.
  111. *
  112. * @param pos
  113. * offset within the file to start supplying input from.
  114. * @param dstbuf
  115. * destination buffer the inflater should output decompressed
  116. * data to.
  117. * @param dstoff
  118. * current offset within <code>dstbuf</code> to inflate into.
  119. * @param inf
  120. * the inflater to feed input to. The caller is responsible for
  121. * initializing the inflater as multiple windows may need to
  122. * supply data to the same inflater to completely decompress
  123. * something.
  124. * @return updated <code>dstoff</code> based on the number of bytes
  125. * successfully copied into <code>dstbuf</code> by
  126. * <code>inf</code>. If the inflater is not yet finished then
  127. * another window's data must still be supplied as input to finish
  128. * decompression.
  129. * @throws DataFormatException
  130. * the inflater encountered an invalid chunk of data. Data
  131. * stream corruption is likely.
  132. */
  133. final int inflate(long pos, byte[] dstbuf, int dstoff, Inflater inf)
  134. throws DataFormatException {
  135. return inflate((int) (pos - start), dstbuf, dstoff, inf);
  136. }
  137. /**
  138. * Pump bytes into the supplied inflater as input.
  139. *
  140. * @param pos
  141. * offset within the window to start supplying input from.
  142. * @param dstbuf
  143. * destination buffer the inflater should output decompressed
  144. * data to.
  145. * @param dstoff
  146. * current offset within <code>dstbuf</code> to inflate into.
  147. * @param inf
  148. * the inflater to feed input to. The caller is responsible for
  149. * initializing the inflater as multiple windows may need to
  150. * supply data to the same inflater to completely decompress
  151. * something.
  152. * @return updated <code>dstoff</code> based on the number of bytes
  153. * successfully copied into <code>dstbuf</code> by
  154. * <code>inf</code>. If the inflater is not yet finished then
  155. * another window's data must still be supplied as input to finish
  156. * decompression.
  157. * @throws DataFormatException
  158. * the inflater encountered an invalid chunk of data. Data
  159. * stream corruption is likely.
  160. */
  161. protected abstract int inflate(int pos, byte[] dstbuf, int dstoff,
  162. Inflater inf) throws DataFormatException;
  163. protected static final byte[] verifyGarbageBuffer = new byte[2048];
  164. final void inflateVerify(final long pos, final Inflater inf)
  165. throws DataFormatException {
  166. inflateVerify((int) (pos - start), inf);
  167. }
  168. protected abstract void inflateVerify(int pos, Inflater inf)
  169. throws DataFormatException;
  170. }