Remove System.out.println from RevWalkFilterTest
[egit/chris.git] / org.spearce.jgit / src / org / spearce / jgit / transport / SideBandInputStream.java
blobc92c7311d3eb781103f625c11eade9e45757be5f
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
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
22 * written permission.
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;
53 /**
54 * Unmultiplexes the data portion of a side-band channel.
55 * <p>
56 * Reading from this input stream obtains data from channel 1, which is
57 * typically the bulk data stream.
58 * <p>
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.
62 * <p>
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;
91 private int lastCnt;
93 private boolean eof;
95 private int channel;
97 private int available;
99 SideBandInputStream(final PacketLineIn aPckIn, final InputStream aIn,
100 final ProgressMonitor aProgress) {
101 pckIn = aPckIn;
102 in = aIn;
103 monitor = aProgress;
104 currentTask = "";
107 @Override
108 public int read() throws IOException {
109 needDataPacket();
110 if (eof)
111 return -1;
112 available--;
113 return in.read();
116 @Override
117 public int read(final byte[] b, int off, int len) throws IOException {
118 int r = 0;
119 while (len > 0) {
120 needDataPacket();
121 if (eof)
122 break;
123 final int n = in.read(b, off, Math.min(len, available));
124 if (n < 0)
125 break;
126 r += n;
127 off += n;
128 len -= n;
129 available -= n;
131 return eof && r == 0 ? -1 : r;
134 private void needDataPacket() throws IOException {
135 if (eof || (channel == CH_DATA && available > 0))
136 return;
137 for (;;) {
138 available = pckIn.readLength();
139 if (available == 0) {
140 eof = true;
141 return;
144 channel = in.read();
145 available -= 5; // length header plus channel indicator
146 if (available == 0)
147 continue;
149 switch (channel) {
150 case CH_DATA:
151 return;
152 case CH_PROGRESS:
153 progress(readString(available));
155 continue;
156 case CH_ERROR:
157 eof = true;
158 throw new TransportException("remote: " + readString(available));
159 default:
160 throw new PackProtocolException("Invalid channel " + channel);
165 private void progress(String pkt) {
166 pkt = progressBuffer + pkt;
167 for (;;) {
168 final int lf = pkt.indexOf('\n');
169 final int cr = pkt.indexOf('\r');
170 final int s;
171 if (0 <= lf && 0 <= cr)
172 s = Math.min(lf, cr);
173 else if (0 <= lf)
174 s = lf;
175 else if (0 <= cr)
176 s = cr;
177 else
178 break;
180 final String msg = pkt.substring(0, s);
181 if (doProgressLine(msg))
182 pkt = pkt.substring(s + 1);
183 else
184 break;
186 progressBuffer = pkt;
189 private boolean doProgressLine(final String msg) {
190 Matcher matcher;
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;
197 lastCnt = 0;
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);
203 lastCnt = cnt;
204 return true;
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;
212 lastCnt = 0;
213 monitor.beginTask(currentTask, ProgressMonitor.UNKNOWN);
215 final int cnt = Integer.parseInt(matcher.group(2));
216 monitor.update(cnt - lastCnt);
217 lastCnt = cnt;
218 return true;
221 return false;
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);