View Javadoc
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  
11  package org.eclipse.jgit.transport;
12  
13  import java.io.IOException;
14  import java.io.OutputStream;
15  import java.text.MessageFormat;
16  
17  import org.eclipse.jgit.internal.JGitText;
18  
19  /**
20   * Multiplexes data and progress messages.
21   * <p>
22   * This stream is buffered at packet sizes, so the caller doesn't need to wrap
23   * it in yet another buffered stream.
24   *
25   * @since 2.0
26   */
27  public class SideBandOutputStream extends OutputStream {
28  	/** Channel used for pack data. */
29  	public static final int CH_DATA = SideBandInputStream.CH_DATA;
30  
31  	/** Channel used for progress messages. */
32  	public static final int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;
33  
34  	/** Channel used for error messages. */
35  	public static final int CH_ERROR = SideBandInputStream.CH_ERROR;
36  
37  	/** Default buffer size for a small amount of data. */
38  	public static final int SMALL_BUF = 1000;
39  
40  	/** Maximum buffer size for a single packet of sideband data. */
41  	public static final int MAX_BUF = 65520;
42  
43  	static final int HDR_SIZE = 5;
44  
45  	private final OutputStream out;
46  
47  	private final byte[] buffer;
48  
49  	/**
50  	 * Number of bytes in {@link #buffer} that are valid data.
51  	 * <p>
52  	 * Initialized to {@link #HDR_SIZE} if there is no application data in the
53  	 * buffer, as the packet header always appears at the start of the buffer.
54  	 */
55  	private int cnt;
56  
57  	/**
58  	 * Create a new stream to write side band packets.
59  	 *
60  	 * @param chan
61  	 *            channel number to prefix all packets with, so the remote side
62  	 *            can demultiplex the stream and get back the original data.
63  	 *            Must be in the range [1, 255].
64  	 * @param sz
65  	 *            maximum size of a data packet within the stream. The remote
66  	 *            side needs to agree to the packet size to prevent buffer
67  	 *            overflows. Must be in the range [HDR_SIZE + 1, MAX_BUF).
68  	 * @param os
69  	 *            stream that the packets are written onto. This stream should
70  	 *            be attached to a SideBandInputStream on the remote side.
71  	 */
72  	public SideBandOutputStream(int chan, int sz, OutputStream os) {
73  		if (chan <= 0 || chan > 255)
74  			throw new IllegalArgumentException(MessageFormat.format(
75  					JGitText.get().channelMustBeInRange1_255,
76  					Integer.valueOf(chan)));
77  		if (sz <= HDR_SIZE)
78  			throw new IllegalArgumentException(MessageFormat.format(
79  					JGitText.get().packetSizeMustBeAtLeast,
80  					Integer.valueOf(sz), Integer.valueOf(HDR_SIZE)));
81  		else if (MAX_BUF < sz)
82  			throw new IllegalArgumentException(MessageFormat.format(
83  					JGitText.get().packetSizeMustBeAtMost, Integer.valueOf(sz),
84  					Integer.valueOf(MAX_BUF)));
85  
86  		out = os;
87  		buffer = new byte[sz];
88  		buffer[4] = (byte) chan;
89  		cnt = HDR_SIZE;
90  	}
91  
92  	void flushBuffer() throws IOException {
93  		if (HDR_SIZE < cnt)
94  			writeBuffer();
95  	}
96  
97  	/** {@inheritDoc} */
98  	@Override
99  	public void flush() throws IOException {
100 		flushBuffer();
101 		out.flush();
102 	}
103 
104 	/** {@inheritDoc} */
105 	@Override
106 	public void write(byte[] b, int off, int len) throws IOException {
107 		while (0 < len) {
108 			int capacity = buffer.length - cnt;
109 			if (cnt == HDR_SIZE && capacity < len) {
110 				// Our block to write is bigger than the packet size,
111 				// stream it out as-is to avoid unnecessary copies.
112 				PacketLineOut.formatLength(buffer, buffer.length);
113 				out.write(buffer, 0, HDR_SIZE);
114 				out.write(b, off, capacity);
115 				off += capacity;
116 				len -= capacity;
117 
118 			} else {
119 				if (capacity == 0)
120 					writeBuffer();
121 
122 				int n = Math.min(len, capacity);
123 				System.arraycopy(b, off, buffer, cnt, n);
124 				cnt += n;
125 				off += n;
126 				len -= n;
127 			}
128 		}
129 	}
130 
131 	/** {@inheritDoc} */
132 	@Override
133 	public void write(int b) throws IOException {
134 		if (cnt == buffer.length)
135 			writeBuffer();
136 		buffer[cnt++] = (byte) b;
137 	}
138 
139 	private void writeBuffer() throws IOException {
140 		PacketLineOut.formatLength(buffer, cnt);
141 		out.write(buffer, 0, cnt);
142 		cnt = HDR_SIZE;
143 	}
144 }