您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ObjectLoader.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. * Get Git in pack object type
  61. *
  62. * @return Git in pack object type, see
  63. * {@link org.eclipse.jgit.lib.Constants}.
  64. */
  65. public abstract int getType();
  66. /**
  67. * Get size of object in bytes
  68. *
  69. * @return size of object in bytes
  70. */
  71. public abstract long getSize();
  72. /**
  73. * Whether this object is too large to obtain as a byte array.
  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 org.eclipse.jgit.errors.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,
  116. * {@link org.eclipse.jgit.errors.LargeObjectException} will be
  117. * thrown.
  118. * @return the bytes of this object.
  119. * @throws org.eclipse.jgit.errors.LargeObjectException
  120. * if the object is bigger than {@code sizeLimit}, or if
  121. * {@link java.lang.OutOfMemoryError} occurs during allocation
  122. * of the result array. Callers should use {@link #openStream()}
  123. * instead to access the contents.
  124. * @throws org.eclipse.jgit.errors.MissingObjectException
  125. * the object is large, and it no longer exists.
  126. * @throws java.io.IOException
  127. * the object store cannot be accessed.
  128. */
  129. public final byte[] getBytes(int sizeLimit) throws LargeObjectException,
  130. MissingObjectException, IOException {
  131. byte[] cached = getCachedBytes(sizeLimit);
  132. try {
  133. return cloneArray(cached);
  134. } catch (OutOfMemoryError tooBig) {
  135. throw new LargeObjectException.OutOfMemory(tooBig);
  136. }
  137. }
  138. /**
  139. * Obtain a reference to the (possibly cached) bytes of this object.
  140. * <p>
  141. * This method offers direct access to the internal caches, potentially
  142. * saving on data copies between the internal cache and higher level code.
  143. * Callers who receive this reference <b>must not</b> modify its contents.
  144. * Changes (if made) will affect the cache but not the repository itself.
  145. *
  146. * @return the cached bytes of this object. Do not modify it.
  147. * @throws org.eclipse.jgit.errors.LargeObjectException
  148. * if the object won't fit into a byte array, because
  149. * {@link #isLarge()} returns true. Callers should use
  150. * {@link #openStream()} instead to access the contents.
  151. */
  152. public abstract byte[] getCachedBytes() throws LargeObjectException;
  153. /**
  154. * Obtain a reference to the (possibly cached) bytes of this object.
  155. *
  156. * If the object size is less than or equal to {@code sizeLimit} this method
  157. * will provide it as a byte array, even if {@link #isLarge()} is true. This
  158. * utility is useful for application code that absolutely must have the
  159. * object as a single contiguous byte array in memory.
  160. *
  161. * This method offers direct access to the internal caches, potentially
  162. * saving on data copies between the internal cache and higher level code.
  163. * Callers who receive this reference <b>must not</b> modify its contents.
  164. * Changes (if made) will affect the cache but not the repository itself.
  165. *
  166. * @param sizeLimit
  167. * maximum number of bytes to return. If the object size is
  168. * larger than this limit and {@link #isLarge()} is true,
  169. * {@link org.eclipse.jgit.errors.LargeObjectException} will be
  170. * thrown.
  171. * @return the cached bytes of this object. Do not modify it.
  172. * @throws org.eclipse.jgit.errors.LargeObjectException
  173. * if the object is bigger than {@code sizeLimit}, or if
  174. * {@link java.lang.OutOfMemoryError} occurs during allocation
  175. * of the result array. Callers should use {@link #openStream()}
  176. * instead to access the contents.
  177. * @throws org.eclipse.jgit.errors.MissingObjectException
  178. * the object is large, and it no longer exists.
  179. * @throws java.io.IOException
  180. * the object store cannot be accessed.
  181. */
  182. public byte[] getCachedBytes(int sizeLimit) throws LargeObjectException,
  183. MissingObjectException, IOException {
  184. if (!isLarge())
  185. return getCachedBytes();
  186. try (ObjectStream in = openStream()) {
  187. long sz = in.getSize();
  188. if (sizeLimit < sz)
  189. throw new LargeObjectException.ExceedsLimit(sizeLimit, sz);
  190. if (Integer.MAX_VALUE < sz)
  191. throw new LargeObjectException.ExceedsByteArrayLimit();
  192. byte[] buf;
  193. try {
  194. buf = new byte[(int) sz];
  195. } catch (OutOfMemoryError notEnoughHeap) {
  196. throw new LargeObjectException.OutOfMemory(notEnoughHeap);
  197. }
  198. IO.readFully(in, buf, 0, buf.length);
  199. return buf;
  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 org.eclipse.jgit.errors.MissingObjectException
  209. * the object no longer exists.
  210. * @throws java.io.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 org.eclipse.jgit.errors.MissingObjectException
  231. * the object no longer exists.
  232. * @throws java.io.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. try (ObjectStream in = openStream()) {
  240. final long sz = in.getSize();
  241. byte[] tmp = new byte[8192];
  242. long copied = 0;
  243. while (copied < sz) {
  244. int n = in.read(tmp);
  245. if (n < 0)
  246. throw new EOFException();
  247. out.write(tmp, 0, n);
  248. copied += n;
  249. }
  250. if (0 <= in.read())
  251. throw new EOFException();
  252. }
  253. } else {
  254. out.write(getCachedBytes());
  255. }
  256. }
  257. private static byte[] cloneArray(byte[] data) {
  258. final byte[] copy = new byte[data.length];
  259. System.arraycopy(data, 0, copy, 0, data.length);
  260. return copy;
  261. }
  262. /**
  263. * Simple loader around the cached byte array.
  264. * <p>
  265. * ObjectReader implementations can use this stream type when the object's
  266. * content is small enough to be accessed as a single byte array.
  267. */
  268. public static class SmallObject extends ObjectLoader {
  269. private final int type;
  270. private final byte[] data;
  271. /**
  272. * Construct a small object loader.
  273. *
  274. * @param type
  275. * type of the object.
  276. * @param data
  277. * the object's data array. This array will be returned as-is
  278. * for the {@link #getCachedBytes()} method.
  279. */
  280. public SmallObject(int type, byte[] data) {
  281. this.type = type;
  282. this.data = data;
  283. }
  284. @Override
  285. public int getType() {
  286. return type;
  287. }
  288. @Override
  289. public long getSize() {
  290. return getCachedBytes().length;
  291. }
  292. @Override
  293. public boolean isLarge() {
  294. return false;
  295. }
  296. @Override
  297. public byte[] getCachedBytes() {
  298. return data;
  299. }
  300. @Override
  301. public ObjectStream openStream() {
  302. return new ObjectStream.SmallStream(this);
  303. }
  304. }
  305. /**
  306. * Wraps a delegate ObjectLoader.
  307. *
  308. * @since 4.10
  309. */
  310. public abstract static class Filter extends ObjectLoader {
  311. /**
  312. * @return delegate ObjectLoader to handle all processing.
  313. * @since 4.10
  314. */
  315. protected abstract ObjectLoader delegate();
  316. @Override
  317. public int getType() {
  318. return delegate().getType();
  319. }
  320. @Override
  321. public long getSize() {
  322. return delegate().getSize();
  323. }
  324. @Override
  325. public boolean isLarge() {
  326. return delegate().isLarge();
  327. }
  328. @Override
  329. public byte[] getCachedBytes() {
  330. return delegate().getCachedBytes();
  331. }
  332. @Override
  333. public ObjectStream openStream() throws IOException {
  334. return delegate().openStream();
  335. }
  336. }
  337. }