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 java.io.IOException;
49 import java.io.OutputStream;
50
51 import org.eclipse.jgit.lib.Constants;
52
53 /**
54 * Write Git style pkt-line formatting to an output stream.
55 * <p>
56 * This class is not thread safe and may issue multiple writes to the underlying
57 * stream for each method call made.
58 * <p>
59 * This class performs no buffering on its own. This makes it suitable to
60 * interleave writes performed by this class with writes performed directly
61 * against the underlying OutputStream.
62 */
63 public class PacketLineOut {
64 private final OutputStream out;
65
66 private final byte[] lenbuffer;
67
68 private boolean flushOnEnd;
69
70 /**
71 * Create a new packet line writer.
72 *
73 * @param outputStream
74 * stream.
75 */
76 public PacketLineOut(final OutputStream outputStream) {
77 out = outputStream;
78 lenbuffer = new byte[5];
79 flushOnEnd = true;
80 }
81
82 /**
83 * Set the flush behavior during {@link #end()}.
84 *
85 * @param flushOnEnd
86 * if true, a flush-pkt written during {@link #end()} also
87 * flushes the underlying stream.
88 */
89 public void setFlushOnEnd(boolean flushOnEnd) {
90 this.flushOnEnd = flushOnEnd;
91 }
92
93 /**
94 * Write a UTF-8 encoded string as a single length-delimited packet.
95 *
96 * @param s
97 * string to write.
98 * @throws IOException
99 * the packet could not be written, the stream is corrupted as
100 * the packet may have been only partially written.
101 */
102 public void writeString(final String s) throws IOException {
103 writePacket(Constants.encode(s));
104 }
105
106 /**
107 * Write a binary packet to the stream.
108 *
109 * @param packet
110 * the packet to write; the length of the packet is equal to the
111 * size of the byte array.
112 * @throws IOException
113 * the packet could not be written, the stream is corrupted as
114 * the packet may have been only partially written.
115 */
116 public void writePacket(final byte[] packet) throws IOException {
117 formatLength(packet.length + 4);
118 out.write(lenbuffer, 0, 4);
119 out.write(packet);
120 }
121
122 /**
123 * Write a packet end marker, sometimes referred to as a flush command.
124 * <p>
125 * Technically this is a magical packet type which can be detected
126 * separately from an empty string or an empty packet.
127 * <p>
128 * Implicitly performs a flush on the underlying OutputStream to ensure the
129 * peer will receive all data written thus far.
130 *
131 * @throws IOException
132 * the end marker could not be written, the stream is corrupted
133 * as the end marker may have been only partially written.
134 */
135 public void end() throws IOException {
136 formatLength(0);
137 out.write(lenbuffer, 0, 4);
138 if (flushOnEnd)
139 flush();
140 }
141
142 /**
143 * Flush the underlying OutputStream.
144 * <p>
145 * Performs a flush on the underlying OutputStream to ensure the peer will
146 * receive all data written thus far.
147 *
148 * @throws IOException
149 * the underlying stream failed to flush.
150 */
151 public void flush() throws IOException {
152 out.flush();
153 }
154
155 private static final byte[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
156 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
157
158 private void formatLength(int w) {
159 formatLength(lenbuffer, w);
160 }
161
162 static void formatLength(byte[] lenbuffer, int w) {
163 int o = 3;
164 while (o >= 0 && w != 0) {
165 lenbuffer[o--] = hexchar[w & 0xf];
166 w >>>= 4;
167 }
168 while (o >= 0)
169 lenbuffer[o--] = '0';
170 }
171 }