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