From 00eae14a7f736640da0086a9a9eb73ed50963eb8 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 28 Mar 2011 10:26:30 -0700 Subject: [PATCH] Make PacketLineIn public PacketLineOut is already public. Make PacketLineIn partially public in case an application needs to use some of the pkt-line protocol. Change-Id: I5b383eca980bd9e16a7dbdb5aed040c6586d4f46 Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/transport/PacketLineIn.java | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java index e5b2494c8..5b06c4389 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java @@ -56,9 +56,19 @@ import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.RawParseUtils; -class PacketLineIn { - /* must not string pool */ - static final String END = new StringBuilder(0).toString(); +/** + * Read Git style pkt-line formatting from an input stream. + *

+ * This class is not thread safe and may issue multiple reads to the underlying + * stream for each method call made. + *

+ * This class performs no buffering on its own. This makes it suitable to + * interleave reads performed by this class with reads performed directly + * against the underlying InputStream. + */ +public class PacketLineIn { + /** Magic return from {@link #readString()} when a flush packet is found. */ + public static final String END = new StringBuilder(0).toString(); /* must not string pool */ static enum AckNackResult { /** NAK */ @@ -77,7 +87,13 @@ static enum AckNackResult { private final byte[] lineBuffer; - PacketLineIn(final InputStream i) { + /** + * Create a new packet line reader. + * + * @param i + * the input stream to consume. + */ + public PacketLineIn(final InputStream i) { in = i; lineBuffer = new byte[SideBandOutputStream.SMALL_BUF]; } @@ -106,7 +122,19 @@ else if (arg.equals(" ready")) throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedACKNAKGot, line)); } - String readString() throws IOException { + /** + * Read a single UTF-8 encoded string packet from the input stream. + *

+ * If the string ends with an LF, it will be removed before returning the + * value to the caller. If this automatic trimming behavior is not desired, + * use {@link #readStringRaw()} instead. + * + * @return the string. {@link #END} if the string was the magic flush + * packet. + * @throws IOException + * the stream cannot be read. + */ + public String readString() throws IOException { int len = readLength(); if (len == 0) return END; @@ -127,7 +155,17 @@ String readString() throws IOException { return RawParseUtils.decode(Constants.CHARSET, raw, 0, len); } - String readStringRaw() throws IOException { + /** + * Read a single UTF-8 encoded string packet from the input stream. + *

+ * Unlike {@link #readString()} a trailing LF will be retained. + * + * @return the string. {@link #END} if the string was the magic flush + * packet. + * @throws IOException + * the stream cannot be read. + */ + public String readStringRaw() throws IOException { int len = readLength(); if (len == 0) return END;