SideBandInputStream.java

  1. /*
  2.  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.transport;

  12. import static java.nio.charset.StandardCharsets.UTF_8;
  13. import static org.eclipse.jgit.transport.SideBandOutputStream.HDR_SIZE;

  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.OutputStream;
  17. import java.io.Writer;
  18. import java.text.MessageFormat;
  19. import java.util.regex.Matcher;
  20. import java.util.regex.Pattern;

  21. import org.eclipse.jgit.errors.PackProtocolException;
  22. import org.eclipse.jgit.errors.TransportException;
  23. import org.eclipse.jgit.internal.JGitText;
  24. import org.eclipse.jgit.lib.ProgressMonitor;
  25. import org.eclipse.jgit.util.IO;
  26. import org.eclipse.jgit.util.RawParseUtils;

  27. /**
  28.  * Unmultiplexes the data portion of a side-band channel.
  29.  * <p>
  30.  * Reading from this input stream obtains data from channel 1, which is
  31.  * typically the bulk data stream.
  32.  * <p>
  33.  * Channel 2 is transparently unpacked and "scraped" to update a progress
  34.  * monitor. The scraping is performed behind the scenes as part of any of the
  35.  * read methods offered by this stream.
  36.  * <p>
  37.  * Channel 3 results in an exception being thrown, as the remote side has issued
  38.  * an unrecoverable error.
  39.  *
  40.  * @see SideBandOutputStream
  41.  * @since 4.11
  42.  */
  43. public class SideBandInputStream extends InputStream {
  44.     static final int CH_DATA = 1;
  45.     static final int CH_PROGRESS = 2;
  46.     static final int CH_ERROR = 3;

  47.     private static Pattern P_UNBOUNDED = Pattern
  48.             .compile("^([\\w ]+): +(\\d+)(?:, done\\.)? *[\r\n]$"); //$NON-NLS-1$

  49.     private static Pattern P_BOUNDED = Pattern
  50.             .compile("^([\\w ]+): +\\d+% +\\( *(\\d+)/ *(\\d+)\\)(?:, done\\.)? *[\r\n]$"); //$NON-NLS-1$

  51.     private final InputStream rawIn;

  52.     private final PacketLineIn pckIn;

  53.     private final ProgressMonitor monitor;

  54.     private final Writer messages;

  55.     private final OutputStream out;

  56.     private String progressBuffer = ""; //$NON-NLS-1$

  57.     private String currentTask;

  58.     private int lastCnt;

  59.     private boolean eof;

  60.     private int channel;

  61.     private int available;

  62.     SideBandInputStream(final InputStream in, final ProgressMonitor progress,
  63.             final Writer messageStream, OutputStream outputStream) {
  64.         rawIn = in;
  65.         pckIn = new PacketLineIn(rawIn);
  66.         monitor = progress;
  67.         messages = messageStream;
  68.         currentTask = ""; //$NON-NLS-1$
  69.         out = outputStream;
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public int read() throws IOException {
  74.         needDataPacket();
  75.         if (eof)
  76.             return -1;
  77.         available--;
  78.         return rawIn.read();
  79.     }

  80.     /** {@inheritDoc} */
  81.     @Override
  82.     public int read(byte[] b, int off, int len) throws IOException {
  83.         int r = 0;
  84.         while (len > 0) {
  85.             needDataPacket();
  86.             if (eof)
  87.                 break;
  88.             final int n = rawIn.read(b, off, Math.min(len, available));
  89.             if (n < 0)
  90.                 break;
  91.             r += n;
  92.             off += n;
  93.             len -= n;
  94.             available -= n;
  95.         }
  96.         return eof && r == 0 ? -1 : r;
  97.     }

  98.     private void needDataPacket() throws IOException {
  99.         if (eof || (channel == CH_DATA && available > 0))
  100.             return;
  101.         for (;;) {
  102.             available = pckIn.readLength();
  103.             if (available == 0) {
  104.                 eof = true;
  105.                 return;
  106.             }

  107.             channel = rawIn.read() & 0xff;
  108.             available -= HDR_SIZE; // length header plus channel indicator
  109.             if (available == 0)
  110.                 continue;

  111.             switch (channel) {
  112.             case CH_DATA:
  113.                 return;
  114.             case CH_PROGRESS:
  115.                 progress(readString(available));
  116.                 continue;
  117.             case CH_ERROR:
  118.                 eof = true;
  119.                 throw new TransportException(remote(readString(available)));
  120.             default:
  121.                 throw new PackProtocolException(
  122.                         MessageFormat.format(JGitText.get().invalidChannel,
  123.                                 Integer.valueOf(channel)));
  124.             }
  125.         }
  126.     }

  127.     private void progress(String pkt) throws IOException {
  128.         pkt = progressBuffer + pkt;
  129.         for (;;) {
  130.             final int lf = pkt.indexOf('\n');
  131.             final int cr = pkt.indexOf('\r');
  132.             final int s;
  133.             if (0 <= lf && 0 <= cr)
  134.                 s = Math.min(lf, cr);
  135.             else if (0 <= lf)
  136.                 s = lf;
  137.             else if (0 <= cr)
  138.                 s = cr;
  139.             else
  140.                 break;

  141.             doProgressLine(pkt.substring(0, s + 1));
  142.             pkt = pkt.substring(s + 1);
  143.         }
  144.         progressBuffer = pkt;
  145.     }

  146.     private void doProgressLine(String msg) throws IOException {
  147.         Matcher matcher;

  148.         matcher = P_BOUNDED.matcher(msg);
  149.         if (matcher.matches()) {
  150.             final String taskname = matcher.group(1);
  151.             if (!currentTask.equals(taskname)) {
  152.                 currentTask = taskname;
  153.                 lastCnt = 0;
  154.                 beginTask(Integer.parseInt(matcher.group(3)));
  155.             }
  156.             final int cnt = Integer.parseInt(matcher.group(2));
  157.             monitor.update(cnt - lastCnt);
  158.             lastCnt = cnt;
  159.             return;
  160.         }

  161.         matcher = P_UNBOUNDED.matcher(msg);
  162.         if (matcher.matches()) {
  163.             final String taskname = matcher.group(1);
  164.             if (!currentTask.equals(taskname)) {
  165.                 currentTask = taskname;
  166.                 lastCnt = 0;
  167.                 beginTask(ProgressMonitor.UNKNOWN);
  168.             }
  169.             final int cnt = Integer.parseInt(matcher.group(2));
  170.             monitor.update(cnt - lastCnt);
  171.             lastCnt = cnt;
  172.             return;
  173.         }

  174.         messages.write(msg);
  175.         if (out != null)
  176.             out.write(msg.getBytes(UTF_8));
  177.     }

  178.     private void beginTask(int totalWorkUnits) {
  179.         monitor.beginTask(remote(currentTask), totalWorkUnits);
  180.     }

  181.     private static String remote(String msg) {
  182.         String prefix = JGitText.get().prefixRemote;
  183.         StringBuilder r = new StringBuilder(prefix.length() + msg.length() + 1);
  184.         r.append(prefix);
  185.         if (prefix.length() > 0 && prefix.charAt(prefix.length() - 1) != ' ') {
  186.             r.append(' ');
  187.         }
  188.         r.append(msg);
  189.         return r.toString();
  190.     }

  191.     private String readString(int len) throws IOException {
  192.         final byte[] raw = new byte[len];
  193.         IO.readFully(rawIn, raw, 0, len);
  194.         return RawParseUtils.decode(UTF_8, raw, 0, len);
  195.     }
  196. }