1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.eclipse.jgit.transport;
46
47 import static java.nio.charset.StandardCharsets.UTF_8;
48 import static org.eclipse.jgit.transport.SideBandOutputStream.HDR_SIZE;
49
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.OutputStream;
53 import java.io.Writer;
54 import java.text.MessageFormat;
55 import java.util.regex.Matcher;
56 import java.util.regex.Pattern;
57
58 import org.eclipse.jgit.errors.PackProtocolException;
59 import org.eclipse.jgit.errors.TransportException;
60 import org.eclipse.jgit.internal.JGitText;
61 import org.eclipse.jgit.lib.ProgressMonitor;
62 import org.eclipse.jgit.util.IO;
63 import org.eclipse.jgit.util.RawParseUtils;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public class SideBandInputStream extends InputStream {
82 static final int CH_DATA = 1;
83 static final int CH_PROGRESS = 2;
84 static final int CH_ERROR = 3;
85
86 private static Pattern P_UNBOUNDED = Pattern
87 .compile("^([\\w ]+): +(\\d+)(?:, done\\.)? *[\r\n]$");
88
89 private static Pattern P_BOUNDED = Pattern
90 .compile("^([\\w ]+): +\\d+% +\\( *(\\d+)/ *(\\d+)\\)(?:, done\\.)? *[\r\n]$");
91
92 private final InputStream rawIn;
93
94 private final PacketLineIn pckIn;
95
96 private final ProgressMonitor monitor;
97
98 private final Writer messages;
99
100 private final OutputStream out;
101
102 private String progressBuffer = "";
103
104 private String currentTask;
105
106 private int lastCnt;
107
108 private boolean eof;
109
110 private int channel;
111
112 private int available;
113
114 SideBandInputStream(final InputStream in, final ProgressMonitor progress,
115 final Writer messageStream, OutputStream outputStream) {
116 rawIn = in;
117 pckIn = new PacketLineIn(rawIn);
118 monitor = progress;
119 messages = messageStream;
120 currentTask = "";
121 out = outputStream;
122 }
123
124
125 @Override
126 public int read() throws IOException {
127 needDataPacket();
128 if (eof)
129 return -1;
130 available--;
131 return rawIn.read();
132 }
133
134
135 @Override
136 public int read(byte[] b, int off, int len) throws IOException {
137 int r = 0;
138 while (len > 0) {
139 needDataPacket();
140 if (eof)
141 break;
142 final int n = rawIn.read(b, off, Math.min(len, available));
143 if (n < 0)
144 break;
145 r += n;
146 off += n;
147 len -= n;
148 available -= n;
149 }
150 return eof && r == 0 ? -1 : r;
151 }
152
153 private void needDataPacket() throws IOException {
154 if (eof || (channel == CH_DATA && available > 0))
155 return;
156 for (;;) {
157 available = pckIn.readLength();
158 if (available == 0) {
159 eof = true;
160 return;
161 }
162
163 channel = rawIn.read() & 0xff;
164 available -= HDR_SIZE;
165 if (available == 0)
166 continue;
167
168 switch (channel) {
169 case CH_DATA:
170 return;
171 case CH_PROGRESS:
172 progress(readString(available));
173 continue;
174 case CH_ERROR:
175 eof = true;
176 throw new TransportException(remote(readString(available)));
177 default:
178 throw new PackProtocolException(
179 MessageFormat.format(JGitText.get().invalidChannel,
180 Integer.valueOf(channel)));
181 }
182 }
183 }
184
185 private void progress(String pkt) throws IOException {
186 pkt = progressBuffer + pkt;
187 for (;;) {
188 final int lf = pkt.indexOf('\n');
189 final int cr = pkt.indexOf('\r');
190 final int s;
191 if (0 <= lf && 0 <= cr)
192 s = Math.min(lf, cr);
193 else if (0 <= lf)
194 s = lf;
195 else if (0 <= cr)
196 s = cr;
197 else
198 break;
199
200 doProgressLine(pkt.substring(0, s + 1));
201 pkt = pkt.substring(s + 1);
202 }
203 progressBuffer = pkt;
204 }
205
206 private void doProgressLine(String msg) throws IOException {
207 Matcher matcher;
208
209 matcher = P_BOUNDED.matcher(msg);
210 if (matcher.matches()) {
211 final String taskname = matcher.group(1);
212 if (!currentTask.equals(taskname)) {
213 currentTask = taskname;
214 lastCnt = 0;
215 beginTask(Integer.parseInt(matcher.group(3)));
216 }
217 final int cnt = Integer.parseInt(matcher.group(2));
218 monitor.update(cnt - lastCnt);
219 lastCnt = cnt;
220 return;
221 }
222
223 matcher = P_UNBOUNDED.matcher(msg);
224 if (matcher.matches()) {
225 final String taskname = matcher.group(1);
226 if (!currentTask.equals(taskname)) {
227 currentTask = taskname;
228 lastCnt = 0;
229 beginTask(ProgressMonitor.UNKNOWN);
230 }
231 final int cnt = Integer.parseInt(matcher.group(2));
232 monitor.update(cnt - lastCnt);
233 lastCnt = cnt;
234 return;
235 }
236
237 messages.write(msg);
238 if (out != null)
239 out.write(msg.getBytes(UTF_8));
240 }
241
242 private void beginTask(int totalWorkUnits) {
243 monitor.beginTask(remote(currentTask), totalWorkUnits);
244 }
245
246 private static String remote(String msg) {
247 String prefix = JGitText.get().prefixRemote;
248 StringBuilder r = new StringBuilder(prefix.length() + msg.length() + 1);
249 r.append(prefix);
250 if (prefix.length() > 0 && prefix.charAt(prefix.length() - 1) != ' ') {
251 r.append(' ');
252 }
253 r.append(msg);
254 return r.toString();
255 }
256
257 private String readString(int len) throws IOException {
258 final byte[] raw = new byte[len];
259 IO.readFully(rawIn, raw, 0, len);
260 return RawParseUtils.decode(UTF_8, raw, 0, len);
261 }
262 }