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 @SuppressWarnings("ShortCircuitBoolean")
191 public final void writeHeader(ObjectToPack otp, long rawLength)
192 throws IOException {
193 ObjectToPack b = otp.getDeltaBase();
194 if (b != null && (b.isWritten() & ofsDelta)) { // Non-short-circuit logic is intentional
195 int n = objectHeader(rawLength, OBJ_OFS_DELTA, headerBuffer);
196 n = ofsDelta(count - b.getOffset(), headerBuffer, n);
197 write(headerBuffer, 0, n);
198 } else if (otp.isDeltaRepresentation()) {
199 int n = objectHeader(rawLength, OBJ_REF_DELTA, headerBuffer);
200 otp.getDeltaBaseId().copyRawTo(headerBuffer, n);
201 write(headerBuffer, 0, n + 20);
202 } else {
203 int n = objectHeader(rawLength, otp.getType(), headerBuffer);
204 write(headerBuffer, 0, n);
205 }
206 }
207
208 private static final int objectHeader(long len, int type, byte[] buf) {
209 byte b = (byte) ((type << 4) | (len & 0x0F));
210 int n = 0;
211 for (len >>>= 4; len != 0; len >>>= 7) {
212 buf[n++] = (byte) (0x80 | b);
213 b = (byte) (len & 0x7F);
214 }
215 buf[n++] = b;
216 return n;
217 }
218
219 private static final int ofsDelta(long diff, byte[] buf, int p) {
220 p += ofsDeltaVarIntLength(diff);
221 int n = p;
222 buf[--n] = (byte) (diff & 0x7F);
223 while ((diff >>>= 7) != 0)
224 buf[--n] = (byte) (0x80 | (--diff & 0x7F));
225 return p;
226 }
227
228 private static final int ofsDeltaVarIntLength(long v) {
229 int n = 1;
230 for (; (v >>>= 7) != 0; n++)
231 --v;
232 return n;
233 }
234
235 /**
236 * Get a temporary buffer writers can use to copy data with.
237 *
238 * @return a temporary buffer writers can use to copy data with.
239 */
240 public final byte[] getCopyBuffer() {
241 return copyBuffer;
242 }
243
244 void endObject() {
245 writeMonitor.update(1);
246 }
247
248 /**
249 * Get total number of bytes written since stream start.
250 *
251 * @return total number of bytes written since stream start.
252 */
253 public final long length() {
254 return count;
255 }
256
257 /** @return obtain the current SHA-1 digest. */
258 final byte[] getDigest() {
259 return md.digest();
260 }
261 }