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
14 package org.eclipse.jgit.transport;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18
19 import org.eclipse.jgit.errors.NotSupportedException;
20 import org.eclipse.jgit.errors.TransportException;
21 import org.eclipse.jgit.internal.JGitText;
22 import org.eclipse.jgit.lib.Repository;
23
24 /**
25 * Single shot fetch from a streamed Git bundle.
26 * <p>
27 * The bundle is read from an unbuffered input stream, which limits the
28 * transport to opening at most one FetchConnection before needing to recreate
29 * the transport instance.
30 */
31 public class TransportBundleStream extends Transport implements TransportBundle {
32 private InputStream src;
33
34 /**
35 * Create a new transport to fetch objects from a streamed bundle.
36 * <p>
37 * The stream can be unbuffered (buffering is automatically provided
38 * internally to smooth out short reads) and unpositionable (the stream is
39 * read from only once, sequentially).
40 * <p>
41 * When the FetchConnection or the this instance is closed the supplied
42 * input stream is also automatically closed. This frees callers from
43 * needing to keep track of the supplied stream.
44 *
45 * @param db
46 * repository the fetched objects will be loaded into.
47 * @param uri
48 * symbolic name of the source of the stream. The URI can
49 * reference a non-existent resource. It is used only for
50 * exception reporting.
51 * @param in
52 * the stream to read the bundle from.
53 */
54 public TransportBundleStream(final Repository db, final URIish uri,
55 final InputStream in) {
56 super(db, uri);
57 src = in;
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 public FetchConnection openFetch() throws TransportException {
63 if (src == null)
64 throw new TransportException(uri, JGitText.get().onlyOneFetchSupported);
65 try {
66 return new BundleFetchConnection(this, src);
67 } finally {
68 src = null;
69 }
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public PushConnection openPush() throws NotSupportedException {
75 throw new NotSupportedException(
76 JGitText.get().pushIsNotSupportedForBundleTransport);
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 public void close() {
82 if (src != null) {
83 try {
84 src.close();
85 } catch (IOException err) {
86 // Ignore a close error.
87 } finally {
88 src = null;
89 }
90 }
91 }
92 }