You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SideBandInputStream.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.transport;
  45. import static java.nio.charset.StandardCharsets.UTF_8;
  46. import static org.eclipse.jgit.transport.SideBandOutputStream.HDR_SIZE;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.io.Writer;
  51. import java.text.MessageFormat;
  52. import java.util.regex.Matcher;
  53. import java.util.regex.Pattern;
  54. import org.eclipse.jgit.errors.PackProtocolException;
  55. import org.eclipse.jgit.errors.TransportException;
  56. import org.eclipse.jgit.internal.JGitText;
  57. import org.eclipse.jgit.lib.ProgressMonitor;
  58. import org.eclipse.jgit.util.IO;
  59. import org.eclipse.jgit.util.RawParseUtils;
  60. /**
  61. * Unmultiplexes the data portion of a side-band channel.
  62. * <p>
  63. * Reading from this input stream obtains data from channel 1, which is
  64. * typically the bulk data stream.
  65. * <p>
  66. * Channel 2 is transparently unpacked and "scraped" to update a progress
  67. * monitor. The scraping is performed behind the scenes as part of any of the
  68. * read methods offered by this stream.
  69. * <p>
  70. * Channel 3 results in an exception being thrown, as the remote side has issued
  71. * an unrecoverable error.
  72. *
  73. * @see SideBandOutputStream
  74. * @since 4.11
  75. */
  76. public class SideBandInputStream extends InputStream {
  77. static final int CH_DATA = 1;
  78. static final int CH_PROGRESS = 2;
  79. static final int CH_ERROR = 3;
  80. private static Pattern P_UNBOUNDED = Pattern
  81. .compile("^([\\w ]+): +(\\d+)(?:, done\\.)? *[\r\n]$"); //$NON-NLS-1$
  82. private static Pattern P_BOUNDED = Pattern
  83. .compile("^([\\w ]+): +\\d+% +\\( *(\\d+)/ *(\\d+)\\)(?:, done\\.)? *[\r\n]$"); //$NON-NLS-1$
  84. private final InputStream rawIn;
  85. private final PacketLineIn pckIn;
  86. private final ProgressMonitor monitor;
  87. private final Writer messages;
  88. private final OutputStream out;
  89. private String progressBuffer = ""; //$NON-NLS-1$
  90. private String currentTask;
  91. private int lastCnt;
  92. private boolean eof;
  93. private int channel;
  94. private int available;
  95. SideBandInputStream(final InputStream in, final ProgressMonitor progress,
  96. final Writer messageStream, OutputStream outputStream) {
  97. rawIn = in;
  98. pckIn = new PacketLineIn(rawIn);
  99. monitor = progress;
  100. messages = messageStream;
  101. currentTask = ""; //$NON-NLS-1$
  102. out = outputStream;
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public int read() throws IOException {
  107. needDataPacket();
  108. if (eof)
  109. return -1;
  110. available--;
  111. return rawIn.read();
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public int read(byte[] b, int off, int len) throws IOException {
  116. int r = 0;
  117. while (len > 0) {
  118. needDataPacket();
  119. if (eof)
  120. break;
  121. final int n = rawIn.read(b, off, Math.min(len, available));
  122. if (n < 0)
  123. break;
  124. r += n;
  125. off += n;
  126. len -= n;
  127. available -= n;
  128. }
  129. return eof && r == 0 ? -1 : r;
  130. }
  131. private void needDataPacket() throws IOException {
  132. if (eof || (channel == CH_DATA && available > 0))
  133. return;
  134. for (;;) {
  135. available = pckIn.readLength();
  136. if (available == 0) {
  137. eof = true;
  138. return;
  139. }
  140. channel = rawIn.read() & 0xff;
  141. available -= HDR_SIZE; // length header plus channel indicator
  142. if (available == 0)
  143. continue;
  144. switch (channel) {
  145. case CH_DATA:
  146. return;
  147. case CH_PROGRESS:
  148. progress(readString(available));
  149. continue;
  150. case CH_ERROR:
  151. eof = true;
  152. throw new TransportException(remote(readString(available)));
  153. default:
  154. throw new PackProtocolException(
  155. MessageFormat.format(JGitText.get().invalidChannel,
  156. Integer.valueOf(channel)));
  157. }
  158. }
  159. }
  160. private void progress(String pkt) throws IOException {
  161. pkt = progressBuffer + pkt;
  162. for (;;) {
  163. final int lf = pkt.indexOf('\n');
  164. final int cr = pkt.indexOf('\r');
  165. final int s;
  166. if (0 <= lf && 0 <= cr)
  167. s = Math.min(lf, cr);
  168. else if (0 <= lf)
  169. s = lf;
  170. else if (0 <= cr)
  171. s = cr;
  172. else
  173. break;
  174. doProgressLine(pkt.substring(0, s + 1));
  175. pkt = pkt.substring(s + 1);
  176. }
  177. progressBuffer = pkt;
  178. }
  179. private void doProgressLine(String msg) throws IOException {
  180. Matcher matcher;
  181. matcher = P_BOUNDED.matcher(msg);
  182. if (matcher.matches()) {
  183. final String taskname = matcher.group(1);
  184. if (!currentTask.equals(taskname)) {
  185. currentTask = taskname;
  186. lastCnt = 0;
  187. beginTask(Integer.parseInt(matcher.group(3)));
  188. }
  189. final int cnt = Integer.parseInt(matcher.group(2));
  190. monitor.update(cnt - lastCnt);
  191. lastCnt = cnt;
  192. return;
  193. }
  194. matcher = P_UNBOUNDED.matcher(msg);
  195. if (matcher.matches()) {
  196. final String taskname = matcher.group(1);
  197. if (!currentTask.equals(taskname)) {
  198. currentTask = taskname;
  199. lastCnt = 0;
  200. beginTask(ProgressMonitor.UNKNOWN);
  201. }
  202. final int cnt = Integer.parseInt(matcher.group(2));
  203. monitor.update(cnt - lastCnt);
  204. lastCnt = cnt;
  205. return;
  206. }
  207. messages.write(msg);
  208. if (out != null)
  209. out.write(msg.getBytes());
  210. }
  211. private void beginTask(int totalWorkUnits) {
  212. monitor.beginTask(remote(currentTask), totalWorkUnits);
  213. }
  214. private static String remote(String msg) {
  215. String prefix = JGitText.get().prefixRemote;
  216. StringBuilder r = new StringBuilder(prefix.length() + msg.length() + 1);
  217. r.append(prefix);
  218. if (prefix.length() > 0 && prefix.charAt(prefix.length() - 1) != ' ') {
  219. r.append(' ');
  220. }
  221. r.append(msg);
  222. return r.toString();
  223. }
  224. private String readString(int len) throws IOException {
  225. final byte[] raw = new byte[len];
  226. IO.readFully(rawIn, raw, 0, len);
  227. return RawParseUtils.decode(UTF_8, raw, 0, len);
  228. }
  229. }