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.

PackedObjectLoader.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.IOException;
  48. import java.io.OutputStream;
  49. /**
  50. * Base class for a set of object loader classes for packed objects.
  51. */
  52. abstract class PackedObjectLoader extends ObjectLoader {
  53. protected final PackFile pack;
  54. protected final long dataOffset;
  55. protected final long objectOffset;
  56. protected int objectType;
  57. protected int objectSize;
  58. protected byte[] cachedBytes;
  59. PackedObjectLoader(final PackFile pr, final long dataOffset,
  60. final long objectOffset) {
  61. pack = pr;
  62. this.dataOffset = dataOffset;
  63. this.objectOffset = objectOffset;
  64. }
  65. /**
  66. * Force this object to be loaded into memory and pinned in this loader.
  67. * <p>
  68. * Once materialized, subsequent get operations for the following methods
  69. * will always succeed without raising an exception, as all information is
  70. * pinned in memory by this loader instance.
  71. * <ul>
  72. * <li>{@link #getType()}</li>
  73. * <li>{@link #getSize()}</li>
  74. * <li>{@link #getBytes()}, {@link #getCachedBytes}</li>
  75. * <li>{@link #getRawSize()}</li>
  76. * <li>{@link #getRawType()}</li>
  77. * </ul>
  78. *
  79. * @param curs
  80. * temporary thread storage during data access.
  81. * @throws IOException
  82. * the object cannot be read.
  83. */
  84. public abstract void materialize(WindowCursor curs) throws IOException;
  85. public final int getType() {
  86. return objectType;
  87. }
  88. public final long getSize() {
  89. return objectSize;
  90. }
  91. @Override
  92. public final byte[] getCachedBytes() {
  93. return cachedBytes;
  94. }
  95. /**
  96. * @return offset of object header within pack file
  97. */
  98. public final long getObjectOffset() {
  99. return objectOffset;
  100. }
  101. /**
  102. * @return offset of object data within pack file
  103. */
  104. public final long getDataOffset() {
  105. return dataOffset;
  106. }
  107. /**
  108. * Peg the pack file open to support data copying.
  109. * <p>
  110. * Applications trying to copy raw pack data should ensure the pack stays
  111. * open and available throughout the entire copy. To do that use:
  112. *
  113. * <pre>
  114. * loader.beginCopyRawData();
  115. * try {
  116. * loader.copyRawData(out, tmpbuf, curs);
  117. * } finally {
  118. * loader.endCopyRawData();
  119. * }
  120. * </pre>
  121. *
  122. * @throws IOException
  123. * this loader contains stale information and cannot be used.
  124. * The most likely cause is the underlying pack file has been
  125. * deleted, and the object has moved to another pack file.
  126. */
  127. public void beginCopyRawData() throws IOException {
  128. pack.beginCopyRawData();
  129. }
  130. /**
  131. * Copy raw object representation from storage to provided output stream.
  132. * <p>
  133. * Copied data doesn't include object header. User must provide temporary
  134. * buffer used during copying by underlying I/O layer.
  135. * </p>
  136. *
  137. * @param out
  138. * output stream when data is copied. No buffering is guaranteed.
  139. * @param buf
  140. * temporary buffer used during copying. Recommended size is at
  141. * least few kB.
  142. * @param curs
  143. * temporary thread storage during data access.
  144. * @throws IOException
  145. * when the object cannot be read.
  146. * @see #beginCopyRawData()
  147. */
  148. public void copyRawData(OutputStream out, byte buf[], WindowCursor curs)
  149. throws IOException {
  150. pack.copyRawData(this, out, buf, curs);
  151. }
  152. /** Release resources after {@link #beginCopyRawData()}. */
  153. public void endCopyRawData() {
  154. pack.endCopyRawData();
  155. }
  156. /**
  157. * @return true if this loader is capable of fast raw-data copying basing on
  158. * compressed data checksum; false if raw-data copying needs
  159. * uncompressing and compressing data
  160. * @throws IOException
  161. * the index file format cannot be determined.
  162. */
  163. public boolean supportsFastCopyRawData() throws IOException {
  164. return pack.supportsFastCopyRawData();
  165. }
  166. /**
  167. * @return id of delta base object for this object representation. null if
  168. * object is not stored as delta.
  169. * @throws IOException
  170. * when delta base cannot read.
  171. */
  172. public abstract ObjectId getDeltaBase() throws IOException;
  173. }