1 /*
2 * Copyright (C) 2008-2010, Google Inc.
3 * Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * and other copyright owners as documented in the project's IP log.
6 *
7 * This program and the accompanying materials are made available
8 * under the terms of the Eclipse Distribution License v1.0 which
9 * accompanies this distribution, is reproduced below, and is
10 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * - Neither the name of the Eclipse Foundation, Inc. nor the
27 * names of its contributors may be used to endorse or promote
28 * products derived from this software without specific prior
29 * written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46 package org.eclipse.jgit.transport;
47
48 import static java.nio.charset.StandardCharsets.UTF_8;
49
50 import java.io.IOException;
51 import java.io.OutputStream;
52
53 import org.eclipse.jgit.lib.Constants;
54 import org.eclipse.jgit.util.RawParseUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 /**
59 * Write Git style pkt-line formatting to an output stream.
60 * <p>
61 * This class is not thread safe and may issue multiple writes to the underlying
62 * stream for each method call made.
63 * <p>
64 * This class performs no buffering on its own. This makes it suitable to
65 * interleave writes performed by this class with writes performed directly
66 * against the underlying OutputStream.
67 */
68 public class PacketLineOut {
69 private static final Logger log = LoggerFactory.getLogger(PacketLineOut.class);
70
71 private final OutputStream out;
72
73 private final byte[] lenbuffer;
74
75 private boolean flushOnEnd;
76
77 /**
78 * Create a new packet line writer.
79 *
80 * @param outputStream
81 * stream.
82 */
83 public PacketLineOut(OutputStream outputStream) {
84 out = outputStream;
85 lenbuffer = new byte[5];
86 flushOnEnd = true;
87 }
88
89 /**
90 * Set the flush behavior during {@link #end()}.
91 *
92 * @param flushOnEnd
93 * if true, a flush-pkt written during {@link #end()} also
94 * flushes the underlying stream.
95 */
96 public void setFlushOnEnd(boolean flushOnEnd) {
97 this.flushOnEnd = flushOnEnd;
98 }
99
100 /**
101 * Write a UTF-8 encoded string as a single length-delimited packet.
102 *
103 * @param s
104 * string to write.
105 * @throws java.io.IOException
106 * the packet could not be written, the stream is corrupted as
107 * the packet may have been only partially written.
108 */
109 public void writeString(String s) throws IOException {
110 writePacket(Constants.encode(s));
111 }
112
113 /**
114 * Write a binary packet to the stream.
115 *
116 * @param packet
117 * the packet to write; the length of the packet is equal to the
118 * size of the byte array.
119 * @throws java.io.IOException
120 * the packet could not be written, the stream is corrupted as
121 * the packet may have been only partially written.
122 */
123 public void writePacket(byte[] packet) throws IOException {
124 writePacket(packet, 0, packet.length);
125 }
126
127 /**
128 * Write a binary packet to the stream.
129 *
130 * @param buf
131 * the packet to write
132 * @param pos
133 * first index within {@code buf}.
134 * @param len
135 * number of bytes to write.
136 * @throws java.io.IOException
137 * the packet could not be written, the stream is corrupted as
138 * the packet may have been only partially written.
139 * @since 4.5
140 */
141 public void writePacket(byte[] buf, int pos, int len) throws IOException {
142 formatLength(len + 4);
143 out.write(lenbuffer, 0, 4);
144 out.write(buf, pos, len);
145 if (log.isDebugEnabled()) {
146 String s = RawParseUtils.decode(UTF_8, buf, pos, len);
147 log.debug("git> " + s); //$NON-NLS-1$
148 }
149 }
150
151 /**
152 * Write a packet delim marker (0001).
153 *
154 * @throws java.io.IOException
155 * the marker could not be written, the stream is corrupted
156 * as the marker may have been only partially written.
157 * @since 5.0
158 */
159 public void writeDelim() throws IOException {
160 formatLength(1);
161 out.write(lenbuffer, 0, 4);
162 log.debug("git> 0001"); //$NON-NLS-1$
163 }
164
165 /**
166 * Write a packet end marker, sometimes referred to as a flush command.
167 * <p>
168 * Technically this is a magical packet type which can be detected
169 * separately from an empty string or an empty packet.
170 * <p>
171 * Implicitly performs a flush on the underlying OutputStream to ensure the
172 * peer will receive all data written thus far.
173 *
174 * @throws java.io.IOException
175 * the end marker could not be written, the stream is corrupted
176 * as the end marker may have been only partially written.
177 */
178 public void end() throws IOException {
179 formatLength(0);
180 out.write(lenbuffer, 0, 4);
181 log.debug("git> 0000"); //$NON-NLS-1$
182 if (flushOnEnd)
183 flush();
184 }
185
186 /**
187 * Flush the underlying OutputStream.
188 * <p>
189 * Performs a flush on the underlying OutputStream to ensure the peer will
190 * receive all data written thus far.
191 *
192 * @throws java.io.IOException
193 * the underlying stream failed to flush.
194 */
195 public void flush() throws IOException {
196 out.flush();
197 }
198
199 private static final byte[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
200 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
201
202 private void formatLength(int w) {
203 formatLength(lenbuffer, w);
204 }
205
206 static void formatLength(byte[] lenbuffer, int w) {
207 int o = 3;
208 while (o >= 0 && w != 0) {
209 lenbuffer[o--] = hexchar[w & 0xf];
210 w >>>= 4;
211 }
212 while (o >= 0)
213 lenbuffer[o--] = '0';
214 }
215 }