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

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