PackIndexWriterV2.java

  1. /*
  2.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.file;

  11. import java.io.IOException;
  12. import java.io.OutputStream;

  13. import org.eclipse.jgit.transport.PackedObjectInfo;
  14. import org.eclipse.jgit.util.NB;

  15. /**
  16.  * Creates the version 2 pack table of contents files.
  17.  *
  18.  * @see PackIndexWriter
  19.  * @see PackIndexV2
  20.  */
  21. class PackIndexWriterV2 extends PackIndexWriter {
  22.     private static final int MAX_OFFSET_32 = 0x7fffffff;
  23.     private static final int IS_OFFSET_64 = 0x80000000;

  24.     PackIndexWriterV2(final OutputStream dst) {
  25.         super(dst);
  26.     }

  27.     /** {@inheritDoc} */
  28.     @Override
  29.     protected void writeImpl() throws IOException {
  30.         writeTOC(2);
  31.         writeFanOutTable();
  32.         writeObjectNames();
  33.         writeCRCs();
  34.         writeOffset32();
  35.         writeOffset64();
  36.         writeChecksumFooter();
  37.     }

  38.     private void writeObjectNames() throws IOException {
  39.         for (PackedObjectInfo oe : entries)
  40.             oe.copyRawTo(out);
  41.     }

  42.     private void writeCRCs() throws IOException {
  43.         for (PackedObjectInfo oe : entries) {
  44.             NB.encodeInt32(tmp, 0, oe.getCRC());
  45.             out.write(tmp, 0, 4);
  46.         }
  47.     }

  48.     private void writeOffset32() throws IOException {
  49.         int o64 = 0;
  50.         for (PackedObjectInfo oe : entries) {
  51.             final long o = oe.getOffset();
  52.             if (o <= MAX_OFFSET_32)
  53.                 NB.encodeInt32(tmp, 0, (int) o);
  54.             else
  55.                 NB.encodeInt32(tmp, 0, IS_OFFSET_64 | o64++);
  56.             out.write(tmp, 0, 4);
  57.         }
  58.     }

  59.     private void writeOffset64() throws IOException {
  60.         for (PackedObjectInfo oe : entries) {
  61.             final long o = oe.getOffset();
  62.             if (MAX_OFFSET_32 < o) {
  63.                 NB.encodeInt64(tmp, 0, o);
  64.                 out.write(tmp, 0, 8);
  65.             }
  66.         }
  67.     }
  68. }