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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. import org.eclipse.jgit.util.IO;
  54. /**
  55. * Base class for a set of loaders for different representations of Git objects.
  56. * New loaders are constructed for every object.
  57. */
  58. public abstract class ObjectLoader {
  59. /**
  60. * Default setting for the large object threshold.
  61. * <p>
  62. * Objects larger than this size must be accessed as a stream through the
  63. * loader's {@link #openStream()} method.
  64. */
  65. public static final int STREAM_THRESHOLD = 5 * 1024 * 1024;
  66. /**
  67. * @return Git in pack object type, see {@link Constants}.
  68. */
  69. public abstract int getType();
  70. /**
  71. * @return size of object in bytes
  72. */
  73. public abstract long getSize();
  74. /**
  75. * @return true if this object is too large to obtain as a byte array.
  76. * Objects over a certain threshold should be accessed only by their
  77. * {@link #openStream()} to prevent overflowing the JVM heap.
  78. */
  79. public boolean isLarge() {
  80. try {
  81. getCachedBytes();
  82. return false;
  83. } catch (LargeObjectException tooBig) {
  84. return true;
  85. }
  86. }
  87. /**
  88. * Obtain a copy of the bytes of this object.
  89. * <p>
  90. * Unlike {@link #getCachedBytes()} this method returns an array that might
  91. * be modified by the caller.
  92. *
  93. * @return the bytes of this object.
  94. * @throws LargeObjectException
  95. * if the object won't fit into a byte array, because
  96. * {@link #isLarge()} returns true. Callers should use
  97. * {@link #openStream()} instead to access the contents.
  98. */
  99. public final byte[] getBytes() throws LargeObjectException {
  100. return cloneArray(getCachedBytes());
  101. }
  102. /**
  103. * Obtain a copy of the bytes of this object.
  104. *
  105. * If the object size is less than or equal to {@code sizeLimit} this method
  106. * will provide it as a byte array, even if {@link #isLarge()} is true. This
  107. * utility is useful for application code that absolutely must have the
  108. * object as a single contiguous byte array in memory.
  109. *
  110. * Unlike {@link #getCachedBytes(int)} this method returns an array that
  111. * might be modified by the caller.
  112. *
  113. * @param sizeLimit
  114. * maximum number of bytes to return. If the object is larger
  115. * than this limit, {@link LargeObjectException} will be thrown.
  116. * @return the bytes of this object.
  117. * @throws LargeObjectException
  118. * if the object is bigger than {@code sizeLimit}, or if
  119. * {@link OutOfMemoryError} occurs during allocation of the
  120. * result array. Callers should use {@link #openStream()}
  121. * instead to access the contents.
  122. * @throws MissingObjectException
  123. * the object is large, and it no longer exists.
  124. * @throws IOException
  125. * the object store cannot be accessed.
  126. */
  127. public final byte[] getBytes(int sizeLimit) throws LargeObjectException,
  128. MissingObjectException, IOException {
  129. byte[] cached = getCachedBytes(sizeLimit);
  130. try {
  131. return cloneArray(cached);
  132. } catch (OutOfMemoryError tooBig) {
  133. throw new LargeObjectException.OutOfMemory(tooBig);
  134. }
  135. }
  136. /**
  137. * Obtain a reference to the (possibly cached) bytes of this object.
  138. * <p>
  139. * This method offers direct access to the internal caches, potentially
  140. * saving on data copies between the internal cache and higher level code.
  141. * Callers who receive this reference <b>must not</b> modify its contents.
  142. * Changes (if made) will affect the cache but not the repository itself.
  143. *
  144. * @return the cached bytes of this object. Do not modify it.
  145. * @throws LargeObjectException
  146. * if the object won't fit into a byte array, because
  147. * {@link #isLarge()} returns true. Callers should use
  148. * {@link #openStream()} instead to access the contents.
  149. */
  150. public abstract byte[] getCachedBytes() throws LargeObjectException;
  151. /**
  152. * Obtain a reference to the (possibly cached) bytes of this object.
  153. *
  154. * If the object size is less than or equal to {@code sizeLimit} this method
  155. * will provide it as a byte array, even if {@link #isLarge()} is true. This
  156. * utility is useful for application code that absolutely must have the
  157. * object as a single contiguous byte array in memory.
  158. *
  159. * This method offers direct access to the internal caches, potentially
  160. * saving on data copies between the internal cache and higher level code.
  161. * Callers who receive this reference <b>must not</b> modify its contents.
  162. * Changes (if made) will affect the cache but not the repository itself.
  163. *
  164. * @param sizeLimit
  165. * maximum number of bytes to return. If the object size is
  166. * larger than this limit and {@link #isLarge()} is true,
  167. * {@link LargeObjectException} will be thrown.
  168. * @return the cached bytes of this object. Do not modify it.
  169. * @throws LargeObjectException
  170. * if the object is bigger than {@code sizeLimit}, or if
  171. * {@link OutOfMemoryError} occurs during allocation of the
  172. * result array. Callers should use {@link #openStream()}
  173. * instead to access the contents.
  174. * @throws MissingObjectException
  175. * the object is large, and it no longer exists.
  176. * @throws IOException
  177. * the object store cannot be accessed.
  178. */
  179. public byte[] getCachedBytes(int sizeLimit) throws LargeObjectException,
  180. MissingObjectException, IOException {
  181. if (!isLarge())
  182. return getCachedBytes();
  183. ObjectStream in = openStream();
  184. try {
  185. long sz = in.getSize();
  186. if (sizeLimit < sz)
  187. throw new LargeObjectException.ExceedsLimit(sizeLimit, sz);
  188. if (Integer.MAX_VALUE < sz)
  189. throw new LargeObjectException.ExceedsByteArrayLimit();
  190. byte[] buf;
  191. try {
  192. buf = new byte[(int) sz];
  193. } catch (OutOfMemoryError notEnoughHeap) {
  194. throw new LargeObjectException.OutOfMemory(notEnoughHeap);
  195. }
  196. IO.readFully(in, buf, 0, buf.length);
  197. return buf;
  198. } finally {
  199. in.close();
  200. }
  201. }
  202. /**
  203. * Obtain an input stream to read this object's data.
  204. *
  205. * @return a stream of this object's data. Caller must close the stream when
  206. * through with it. The returned stream is buffered with a
  207. * reasonable buffer size.
  208. * @throws MissingObjectException
  209. * the object no longer exists.
  210. * @throws IOException
  211. * the object store cannot be accessed.
  212. */
  213. public abstract ObjectStream openStream() throws MissingObjectException,
  214. IOException;
  215. /**
  216. * Copy this object to the output stream.
  217. * <p>
  218. * For some object store implementations, this method may be more efficient
  219. * than reading from {@link #openStream()} into a temporary byte array, then
  220. * writing to the destination stream.
  221. * <p>
  222. * The default implementation of this method is to copy with a temporary
  223. * byte array for large objects, or to pass through the cached byte array
  224. * for small objects.
  225. *
  226. * @param out
  227. * stream to receive the complete copy of this object's data.
  228. * Caller is responsible for flushing or closing this stream
  229. * after this method returns.
  230. * @throws MissingObjectException
  231. * the object no longer exists.
  232. * @throws IOException
  233. * the object store cannot be accessed, or the stream cannot be
  234. * written to.
  235. */
  236. public void copyTo(OutputStream out) throws MissingObjectException,
  237. IOException {
  238. if (isLarge()) {
  239. ObjectStream in = openStream();
  240. try {
  241. final long sz = in.getSize();
  242. byte[] tmp = new byte[8192];
  243. long copied = 0;
  244. while (copied < sz) {
  245. int n = in.read(tmp);
  246. if (n < 0)
  247. throw new EOFException();
  248. out.write(tmp, 0, n);
  249. copied += n;
  250. }
  251. if (0 <= in.read())
  252. throw new EOFException();
  253. } finally {
  254. in.close();
  255. }
  256. } else {
  257. out.write(getCachedBytes());
  258. }
  259. }
  260. private static byte[] cloneArray(final byte[] data) {
  261. final byte[] copy = new byte[data.length];
  262. System.arraycopy(data, 0, copy, 0, data.length);
  263. return copy;
  264. }
  265. /**
  266. * Simple loader around the cached byte array.
  267. * <p>
  268. * ObjectReader implementations can use this stream type when the object's
  269. * content is small enough to be accessed as a single byte array.
  270. */
  271. public static class SmallObject extends ObjectLoader {
  272. private final int type;
  273. private final byte[] data;
  274. /**
  275. * Construct a small object loader.
  276. *
  277. * @param type
  278. * type of the object.
  279. * @param data
  280. * the object's data array. This array will be returned as-is
  281. * for the {@link #getCachedBytes()} method.
  282. */
  283. public SmallObject(int type, byte[] data) {
  284. this.type = type;
  285. this.data = data;
  286. }
  287. @Override
  288. public int getType() {
  289. return type;
  290. }
  291. @Override
  292. public long getSize() {
  293. return getCachedBytes().length;
  294. }
  295. @Override
  296. public boolean isLarge() {
  297. return false;
  298. }
  299. @Override
  300. public byte[] getCachedBytes() {
  301. return data;
  302. }
  303. @Override
  304. public ObjectStream openStream() {
  305. return new ObjectStream.SmallStream(this);
  306. }
  307. }
  308. }