Refactor bundle transport to permit streaming from application
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / transport / TransportBundleStream.java
blob611ad3b518c053f2eea84f89e96d810a37a8e6c6
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.transport;
40 import java.io.IOException;
41 import java.io.InputStream;
43 import org.spearce.jgit.errors.TransportException;
44 import org.spearce.jgit.lib.Repository;
46 /**
47 * Single shot fetch from a streamed Git bundle.
48 * <p>
49 * The bundle is read from an unbuffered input stream, which limits the
50 * transport to opening at most one FetchConnection before needing to recreate
51 * the transport instance.
53 public class TransportBundleStream extends TransportBundle {
54 private InputStream src;
56 /**
57 * Create a new transport to fetch objects from a streamed bundle.
58 * <p>
59 * The stream can be unbuffered (buffering is automatically provided
60 * internally to smooth out short reads) and unpositionable (the stream is
61 * read from only once, sequentially).
62 * <p>
63 * When the FetchConnection or the this instance is closed the supplied
64 * input stream is also automatically closed. This frees callers from
65 * needing to keep track of the supplied stream.
67 * @param db
68 * repository the fetched objects will be loaded into.
69 * @param uri
70 * symbolic name of the source of the stream. The URI can
71 * reference a non-existent resource. It is used only for
72 * exception reporting.
73 * @param in
74 * the stream to read the bundle from.
76 public TransportBundleStream(final Repository db, final URIish uri,
77 final InputStream in) {
78 super(db, uri);
79 src = in;
82 @Override
83 public FetchConnection openFetch() throws TransportException {
84 if (src == null)
85 throw new TransportException(uri, "Only one fetch supported");
86 try {
87 return new BundleFetchConnection(src);
88 } finally {
89 src = null;
93 @Override
94 public void close() {
95 if (src != null) {
96 try {
97 src.close();
98 } catch (IOException err) {
99 // Ignore a close error.
100 } finally {
101 src = null;