1 /*
2 * Copyright (C) 2008-2010, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.transport;
45
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.text.MessageFormat;
49
50 import org.eclipse.jgit.internal.JGitText;
51
52 /**
53 * Multiplexes data and progress messages.
54 * <p>
55 * This stream is buffered at packet sizes, so the caller doesn't need to wrap
56 * it in yet another buffered stream.
57 *
58 * @since 2.0
59 */
60 public class SideBandOutputStream extends OutputStream {
61 /** Channel used for pack data. */
62 public static final int CH_DATA = SideBandInputStream.CH_DATA;
63
64 /** Channel used for progress messages. */
65 public static final int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;
66
67 /** Channel used for error messages. */
68 public static final int CH_ERROR = SideBandInputStream.CH_ERROR;
69
70 /** Default buffer size for a small amount of data. */
71 public static final int SMALL_BUF = 1000;
72
73 /** Maximum buffer size for a single packet of sideband data. */
74 public static final int MAX_BUF = 65520;
75
76 static final int HDR_SIZE = 5;
77
78 private final OutputStream out;
79
80 private final byte[] buffer;
81
82 /**
83 * Number of bytes in {@link #buffer} that are valid data.
84 * <p>
85 * Initialized to {@link #HDR_SIZE} if there is no application data in the
86 * buffer, as the packet header always appears at the start of the buffer.
87 */
88 private int cnt;
89
90 /**
91 * Create a new stream to write side band packets.
92 *
93 * @param chan
94 * channel number to prefix all packets with, so the remote side
95 * can demultiplex the stream and get back the original data.
96 * Must be in the range [1, 255].
97 * @param sz
98 * maximum size of a data packet within the stream. The remote
99 * side needs to agree to the packet size to prevent buffer
100 * overflows. Must be in the range [HDR_SIZE + 1, MAX_BUF).
101 * @param os
102 * stream that the packets are written onto. This stream should
103 * be attached to a SideBandInputStream on the remote side.
104 */
105 public SideBandOutputStream(int chan, int sz, OutputStream os) {
106 if (chan <= 0 || chan > 255)
107 throw new IllegalArgumentException(MessageFormat.format(
108 JGitText.get().channelMustBeInRange1_255,
109 Integer.valueOf(chan)));
110 if (sz <= HDR_SIZE)
111 throw new IllegalArgumentException(MessageFormat.format(
112 JGitText.get().packetSizeMustBeAtLeast,
113 Integer.valueOf(sz), Integer.valueOf(HDR_SIZE)));
114 else if (MAX_BUF < sz)
115 throw new IllegalArgumentException(MessageFormat.format(
116 JGitText.get().packetSizeMustBeAtMost, Integer.valueOf(sz),
117 Integer.valueOf(MAX_BUF)));
118
119 out = os;
120 buffer = new byte[sz];
121 buffer[4] = (byte) chan;
122 cnt = HDR_SIZE;
123 }
124
125 void flushBuffer() throws IOException {
126 if (HDR_SIZE < cnt)
127 writeBuffer();
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 public void flush() throws IOException {
133 flushBuffer();
134 out.flush();
135 }
136
137 /** {@inheritDoc} */
138 @Override
139 public void write(byte[] b, int off, int len) throws IOException {
140 while (0 < len) {
141 int capacity = buffer.length - cnt;
142 if (cnt == HDR_SIZE && capacity < len) {
143 // Our block to write is bigger than the packet size,
144 // stream it out as-is to avoid unnecessary copies.
145 PacketLineOut.formatLength(buffer, buffer.length);
146 out.write(buffer, 0, HDR_SIZE);
147 out.write(b, off, capacity);
148 off += capacity;
149 len -= capacity;
150
151 } else {
152 if (capacity == 0)
153 writeBuffer();
154
155 int n = Math.min(len, capacity);
156 System.arraycopy(b, off, buffer, cnt, n);
157 cnt += n;
158 off += n;
159 len -= n;
160 }
161 }
162 }
163
164 /** {@inheritDoc} */
165 @Override
166 public void write(int b) throws IOException {
167 if (cnt == buffer.length)
168 writeBuffer();
169 buffer[cnt++] = (byte) b;
170 }
171
172 private void writeBuffer() throws IOException {
173 PacketLineOut.formatLength(buffer, cnt);
174 out.write(buffer, 0, cnt);
175 cnt = HDR_SIZE;
176 }
177 }