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.

ObjectWriter.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2009, Google Inc.
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  47. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. /**
  53. * A class for writing loose objects.
  54. *
  55. * @deprecated Use {@link Repository#newObjectInserter()}.
  56. */
  57. public class ObjectWriter {
  58. private final ObjectInserter inserter;
  59. /**
  60. * Construct an Object writer for the specified repository
  61. *
  62. * @param d
  63. */
  64. public ObjectWriter(final Repository d) {
  65. inserter = d.newObjectInserter();
  66. }
  67. /**
  68. * Write a blob with the specified data
  69. *
  70. * @param b
  71. * bytes of the blob
  72. * @return SHA-1 of the blob
  73. * @throws IOException
  74. */
  75. public ObjectId writeBlob(final byte[] b) throws IOException {
  76. try {
  77. ObjectId id = inserter.insert(OBJ_BLOB, b);
  78. inserter.flush();
  79. return id;
  80. } finally {
  81. inserter.release();
  82. }
  83. }
  84. /**
  85. * Write a blob with the data in the specified file
  86. *
  87. * @param f
  88. * a file containing blob data
  89. * @return SHA-1 of the blob
  90. * @throws IOException
  91. */
  92. public ObjectId writeBlob(final File f) throws IOException {
  93. final FileInputStream is = new FileInputStream(f);
  94. try {
  95. return writeBlob(f.length(), is);
  96. } finally {
  97. is.close();
  98. }
  99. }
  100. /**
  101. * Write a blob with data from a stream
  102. *
  103. * @param len
  104. * number of bytes to consume from the stream
  105. * @param is
  106. * stream with blob data
  107. * @return SHA-1 of the blob
  108. * @throws IOException
  109. */
  110. public ObjectId writeBlob(final long len, final InputStream is)
  111. throws IOException {
  112. try {
  113. ObjectId id = inserter.insert(OBJ_BLOB, len, is);
  114. inserter.flush();
  115. return id;
  116. } finally {
  117. inserter.release();
  118. }
  119. }
  120. /**
  121. * Write a Tree to the object database.
  122. *
  123. * @param tree
  124. * Tree
  125. * @return SHA-1 of the tree
  126. * @throws IOException
  127. */
  128. public ObjectId writeTree(Tree tree) throws IOException {
  129. try {
  130. ObjectId id = inserter.insert(OBJ_TREE, inserter.format(tree));
  131. inserter.flush();
  132. return id;
  133. } finally {
  134. inserter.release();
  135. }
  136. }
  137. /**
  138. * Write a canonical tree to the object database.
  139. *
  140. * @param treeData
  141. * the canonical encoding of the tree object.
  142. * @return SHA-1 of the tree
  143. * @throws IOException
  144. */
  145. public ObjectId writeCanonicalTree(byte[] treeData) throws IOException {
  146. try {
  147. ObjectId id = inserter.insert(OBJ_TREE, treeData);
  148. inserter.flush();
  149. return id;
  150. } finally {
  151. inserter.release();
  152. }
  153. }
  154. /**
  155. * Write a Commit to the object database
  156. *
  157. * @param commit
  158. * Commit to store
  159. * @return SHA-1 of the commit
  160. * @throws IOException
  161. */
  162. public ObjectId writeCommit(CommitBuilder commit) throws IOException {
  163. try {
  164. ObjectId id = inserter.insert(commit);
  165. inserter.flush();
  166. return id;
  167. } finally {
  168. inserter.release();
  169. }
  170. }
  171. /**
  172. * Write an annotated Tag to the object database
  173. *
  174. * @param tag
  175. * Tag
  176. * @return SHA-1 of the tag
  177. * @throws IOException
  178. */
  179. public ObjectId writeTag(TagBuilder tag) throws IOException {
  180. try {
  181. ObjectId id = inserter.insert(tag);
  182. inserter.flush();
  183. return id;
  184. } finally {
  185. inserter.release();
  186. }
  187. }
  188. /**
  189. * Compute the SHA-1 of a blob without creating an object. This is for
  190. * figuring out if we already have a blob or not.
  191. *
  192. * @param len
  193. * number of bytes to consume
  194. * @param is
  195. * stream for read blob data from
  196. * @return SHA-1 of a looked for blob
  197. * @throws IOException
  198. */
  199. public ObjectId computeBlobSha1(long len, InputStream is)
  200. throws IOException {
  201. return computeObjectSha1(OBJ_BLOB, len, is);
  202. }
  203. /**
  204. * Compute the SHA-1 of an object without actually creating an object in the
  205. * database
  206. *
  207. * @param type
  208. * kind of object
  209. * @param len
  210. * number of bytes to consume
  211. * @param is
  212. * stream for read data from
  213. * @return SHA-1 of data combined with type information
  214. * @throws IOException
  215. */
  216. public ObjectId computeObjectSha1(int type, long len, InputStream is)
  217. throws IOException {
  218. try {
  219. return inserter.idFor(type, len, is);
  220. } finally {
  221. inserter.release();
  222. }
  223. }
  224. }