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.

PackOutputStream.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.internal.storage.pack;
  45. import static org.eclipse.jgit.lib.Constants.OBJ_OFS_DELTA;
  46. import static org.eclipse.jgit.lib.Constants.OBJ_REF_DELTA;
  47. import static org.eclipse.jgit.lib.Constants.PACK_SIGNATURE;
  48. import java.io.IOException;
  49. import java.io.OutputStream;
  50. import java.security.MessageDigest;
  51. import org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ProgressMonitor;
  54. import org.eclipse.jgit.util.NB;
  55. /** Custom output stream to support {@link PackWriter}. */
  56. public final class PackOutputStream extends OutputStream {
  57. private static final int BYTES_TO_WRITE_BEFORE_CANCEL_CHECK = 128 * 1024;
  58. private final ProgressMonitor writeMonitor;
  59. private final OutputStream out;
  60. private final PackWriter packWriter;
  61. private final MessageDigest md = Constants.newMessageDigest();
  62. private long count;
  63. private final byte[] headerBuffer = new byte[32];
  64. private final byte[] copyBuffer = new byte[64 << 10];
  65. private long checkCancelAt;
  66. private boolean ofsDelta;
  67. /**
  68. * Initialize a pack output stream.
  69. * <p>
  70. * This constructor is exposed to support debugging the JGit library only.
  71. * Application or storage level code should not create a PackOutputStream,
  72. * instead use {@link PackWriter}, and let the writer create the stream.
  73. *
  74. * @param writeMonitor
  75. * monitor to update on object output progress.
  76. * @param out
  77. * target stream to receive all object contents.
  78. * @param pw
  79. * packer that is going to perform the output.
  80. */
  81. public PackOutputStream(final ProgressMonitor writeMonitor,
  82. final OutputStream out, final PackWriter pw) {
  83. this.writeMonitor = writeMonitor;
  84. this.out = out;
  85. this.packWriter = pw;
  86. this.checkCancelAt = BYTES_TO_WRITE_BEFORE_CANCEL_CHECK;
  87. }
  88. @Override
  89. public final void write(final int b) throws IOException {
  90. count++;
  91. out.write(b);
  92. md.update((byte) b);
  93. }
  94. @Override
  95. public final void write(final byte[] b, int off, int len)
  96. throws IOException {
  97. while (0 < len) {
  98. final int n = Math.min(len, BYTES_TO_WRITE_BEFORE_CANCEL_CHECK);
  99. count += n;
  100. if (checkCancelAt <= count) {
  101. if (writeMonitor.isCancelled()) {
  102. throw new IOException(
  103. JGitText.get().packingCancelledDuringObjectsWriting);
  104. }
  105. checkCancelAt = count + BYTES_TO_WRITE_BEFORE_CANCEL_CHECK;
  106. }
  107. out.write(b, off, n);
  108. md.update(b, off, n);
  109. off += n;
  110. len -= n;
  111. }
  112. }
  113. @Override
  114. public void flush() throws IOException {
  115. out.flush();
  116. }
  117. final void writeFileHeader(int version, long objectCount)
  118. throws IOException {
  119. System.arraycopy(PACK_SIGNATURE, 0, headerBuffer, 0, 4);
  120. NB.encodeInt32(headerBuffer, 4, version);
  121. NB.encodeInt32(headerBuffer, 8, (int) objectCount);
  122. write(headerBuffer, 0, 12);
  123. ofsDelta = packWriter.isDeltaBaseAsOffset();
  124. }
  125. /**
  126. * Write one object.
  127. *
  128. * If the object was already written, this method does nothing and returns
  129. * quickly. This case occurs whenever an object was written out of order in
  130. * order to ensure the delta base occurred before the object that needs it.
  131. *
  132. * @param otp
  133. * the object to write.
  134. * @throws IOException
  135. * the object cannot be read from the object reader, or the
  136. * output stream is no longer accepting output. Caller must
  137. * examine the type of exception and possibly its message to
  138. * distinguish between these cases.
  139. */
  140. public final void writeObject(ObjectToPack otp) throws IOException {
  141. packWriter.writeObject(this, otp);
  142. }
  143. /**
  144. * Commits the object header onto the stream.
  145. * <p>
  146. * Once the header has been written, the object representation must be fully
  147. * output, or packing must abort abnormally.
  148. *
  149. * @param otp
  150. * the object to pack. Header information is obtained.
  151. * @param rawLength
  152. * number of bytes of the inflated content. For an object that is
  153. * in whole object format, this is the same as the object size.
  154. * For an object that is in a delta format, this is the size of
  155. * the inflated delta instruction stream.
  156. * @throws IOException
  157. * the underlying stream refused to accept the header.
  158. */
  159. public final void writeHeader(ObjectToPack otp, long rawLength)
  160. throws IOException {
  161. ObjectToPack b = otp.getDeltaBase();
  162. if (b != null && (b.isWritten() & ofsDelta)) {
  163. int n = objectHeader(rawLength, OBJ_OFS_DELTA, headerBuffer);
  164. n = ofsDelta(count - b.getOffset(), headerBuffer, n);
  165. write(headerBuffer, 0, n);
  166. } else if (otp.isDeltaRepresentation()) {
  167. int n = objectHeader(rawLength, OBJ_REF_DELTA, headerBuffer);
  168. otp.getDeltaBaseId().copyRawTo(headerBuffer, n);
  169. write(headerBuffer, 0, n + 20);
  170. } else {
  171. int n = objectHeader(rawLength, otp.getType(), headerBuffer);
  172. write(headerBuffer, 0, n);
  173. }
  174. }
  175. private static final int objectHeader(long len, int type, byte[] buf) {
  176. byte b = (byte) ((type << 4) | (len & 0x0F));
  177. int n = 0;
  178. for (len >>>= 4; len != 0; len >>>= 7) {
  179. buf[n++] = (byte) (0x80 | b);
  180. b = (byte) (len & 0x7F);
  181. }
  182. buf[n++] = b;
  183. return n;
  184. }
  185. private static final int ofsDelta(long diff, byte[] buf, int p) {
  186. p += ofsDeltaVarIntLength(diff);
  187. int n = p;
  188. buf[--n] = (byte) (diff & 0x7F);
  189. while ((diff >>>= 7) != 0)
  190. buf[--n] = (byte) (0x80 | (--diff & 0x7F));
  191. return p;
  192. }
  193. private static final int ofsDeltaVarIntLength(long v) {
  194. int n = 1;
  195. for (; (v >>>= 7) != 0; n++)
  196. --v;
  197. return n;
  198. }
  199. /** @return a temporary buffer writers can use to copy data with. */
  200. public final byte[] getCopyBuffer() {
  201. return copyBuffer;
  202. }
  203. void endObject() {
  204. writeMonitor.update(1);
  205. }
  206. /** @return total number of bytes written since stream start. */
  207. public final long length() {
  208. return count;
  209. }
  210. /** @return obtain the current SHA-1 digest. */
  211. final byte[] getDigest() {
  212. return md.digest();
  213. }
  214. }