TransportBundleStream.java

  1. /*
  2.  * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  3.  * Copyright (C) 2008, Google Inc.
  4.  * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  5.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  6.  *
  7.  * This program and the accompanying materials are made available under the
  8.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  9.  * https://www.eclipse.org/org/documents/edl-v10.php.
  10.  *
  11.  * SPDX-License-Identifier: BSD-3-Clause
  12.  */

  13. package org.eclipse.jgit.transport;

  14. import java.io.IOException;
  15. import java.io.InputStream;

  16. import org.eclipse.jgit.errors.NotSupportedException;
  17. import org.eclipse.jgit.errors.TransportException;
  18. import org.eclipse.jgit.internal.JGitText;
  19. import org.eclipse.jgit.lib.Repository;

  20. /**
  21.  * Single shot fetch from a streamed Git bundle.
  22.  * <p>
  23.  * The bundle is read from an unbuffered input stream, which limits the
  24.  * transport to opening at most one FetchConnection before needing to recreate
  25.  * the transport instance.
  26.  */
  27. public class TransportBundleStream extends Transport implements TransportBundle {
  28.     private InputStream src;

  29.     /**
  30.      * Create a new transport to fetch objects from a streamed bundle.
  31.      * <p>
  32.      * The stream can be unbuffered (buffering is automatically provided
  33.      * internally to smooth out short reads) and unpositionable (the stream is
  34.      * read from only once, sequentially).
  35.      * <p>
  36.      * When the FetchConnection or the this instance is closed the supplied
  37.      * input stream is also automatically closed. This frees callers from
  38.      * needing to keep track of the supplied stream.
  39.      *
  40.      * @param db
  41.      *            repository the fetched objects will be loaded into.
  42.      * @param uri
  43.      *            symbolic name of the source of the stream. The URI can
  44.      *            reference a non-existent resource. It is used only for
  45.      *            exception reporting.
  46.      * @param in
  47.      *            the stream to read the bundle from.
  48.      */
  49.     public TransportBundleStream(final Repository db, final URIish uri,
  50.             final InputStream in) {
  51.         super(db, uri);
  52.         src = in;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public FetchConnection openFetch() throws TransportException {
  57.         if (src == null)
  58.             throw new TransportException(uri, JGitText.get().onlyOneFetchSupported);
  59.         try {
  60.             return new BundleFetchConnection(this, src);
  61.         } finally {
  62.             src = null;
  63.         }
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public PushConnection openPush() throws NotSupportedException {
  68.         throw new NotSupportedException(
  69.                 JGitText.get().pushIsNotSupportedForBundleTransport);
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public void close() {
  74.         if (src != null) {
  75.             try {
  76.                 src.close();
  77.             } catch (IOException err) {
  78.                 // Ignore a close error.
  79.             } finally {
  80.                 src = null;
  81.             }
  82.         }
  83.     }
  84. }