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.

WindowCursor.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  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.io.IOException;
  46. import java.util.zip.DataFormatException;
  47. import java.util.zip.Inflater;
  48. /** Active handle to a ByteWindow. */
  49. public final class WindowCursor {
  50. /** Temporary buffer large enough for at least one raw object id. */
  51. final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
  52. private Inflater inf;
  53. private ByteWindow window;
  54. /**
  55. * Copy bytes from the window to a caller supplied buffer.
  56. *
  57. * @param pack
  58. * the file the desired window is stored within.
  59. * @param position
  60. * position within the file to read from.
  61. * @param dstbuf
  62. * destination buffer to copy into.
  63. * @param dstoff
  64. * offset within <code>dstbuf</code> to start copying into.
  65. * @param cnt
  66. * number of bytes to copy. This value may exceed the number of
  67. * bytes remaining in the window starting at offset
  68. * <code>pos</code>.
  69. * @return number of bytes actually copied; this may be less than
  70. * <code>cnt</code> if <code>cnt</code> exceeded the number of
  71. * bytes available.
  72. * @throws IOException
  73. * this cursor does not match the provider or id and the proper
  74. * window could not be acquired through the provider's cache.
  75. */
  76. int copy(final PackFile pack, long position, final byte[] dstbuf,
  77. int dstoff, final int cnt) throws IOException {
  78. final long length = pack.length;
  79. int need = cnt;
  80. while (need > 0 && position < length) {
  81. pin(pack, position);
  82. final int r = window.copy(position, dstbuf, dstoff, need);
  83. position += r;
  84. dstoff += r;
  85. need -= r;
  86. }
  87. return cnt - need;
  88. }
  89. /**
  90. * Pump bytes into the supplied inflater as input.
  91. *
  92. * @param pack
  93. * the file the desired window is stored within.
  94. * @param position
  95. * position within the file to read from.
  96. * @param dstbuf
  97. * destination buffer the inflater should output decompressed
  98. * data to.
  99. * @param dstoff
  100. * current offset within <code>dstbuf</code> to inflate into.
  101. * @return updated <code>dstoff</code> based on the number of bytes
  102. * successfully inflated into <code>dstbuf</code>.
  103. * @throws IOException
  104. * this cursor does not match the provider or id and the proper
  105. * window could not be acquired through the provider's cache.
  106. * @throws DataFormatException
  107. * the inflater encountered an invalid chunk of data. Data
  108. * stream corruption is likely.
  109. */
  110. int inflate(final PackFile pack, long position, final byte[] dstbuf,
  111. int dstoff) throws IOException, DataFormatException {
  112. prepareInflater();
  113. for (;;) {
  114. pin(pack, position);
  115. dstoff = window.inflate(position, dstbuf, dstoff, inf);
  116. if (inf.finished())
  117. return dstoff;
  118. position = window.end;
  119. }
  120. }
  121. void inflateVerify(final PackFile pack, long position)
  122. throws IOException, DataFormatException {
  123. prepareInflater();
  124. for (;;) {
  125. pin(pack, position);
  126. window.inflateVerify(position, inf);
  127. if (inf.finished())
  128. return;
  129. position = window.end;
  130. }
  131. }
  132. private void prepareInflater() {
  133. if (inf == null)
  134. inf = InflaterCache.get();
  135. else
  136. inf.reset();
  137. }
  138. private void pin(final PackFile pack, final long position)
  139. throws IOException {
  140. final ByteWindow w = window;
  141. if (w == null || !w.contains(pack, position)) {
  142. // If memory is low, we may need what is in our window field to
  143. // be cleaned up by the GC during the get for the next window.
  144. // So we always clear it, even though we are just going to set
  145. // it again.
  146. //
  147. window = null;
  148. window = WindowCache.get(pack, position);
  149. }
  150. }
  151. /** Release the current window cursor. */
  152. public void release() {
  153. window = null;
  154. try {
  155. InflaterCache.release(inf);
  156. } finally {
  157. inf = null;
  158. }
  159. }
  160. /**
  161. * @param curs cursor to release; may be null.
  162. * @return always null.
  163. */
  164. public static WindowCursor release(final WindowCursor curs) {
  165. if (curs != null)
  166. curs.release();
  167. return null;
  168. }
  169. }