SideBandOutputStream.java

  1. /*
  2.  * Copyright (C) 2008-2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.transport;

  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.text.MessageFormat;

  14. import org.eclipse.jgit.internal.JGitText;

  15. /**
  16.  * Multiplexes data and progress messages.
  17.  * <p>
  18.  * This stream is buffered at packet sizes, so the caller doesn't need to wrap
  19.  * it in yet another buffered stream.
  20.  *
  21.  * @since 2.0
  22.  */
  23. public class SideBandOutputStream extends OutputStream {
  24.     /** Channel used for pack data. */
  25.     public static final int CH_DATA = SideBandInputStream.CH_DATA;

  26.     /** Channel used for progress messages. */
  27.     public static final int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;

  28.     /** Channel used for error messages. */
  29.     public static final int CH_ERROR = SideBandInputStream.CH_ERROR;

  30.     /** Default buffer size for a small amount of data. */
  31.     public static final int SMALL_BUF = 1000;

  32.     /** Maximum buffer size for a single packet of sideband data. */
  33.     public static final int MAX_BUF = 65520;

  34.     static final int HDR_SIZE = 5;

  35.     private final OutputStream out;

  36.     private final byte[] buffer;

  37.     /**
  38.      * Number of bytes in {@link #buffer} that are valid data.
  39.      * <p>
  40.      * Initialized to {@link #HDR_SIZE} if there is no application data in the
  41.      * buffer, as the packet header always appears at the start of the buffer.
  42.      */
  43.     private int cnt;

  44.     /**
  45.      * Create a new stream to write side band packets.
  46.      *
  47.      * @param chan
  48.      *            channel number to prefix all packets with, so the remote side
  49.      *            can demultiplex the stream and get back the original data.
  50.      *            Must be in the range [1, 255].
  51.      * @param sz
  52.      *            maximum size of a data packet within the stream. The remote
  53.      *            side needs to agree to the packet size to prevent buffer
  54.      *            overflows. Must be in the range [HDR_SIZE + 1, MAX_BUF).
  55.      * @param os
  56.      *            stream that the packets are written onto. This stream should
  57.      *            be attached to a SideBandInputStream on the remote side.
  58.      */
  59.     public SideBandOutputStream(int chan, int sz, OutputStream os) {
  60.         if (chan <= 0 || chan > 255)
  61.             throw new IllegalArgumentException(MessageFormat.format(
  62.                     JGitText.get().channelMustBeInRange1_255,
  63.                     Integer.valueOf(chan)));
  64.         if (sz <= HDR_SIZE)
  65.             throw new IllegalArgumentException(MessageFormat.format(
  66.                     JGitText.get().packetSizeMustBeAtLeast,
  67.                     Integer.valueOf(sz), Integer.valueOf(HDR_SIZE)));
  68.         else if (MAX_BUF < sz)
  69.             throw new IllegalArgumentException(MessageFormat.format(
  70.                     JGitText.get().packetSizeMustBeAtMost, Integer.valueOf(sz),
  71.                     Integer.valueOf(MAX_BUF)));

  72.         out = os;
  73.         buffer = new byte[sz];
  74.         buffer[4] = (byte) chan;
  75.         cnt = HDR_SIZE;
  76.     }

  77.     void flushBuffer() throws IOException {
  78.         if (HDR_SIZE < cnt)
  79.             writeBuffer();
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public void flush() throws IOException {
  84.         flushBuffer();
  85.         out.flush();
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public void write(byte[] b, int off, int len) throws IOException {
  90.         while (0 < len) {
  91.             int capacity = buffer.length - cnt;
  92.             if (cnt == HDR_SIZE && capacity < len) {
  93.                 // Our block to write is bigger than the packet size,
  94.                 // stream it out as-is to avoid unnecessary copies.
  95.                 PacketLineOut.formatLength(buffer, buffer.length);
  96.                 out.write(buffer, 0, HDR_SIZE);
  97.                 out.write(b, off, capacity);
  98.                 off += capacity;
  99.                 len -= capacity;

  100.             } else {
  101.                 if (capacity == 0)
  102.                     writeBuffer();

  103.                 int n = Math.min(len, capacity);
  104.                 System.arraycopy(b, off, buffer, cnt, n);
  105.                 cnt += n;
  106.                 off += n;
  107.                 len -= n;
  108.             }
  109.         }
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     public void write(int b) throws IOException {
  114.         if (cnt == buffer.length)
  115.             writeBuffer();
  116.         buffer[cnt++] = (byte) b;
  117.     }

  118.     private void writeBuffer() throws IOException {
  119.         PacketLineOut.formatLength(buffer, cnt);
  120.         out.write(buffer, 0, cnt);
  121.         cnt = HDR_SIZE;
  122.     }
  123. }