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