LargePackedWholeObject.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc. 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.BufferedInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.zip.InflaterInputStream;

  15. import org.eclipse.jgit.errors.LargeObjectException;
  16. import org.eclipse.jgit.errors.MissingObjectException;
  17. import org.eclipse.jgit.lib.ObjectId;
  18. import org.eclipse.jgit.lib.ObjectLoader;
  19. import org.eclipse.jgit.lib.ObjectStream;

  20. class LargePackedWholeObject extends ObjectLoader {
  21.     private final int type;

  22.     private final long size;

  23.     private final long objectOffset;

  24.     private final int headerLength;

  25.     private final Pack pack;

  26.     private final FileObjectDatabase db;

  27.     LargePackedWholeObject(int type, long size, long objectOffset,
  28.             int headerLength, Pack pack, FileObjectDatabase db) {
  29.         this.type = type;
  30.         this.size = size;
  31.         this.objectOffset = objectOffset;
  32.         this.headerLength = headerLength;
  33.         this.pack = pack;
  34.         this.db = db;
  35.     }

  36.     /** {@inheritDoc} */
  37.     @Override
  38.     public int getType() {
  39.         return type;
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public long getSize() {
  44.         return size;
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public boolean isLarge() {
  49.         return true;
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public byte[] getCachedBytes() throws LargeObjectException {
  54.         try {
  55.             throw new LargeObjectException(getObjectId());
  56.         } catch (IOException cannotObtainId) {
  57.             throw new LargeObjectException(cannotObtainId);
  58.         }
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public ObjectStream openStream() throws MissingObjectException, IOException {
  63.         WindowCursor wc = new WindowCursor(db);
  64.         InputStream in;
  65.         try {
  66.             in = new PackInputStream(pack, objectOffset + headerLength, wc);
  67.         } catch (IOException packGone) {
  68.             // If the pack file cannot be pinned into the cursor, it
  69.             // probably was repacked recently. Go find the object
  70.             // again and open the stream from that location instead.
  71.             //
  72.             return wc.open(getObjectId(), type).openStream();
  73.         }

  74.         in = new BufferedInputStream( //
  75.                 new InflaterInputStream( //
  76.                         in, //
  77.                         wc.inflater(), //
  78.                         8192), //
  79.                 8192);
  80.         return new ObjectStream.Filter(type, size, in);
  81.     }

  82.     private ObjectId getObjectId() throws IOException {
  83.         return pack.findObjectForOffset(objectOffset);
  84.     }
  85. }