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.InputStream;
50 import java.text.MessageFormat;
51
52 import org.eclipse.jgit.errors.PackProtocolException;
53 import org.eclipse.jgit.internal.JGitText;
54 import org.eclipse.jgit.lib.Constants;
55 import org.eclipse.jgit.lib.MutableObjectId;
56 import org.eclipse.jgit.util.IO;
57 import org.eclipse.jgit.util.RawParseUtils;
58
59 /**
60 * Read Git style pkt-line formatting from an input stream.
61 * <p>
62 * This class is not thread safe and may issue multiple reads to the underlying
63 * stream for each method call made.
64 * <p>
65 * This class performs no buffering on its own. This makes it suitable to
66 * interleave reads performed by this class with reads performed directly
67 * against the underlying InputStream.
68 */
69 public class PacketLineIn {
70 /** Magic return from {@link #readString()} when a flush packet is found. */
71 public static final String END = new StringBuilder(0).toString(); /* must not string pool */
72
73 static enum AckNackResult {
74 /** NAK */
75 NAK,
76 /** ACK */
77 ACK,
78 /** ACK + continue */
79 ACK_CONTINUE,
80 /** ACK + common */
81 ACK_COMMON,
82 /** ACK + ready */
83 ACK_READY;
84 }
85
86 private final InputStream in;
87
88 private final byte[] lineBuffer;
89
90 /**
91 * Create a new packet line reader.
92 *
93 * @param i
94 * the input stream to consume.
95 */
96 public PacketLineIn(final InputStream i) {
97 in = i;
98 lineBuffer = new byte[SideBandOutputStream.SMALL_BUF];
99 }
100
101 AckNackResult readACK(final MutableObjectId returnedId) throws IOException {
102 final String line = readString();
103 if (line.length() == 0)
104 throw new PackProtocolException(JGitText.get().expectedACKNAKFoundEOF);
105 if ("NAK".equals(line)) //$NON-NLS-1$
106 return AckNackResult.NAK;
107 if (line.startsWith("ACK ")) { //$NON-NLS-1$
108 returnedId.fromString(line.substring(4, 44));
109 if (line.length() == 44)
110 return AckNackResult.ACK;
111
112 final String arg = line.substring(44);
113 if (arg.equals(" continue")) //$NON-NLS-1$
114 return AckNackResult.ACK_CONTINUE;
115 else if (arg.equals(" common")) //$NON-NLS-1$
116 return AckNackResult.ACK_COMMON;
117 else if (arg.equals(" ready")) //$NON-NLS-1$
118 return AckNackResult.ACK_READY;
119 }
120 if (line.startsWith("ERR ")) //$NON-NLS-1$
121 throw new PackProtocolException(line.substring(4));
122 throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedACKNAKGot, line));
123 }
124
125 /**
126 * Read a single UTF-8 encoded string packet from the input stream.
127 * <p>
128 * If the string ends with an LF, it will be removed before returning the
129 * value to the caller. If this automatic trimming behavior is not desired,
130 * use {@link #readStringRaw()} instead.
131 *
132 * @return the string. {@link #END} if the string was the magic flush
133 * packet.
134 * @throws IOException
135 * the stream cannot be read.
136 */
137 public String readString() throws IOException {
138 int len = readLength();
139 if (len == 0)
140 return END;
141
142 len -= 4; // length header (4 bytes)
143 if (len == 0)
144 return ""; //$NON-NLS-1$
145
146 byte[] raw;
147 if (len <= lineBuffer.length)
148 raw = lineBuffer;
149 else
150 raw = new byte[len];
151
152 IO.readFully(in, raw, 0, len);
153 if (raw[len - 1] == '\n')
154 len--;
155 return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
156 }
157
158 /**
159 * Read a single UTF-8 encoded string packet from the input stream.
160 * <p>
161 * Unlike {@link #readString()} a trailing LF will be retained.
162 *
163 * @return the string. {@link #END} if the string was the magic flush
164 * packet.
165 * @throws IOException
166 * the stream cannot be read.
167 */
168 public String readStringRaw() throws IOException {
169 int len = readLength();
170 if (len == 0)
171 return END;
172
173 len -= 4; // length header (4 bytes)
174
175 byte[] raw;
176 if (len <= lineBuffer.length)
177 raw = lineBuffer;
178 else
179 raw = new byte[len];
180
181 IO.readFully(in, raw, 0, len);
182 return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
183 }
184
185 int readLength() throws IOException {
186 IO.readFully(in, lineBuffer, 0, 4);
187 try {
188 final int len = RawParseUtils.parseHexInt16(lineBuffer, 0);
189 if (len != 0 && len < 4)
190 throw new ArrayIndexOutOfBoundsException();
191 return len;
192 } catch (ArrayIndexOutOfBoundsException err) {
193 throw new IOException(MessageFormat.format(JGitText.get().invalidPacketLineHeader,
194 "" + (char) lineBuffer[0] + (char) lineBuffer[1] //$NON-NLS-1$
195 + (char) lineBuffer[2] + (char) lineBuffer[3]));
196 }
197 }
198 }