View Javadoc
1   /*
2    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import java.io.IOException;
15  import java.io.OutputStream;
16  
17  import org.eclipse.jgit.internal.JGitText;
18  import org.eclipse.jgit.transport.PackedObjectInfo;
19  import org.eclipse.jgit.util.NB;
20  
21  /**
22   * Creates the version 1 (old style) pack table of contents files.
23   *
24   * @see PackIndexWriter
25   * @see PackIndexV1
26   */
27  class PackIndexWriterV1 extends PackIndexWriter {
28  	static boolean canStore(PackedObjectInfo oe) {
29  		// We are limited to 4 GB per pack as offset is 32 bit unsigned int.
30  		//
31  		return oe.getOffset() >>> 1 < Integer.MAX_VALUE;
32  	}
33  
34  	PackIndexWriterV1(final OutputStream dst) {
35  		super(dst);
36  	}
37  
38  	/** {@inheritDoc} */
39  	@Override
40  	protected void writeImpl() throws IOException {
41  		writeFanOutTable();
42  
43  		for (PackedObjectInfo oe : entries) {
44  			if (!canStore(oe))
45  				throw new IOException(JGitText.get().packTooLargeForIndexVersion1);
46  			NB.encodeInt32(tmp, 0, (int) oe.getOffset());
47  			oe.copyRawTo(tmp, 4);
48  			out.write(tmp);
49  		}
50  
51  		writeChecksumFooter();
52  	}
53  }