Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PackOutputStream.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 java.io.IOException;
  46. import java.io.OutputStream;
  47. import java.security.MessageDigest;
  48. import org.eclipse.jgit.internal.JGitText;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.ProgressMonitor;
  51. import org.eclipse.jgit.util.NB;
  52. /** Custom output stream to support {@link PackWriter}. */
  53. public final class PackOutputStream extends OutputStream {
  54. private static final int BYTES_TO_WRITE_BEFORE_CANCEL_CHECK = 128 * 1024;
  55. private final ProgressMonitor writeMonitor;
  56. private final OutputStream out;
  57. private final PackWriter packWriter;
  58. private final MessageDigest md = Constants.newMessageDigest();
  59. private long count;
  60. private final byte[] headerBuffer = new byte[32];
  61. private final byte[] copyBuffer = new byte[16 << 10];
  62. private long checkCancelAt;
  63. /**
  64. * Initialize a pack output stream.
  65. * <p>
  66. * This constructor is exposed to support debugging the JGit library only.
  67. * Application or storage level code should not create a PackOutputStream,
  68. * instead use {@link PackWriter}, and let the writer create the stream.
  69. *
  70. * @param writeMonitor
  71. * monitor to update on object output progress.
  72. * @param out
  73. * target stream to receive all object contents.
  74. * @param pw
  75. * packer that is going to perform the output.
  76. */
  77. public PackOutputStream(final ProgressMonitor writeMonitor,
  78. final OutputStream out, final PackWriter pw) {
  79. this.writeMonitor = writeMonitor;
  80. this.out = out;
  81. this.packWriter = pw;
  82. this.checkCancelAt = BYTES_TO_WRITE_BEFORE_CANCEL_CHECK;
  83. }
  84. @Override
  85. public void write(final int b) throws IOException {
  86. count++;
  87. out.write(b);
  88. md.update((byte) b);
  89. }
  90. @Override
  91. public void write(final byte[] b, int off, int len)
  92. throws IOException {
  93. while (0 < len) {
  94. final int n = Math.min(len, BYTES_TO_WRITE_BEFORE_CANCEL_CHECK);
  95. count += n;
  96. if (checkCancelAt <= count) {
  97. if (writeMonitor.isCancelled()) {
  98. throw new IOException(
  99. JGitText.get().packingCancelledDuringObjectsWriting);
  100. }
  101. checkCancelAt = count + BYTES_TO_WRITE_BEFORE_CANCEL_CHECK;
  102. }
  103. out.write(b, off, n);
  104. md.update(b, off, n);
  105. off += n;
  106. len -= n;
  107. }
  108. }
  109. @Override
  110. public void flush() throws IOException {
  111. out.flush();
  112. }
  113. void writeFileHeader(int version, long objectCount) throws IOException {
  114. System.arraycopy(Constants.PACK_SIGNATURE, 0, headerBuffer, 0, 4);
  115. NB.encodeInt32(headerBuffer, 4, version);
  116. NB.encodeInt32(headerBuffer, 8, (int) objectCount);
  117. write(headerBuffer, 0, 12);
  118. }
  119. /**
  120. * Write one object.
  121. *
  122. * If the object was already written, this method does nothing and returns
  123. * quickly. This case occurs whenever an object was written out of order in
  124. * order to ensure the delta base occurred before the object that needs it.
  125. *
  126. * @param otp
  127. * the object to write.
  128. * @throws IOException
  129. * the object cannot be read from the object reader, or the
  130. * output stream is no longer accepting output. Caller must
  131. * examine the type of exception and possibly its message to
  132. * distinguish between these cases.
  133. */
  134. public void writeObject(ObjectToPack otp) throws IOException {
  135. packWriter.writeObject(this, otp);
  136. }
  137. /**
  138. * Commits the object header onto the stream.
  139. * <p>
  140. * Once the header has been written, the object representation must be fully
  141. * output, or packing must abort abnormally.
  142. *
  143. * @param otp
  144. * the object to pack. Header information is obtained.
  145. * @param rawLength
  146. * number of bytes of the inflated content. For an object that is
  147. * in whole object format, this is the same as the object size.
  148. * For an object that is in a delta format, this is the size of
  149. * the inflated delta instruction stream.
  150. * @throws IOException
  151. * the underlying stream refused to accept the header.
  152. */
  153. public void writeHeader(ObjectToPack otp, long rawLength)
  154. throws IOException {
  155. if (otp.isDeltaRepresentation()) {
  156. if (packWriter.isDeltaBaseAsOffset()) {
  157. ObjectToPack baseInPack = otp.getDeltaBase();
  158. if (baseInPack != null && baseInPack.isWritten()) {
  159. final long start = count;
  160. int n = encodeTypeSize(Constants.OBJ_OFS_DELTA, rawLength);
  161. write(headerBuffer, 0, n);
  162. long offsetDiff = start - baseInPack.getOffset();
  163. n = headerBuffer.length - 1;
  164. headerBuffer[n] = (byte) (offsetDiff & 0x7F);
  165. while ((offsetDiff >>= 7) > 0)
  166. headerBuffer[--n] = (byte) (0x80 | (--offsetDiff & 0x7F));
  167. write(headerBuffer, n, headerBuffer.length - n);
  168. return;
  169. }
  170. }
  171. int n = encodeTypeSize(Constants.OBJ_REF_DELTA, rawLength);
  172. otp.getDeltaBaseId().copyRawTo(headerBuffer, n);
  173. write(headerBuffer, 0, n + Constants.OBJECT_ID_LENGTH);
  174. } else {
  175. int n = encodeTypeSize(otp.getType(), rawLength);
  176. write(headerBuffer, 0, n);
  177. }
  178. }
  179. private int encodeTypeSize(int type, long rawLength) {
  180. long nextLength = rawLength >>> 4;
  181. headerBuffer[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  182. rawLength = nextLength;
  183. int n = 1;
  184. while (rawLength > 0) {
  185. nextLength >>>= 7;
  186. headerBuffer[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  187. rawLength = nextLength;
  188. }
  189. return n;
  190. }
  191. /** @return a temporary buffer writers can use to copy data with. */
  192. public byte[] getCopyBuffer() {
  193. return copyBuffer;
  194. }
  195. void endObject() {
  196. writeMonitor.update(1);
  197. }
  198. /** @return total number of bytes written since stream start. */
  199. public long length() {
  200. return count;
  201. }
  202. /** @return obtain the current SHA-1 digest. */
  203. byte[] getDigest() {
  204. return md.digest();
  205. }
  206. }