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.

ObjectLoader.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
  4. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  5. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  6. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  7. * and other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available
  10. * under the terms of the Eclipse Distribution License v1.0 which
  11. * accompanies this distribution, is reproduced below, and is
  12. * available at http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials provided
  26. * with the distribution.
  27. *
  28. * - Neither the name of the Eclipse Foundation, Inc. nor the
  29. * names of its contributors may be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  43. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. package org.eclipse.jgit.lib;
  48. import java.io.EOFException;
  49. import java.io.IOException;
  50. import java.io.OutputStream;
  51. import org.eclipse.jgit.errors.LargeObjectException;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. /**
  54. * Base class for a set of loaders for different representations of Git objects.
  55. * New loaders are constructed for every object.
  56. */
  57. public abstract class ObjectLoader {
  58. /**
  59. * Default setting for the large object threshold.
  60. * <p>
  61. * Objects larger than this size must be accessed as a stream through the
  62. * loader's {@link #openStream()} method.
  63. */
  64. public static final int STREAM_THRESHOLD = 1024 * 1024;
  65. /**
  66. * @return Git in pack object type, see {@link Constants}.
  67. */
  68. public abstract int getType();
  69. /**
  70. * @return size of object in bytes
  71. */
  72. public abstract long getSize();
  73. /**
  74. * @return true if this object is too large to obtain as a byte array.
  75. * Objects over a certain threshold should be accessed only by their
  76. * {@link #openStream()} to prevent overflowing the JVM heap.
  77. */
  78. public boolean isLarge() {
  79. try {
  80. getCachedBytes();
  81. return false;
  82. } catch (LargeObjectException tooBig) {
  83. return true;
  84. }
  85. }
  86. /**
  87. * Obtain a copy of the bytes of this object.
  88. * <p>
  89. * Unlike {@link #getCachedBytes()} this method returns an array that might
  90. * be modified by the caller.
  91. *
  92. * @return the bytes of this object.
  93. * @throws LargeObjectException
  94. * if the object won't fit into a byte array, because
  95. * {@link #isLarge()} returns true. Callers should use
  96. * {@link #openStream()} instead to access the contents.
  97. */
  98. public final byte[] getBytes() throws LargeObjectException {
  99. final byte[] data = getCachedBytes();
  100. final byte[] copy = new byte[data.length];
  101. System.arraycopy(data, 0, copy, 0, data.length);
  102. return copy;
  103. }
  104. /**
  105. * Obtain a reference to the (possibly cached) bytes of this object.
  106. * <p>
  107. * This method offers direct access to the internal caches, potentially
  108. * saving on data copies between the internal cache and higher level code.
  109. * Callers who receive this reference <b>must not</b> modify its contents.
  110. * Changes (if made) will affect the cache but not the repository itself.
  111. *
  112. * @return the cached bytes of this object. Do not modify it.
  113. * @throws LargeObjectException
  114. * if the object won't fit into a byte array, because
  115. * {@link #isLarge()} returns true. Callers should use
  116. * {@link #openStream()} instead to access the contents.
  117. */
  118. public abstract byte[] getCachedBytes() throws LargeObjectException;
  119. /**
  120. * Obtain an input stream to read this object's data.
  121. *
  122. * @return a stream of this object's data. Caller must close the stream when
  123. * through with it. The returned stream is buffered with a
  124. * reasonable buffer size.
  125. * @throws MissingObjectException
  126. * the object no longer exists.
  127. * @throws IOException
  128. * the object store cannot be accessed.
  129. */
  130. public abstract ObjectStream openStream() throws MissingObjectException,
  131. IOException;
  132. /**
  133. * Copy this object to the output stream.
  134. * <p>
  135. * For some object store implementations, this method may be more efficient
  136. * than reading from {@link #openStream()} into a temporary byte array, then
  137. * writing to the destination stream.
  138. * <p>
  139. * The default implementation of this method is to copy with a temporary
  140. * byte array for large objects, or to pass through the cached byte array
  141. * for small objects.
  142. *
  143. * @param out
  144. * stream to receive the complete copy of this object's data.
  145. * Caller is responsible for flushing or closing this stream
  146. * after this method returns.
  147. * @throws MissingObjectException
  148. * the object no longer exists.
  149. * @throws IOException
  150. * the object store cannot be accessed, or the stream cannot be
  151. * written to.
  152. */
  153. public void copyTo(OutputStream out) throws MissingObjectException,
  154. IOException {
  155. if (isLarge()) {
  156. ObjectStream in = openStream();
  157. try {
  158. byte[] tmp = new byte[1024];
  159. long copied = 0;
  160. for (;;) {
  161. int n = in.read(tmp);
  162. if (n < 0)
  163. break;
  164. out.write(tmp, 0, n);
  165. copied += n;
  166. }
  167. if (copied != getSize())
  168. throw new EOFException();
  169. } finally {
  170. in.close();
  171. }
  172. } else {
  173. out.write(getCachedBytes());
  174. }
  175. }
  176. /**
  177. * Simple loader around the cached byte array.
  178. * <p>
  179. * ObjectReader implementations can use this stream type when the object's
  180. * content is small enough to be accessed as a single byte array.
  181. */
  182. public static class SmallObject extends ObjectLoader {
  183. private final int type;
  184. private final byte[] data;
  185. /**
  186. * Construct a small object loader.
  187. *
  188. * @param type
  189. * type of the object.
  190. * @param data
  191. * the object's data array. This array will be returned as-is
  192. * for the {@link #getCachedBytes()} method.
  193. */
  194. public SmallObject(int type, byte[] data) {
  195. this.type = type;
  196. this.data = data;
  197. }
  198. @Override
  199. public int getType() {
  200. return type;
  201. }
  202. @Override
  203. public long getSize() {
  204. return getCachedBytes().length;
  205. }
  206. @Override
  207. public boolean isLarge() {
  208. return false;
  209. }
  210. @Override
  211. public byte[] getCachedBytes() {
  212. return data;
  213. }
  214. @Override
  215. public ObjectStream openStream() {
  216. return new ObjectStream.SmallStream(this);
  217. }
  218. }
  219. }