2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.transport
;
41 import java
.io
.IOException
;
42 import java
.io
.InputStream
;
43 import java
.util
.regex
.Matcher
;
44 import java
.util
.regex
.Pattern
;
46 import org
.spearce
.jgit
.errors
.PackProtocolException
;
47 import org
.spearce
.jgit
.errors
.TransportException
;
48 import org
.spearce
.jgit
.lib
.Constants
;
49 import org
.spearce
.jgit
.lib
.ProgressMonitor
;
50 import org
.spearce
.jgit
.util
.NB
;
51 import org
.spearce
.jgit
.util
.RawParseUtils
;
54 * Unmultiplexes the data portion of a side-band channel.
56 * Reading from this input stream obtains data from channel 1, which is
57 * typically the bulk data stream.
59 * Channel 2 is transparently unpacked and "scraped" to update a progress
60 * monitor. The scraping is performed behind the scenes as part of any of the
61 * read methods offered by this stream.
63 * Channel 3 results in an exception being thrown, as the remote side has issued
64 * an unrecoverable error.
66 * @see PacketLineIn#sideband(ProgressMonitor)
68 class SideBandInputStream
extends InputStream
{
69 static final int CH_DATA
= 1;
71 static final int CH_PROGRESS
= 2;
73 static final int CH_ERROR
= 3;
75 private static Pattern P_UNBOUNDED
= Pattern
.compile(
76 "^([\\w ]+): (\\d+)( |, done)?.*", Pattern
.DOTALL
);
78 private static Pattern P_BOUNDED
= Pattern
.compile(
79 "^([\\w ]+):.*\\((\\d+)/(\\d+)\\).*", Pattern
.DOTALL
);
81 private final PacketLineIn pckIn
;
83 private final InputStream in
;
85 private final ProgressMonitor monitor
;
87 private String progressBuffer
= "";
89 private String currentTask
;
97 private int available
;
99 SideBandInputStream(final PacketLineIn aPckIn
, final InputStream aIn
,
100 final ProgressMonitor aProgress
) {
108 public int read() throws IOException
{
117 public int read(final byte[] b
, int off
, int len
) throws IOException
{
123 final int n
= in
.read(b
, off
, Math
.min(len
, available
));
131 return eof
&& r
== 0 ?
-1 : r
;
134 private void needDataPacket() throws IOException
{
135 if (eof
|| (channel
== CH_DATA
&& available
> 0))
138 available
= pckIn
.readLength();
139 if (available
== 0) {
145 available
-= 5; // length header plus channel indicator
153 progress(readString(available
));
158 throw new TransportException("remote: " + readString(available
));
160 throw new PackProtocolException("Invalid channel " + channel
);
165 private void progress(String pkt
) {
166 pkt
= progressBuffer
+ pkt
;
168 final int lf
= pkt
.indexOf('\n');
169 final int cr
= pkt
.indexOf('\r');
171 if (0 <= lf
&& 0 <= cr
)
172 s
= Math
.min(lf
, cr
);
180 final String msg
= pkt
.substring(0, s
);
181 if (doProgressLine(msg
))
182 pkt
= pkt
.substring(s
+ 1);
186 progressBuffer
= pkt
;
189 private boolean doProgressLine(final String msg
) {
192 matcher
= P_BOUNDED
.matcher(msg
);
193 if (matcher
.matches()) {
194 final String taskname
= matcher
.group(1);
195 if (!currentTask
.equals(taskname
)) {
196 currentTask
= taskname
;
198 final int tot
= Integer
.parseInt(matcher
.group(3));
199 monitor
.beginTask(currentTask
, tot
);
201 final int cnt
= Integer
.parseInt(matcher
.group(2));
202 monitor
.update(cnt
- lastCnt
);
207 matcher
= P_UNBOUNDED
.matcher(msg
);
208 if (matcher
.matches()) {
209 final String taskname
= matcher
.group(1);
210 if (!currentTask
.equals(taskname
)) {
211 currentTask
= taskname
;
213 monitor
.beginTask(currentTask
, ProgressMonitor
.UNKNOWN
);
215 final int cnt
= Integer
.parseInt(matcher
.group(2));
216 monitor
.update(cnt
- lastCnt
);
224 private String
readString(final int len
) throws IOException
{
225 final byte[] raw
= new byte[len
];
226 NB
.readFully(in
, raw
, 0, len
);
227 return RawParseUtils
.decode(Constants
.CHARSET
, raw
, 0, len
);