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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 org.eclipse.jgit.transport.SideBandOutputStream.HDR_SIZE;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.io.Writer;
  49. import java.util.regex.Matcher;
  50. import java.util.regex.Pattern;
  51. import org.eclipse.jgit.errors.PackProtocolException;
  52. import org.eclipse.jgit.errors.TransportException;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.ProgressMonitor;
  55. import org.eclipse.jgit.util.IO;
  56. import org.eclipse.jgit.util.RawParseUtils;
  57. /**
  58. * Unmultiplexes the data portion of a side-band channel.
  59. * <p>
  60. * Reading from this input stream obtains data from channel 1, which is
  61. * typically the bulk data stream.
  62. * <p>
  63. * Channel 2 is transparently unpacked and "scraped" to update a progress
  64. * monitor. The scraping is performed behind the scenes as part of any of the
  65. * read methods offered by this stream.
  66. * <p>
  67. * Channel 3 results in an exception being thrown, as the remote side has issued
  68. * an unrecoverable error.
  69. *
  70. * @see SideBandOutputStream
  71. */
  72. class SideBandInputStream extends InputStream {
  73. private static final String PFX_REMOTE = "remote: ";
  74. static final int CH_DATA = 1;
  75. static final int CH_PROGRESS = 2;
  76. static final int CH_ERROR = 3;
  77. private static Pattern P_UNBOUNDED = Pattern
  78. .compile("^([\\w ]+): +(\\d+)(?:, done\\.)? *[\r\n]$");
  79. private static Pattern P_BOUNDED = Pattern
  80. .compile("^([\\w ]+): +\\d+% +\\( *(\\d+)/ *(\\d+)\\)(?:, done\\.)? *[\r\n]$");
  81. private final InputStream rawIn;
  82. private final PacketLineIn pckIn;
  83. private final ProgressMonitor monitor;
  84. private final Writer messages;
  85. private String progressBuffer = "";
  86. private String currentTask;
  87. private int lastCnt;
  88. private boolean eof;
  89. private int channel;
  90. private int available;
  91. SideBandInputStream(final InputStream in, final ProgressMonitor progress,
  92. final Writer messageStream) {
  93. rawIn = in;
  94. pckIn = new PacketLineIn(rawIn);
  95. monitor = progress;
  96. messages = messageStream;
  97. currentTask = "";
  98. }
  99. @Override
  100. public int read() throws IOException {
  101. needDataPacket();
  102. if (eof)
  103. return -1;
  104. available--;
  105. return rawIn.read();
  106. }
  107. @Override
  108. public int read(final byte[] b, int off, int len) throws IOException {
  109. int r = 0;
  110. while (len > 0) {
  111. needDataPacket();
  112. if (eof)
  113. break;
  114. final int n = rawIn.read(b, off, Math.min(len, available));
  115. if (n < 0)
  116. break;
  117. r += n;
  118. off += n;
  119. len -= n;
  120. available -= n;
  121. }
  122. return eof && r == 0 ? -1 : r;
  123. }
  124. private void needDataPacket() throws IOException {
  125. if (eof || (channel == CH_DATA && available > 0))
  126. return;
  127. for (;;) {
  128. available = pckIn.readLength();
  129. if (available == 0) {
  130. eof = true;
  131. return;
  132. }
  133. channel = rawIn.read() & 0xff;
  134. available -= HDR_SIZE; // length header plus channel indicator
  135. if (available == 0)
  136. continue;
  137. switch (channel) {
  138. case CH_DATA:
  139. return;
  140. case CH_PROGRESS:
  141. progress(readString(available));
  142. continue;
  143. case CH_ERROR:
  144. eof = true;
  145. throw new TransportException(PFX_REMOTE + readString(available));
  146. default:
  147. throw new PackProtocolException("Invalid channel " + channel);
  148. }
  149. }
  150. }
  151. private void progress(String pkt) throws IOException {
  152. pkt = progressBuffer + pkt;
  153. for (;;) {
  154. final int lf = pkt.indexOf('\n');
  155. final int cr = pkt.indexOf('\r');
  156. final int s;
  157. if (0 <= lf && 0 <= cr)
  158. s = Math.min(lf, cr);
  159. else if (0 <= lf)
  160. s = lf;
  161. else if (0 <= cr)
  162. s = cr;
  163. else
  164. break;
  165. doProgressLine(pkt.substring(0, s + 1));
  166. pkt = pkt.substring(s + 1);
  167. }
  168. progressBuffer = pkt;
  169. }
  170. private void doProgressLine(final String msg) throws IOException {
  171. Matcher matcher;
  172. matcher = P_BOUNDED.matcher(msg);
  173. if (matcher.matches()) {
  174. final String taskname = matcher.group(1);
  175. if (!currentTask.equals(taskname)) {
  176. currentTask = taskname;
  177. lastCnt = 0;
  178. beginTask(Integer.parseInt(matcher.group(3)));
  179. }
  180. final int cnt = Integer.parseInt(matcher.group(2));
  181. monitor.update(cnt - lastCnt);
  182. lastCnt = cnt;
  183. return;
  184. }
  185. matcher = P_UNBOUNDED.matcher(msg);
  186. if (matcher.matches()) {
  187. final String taskname = matcher.group(1);
  188. if (!currentTask.equals(taskname)) {
  189. currentTask = taskname;
  190. lastCnt = 0;
  191. beginTask(ProgressMonitor.UNKNOWN);
  192. }
  193. final int cnt = Integer.parseInt(matcher.group(2));
  194. monitor.update(cnt - lastCnt);
  195. lastCnt = cnt;
  196. return;
  197. }
  198. messages.write(msg);
  199. }
  200. private void beginTask(final int totalWorkUnits) {
  201. monitor.beginTask(PFX_REMOTE + currentTask, totalWorkUnits);
  202. }
  203. private String readString(final int len) throws IOException {
  204. final byte[] raw = new byte[len];
  205. IO.readFully(rawIn, raw, 0, len);
  206. return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
  207. }
  208. }