aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@gmail.com>2019-06-02 17:02:51 +0900
committerDavid Pursehouse <david.pursehouse@gmail.com>2019-06-03 08:07:24 +0900
commitee747827b0d26cf770f44a33b201de31e81ca8ea (patch)
tree0ae0555ffb44f5c9adcb0b94a675ed3e90ac441e
parente0133b9440cf28cc8495e4af0546d1467edbb028 (diff)
downloadjgit-ee747827b0d26cf770f44a33b201de31e81ca8ea.tar.gz
jgit-ee747827b0d26cf770f44a33b201de31e81ca8ea.zip
PacketLineIn: Add an iterator over strings in the input stream
Allows callers to read all lines in the input stream until the END marker is reached, without having to explicitly check for the END marker. Replace all remaining usage of the END marker with the new method. Change-Id: I51f419c7f569ab7ed01e1aaaf6b40ed8cdc2116b Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
-rw-r--r--org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackPushConnection.java3
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java58
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV2Parser.java63
4 files changed, 93 insertions, 37 deletions
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
index 4158049171..8ec2f51cff 100644
--- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
@@ -387,8 +387,7 @@ public class HttpClientTests extends HttpTestCase {
// What remains are capabilities - ensure that all of them are
// non-empty strings, and that we see END at the end.
- String s;
- while ((s = pckIn.readString()) != PacketLineIn.END) {
+ for (String s : pckIn.readStrings()) {
assertTrue(!s.isEmpty());
}
}
@@ -421,8 +420,7 @@ public class HttpClientTests extends HttpTestCase {
PacketLineIn pckIn = new PacketLineIn(c.getInputStream());
// Just check that we get what looks like a ref advertisement.
- String s;
- while ((s = pckIn.readString()) != PacketLineIn.END) {
+ for (String s : pckIn.readStrings()) {
assertTrue(s.matches("[0-9a-f]{40} [A-Za-z/]*"));
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackPushConnection.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackPushConnection.java
index 847e901980..35ea35ecb8 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackPushConnection.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackPushConnection.java
@@ -383,8 +383,7 @@ public abstract class BasePackPushConnection extends BasePackConnection implemen
JGitText.get().errorOccurredDuringUnpackingOnTheRemoteEnd, unpackStatus));
}
- String refLine;
- while ((refLine = pckIn.readString()) != PacketLineIn.END) {
+ for (String refLine : pckIn.readStrings()) {
boolean ok = false;
int refNameEnd = -1;
if (refLine.startsWith("ok ")) { //$NON-NLS-1$
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 fff4f08c5a..e218c1e608 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java
@@ -49,7 +49,9 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UncheckedIOException;
import java.text.MessageFormat;
+import java.util.Iterator;
import org.eclipse.jgit.errors.PackProtocolException;
import org.eclipse.jgit.internal.JGitText;
@@ -196,6 +198,20 @@ public class PacketLineIn {
}
/**
+ * Get an iterator to read strings from the input stream.
+ *
+ * @return an iterator that calls {@link #readString()} until {@link #END}
+ * is encountered.
+ *
+ * @throws IOException
+ * on failure to read the initial packet line.
+ * @since 5.4
+ */
+ public PacketLineInIterator readStrings() throws IOException {
+ return new PacketLineInIterator(this);
+ }
+
+ /**
* Read a single UTF-8 encoded string packet from the input stream.
* <p>
* Unlike {@link #readString()} a trailing LF will be retained.
@@ -331,4 +347,46 @@ public class PacketLineIn {
public static class InputOverLimitIOException extends IOException {
private static final long serialVersionUID = 1L;
}
+
+ /**
+ * Iterator over packet lines.
+ * <p>
+ * Calls {@link #readString()} on the {@link PacketLineIn} until
+ * {@link #END} is encountered.
+ *
+ * @since 5.4
+ *
+ */
+ public static class PacketLineInIterator implements Iterable<String> {
+ private PacketLineIn in;
+
+ private String current;
+
+ PacketLineInIterator(PacketLineIn in) throws IOException {
+ this.in = in;
+ current = in.readString();
+ }
+
+ @Override
+ public Iterator<String> iterator() {
+ return new Iterator<String>() {
+ @Override
+ public boolean hasNext() {
+ return !PacketLineIn.isEnd(current);
+ }
+
+ @Override
+ public String next() {
+ String next = current;
+ try {
+ current = in.readString();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ return next;
+ }
+ };
+ }
+
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV2Parser.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV2Parser.java
index 2c7beedc62..caba15fc54 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV2Parser.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV2Parser.java
@@ -144,29 +144,30 @@ final class ProtocolV2Parser {
}
boolean filterReceived = false;
- while ((line = pckIn.readString()) != PacketLineIn.END) {
- if (line.startsWith("want ")) { //$NON-NLS-1$
- reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
+ for (String line2 : pckIn.readStrings()) {
+ if (line2.startsWith("want ")) { //$NON-NLS-1$
+ reqBuilder.addWantId(ObjectId.fromString(line2.substring(5)));
} else if (transferConfig.isAllowRefInWant()
- && line.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$
- reqBuilder.addWantedRef(line.substring(OPTION_WANT_REF.length() + 1));
- } else if (line.startsWith("have ")) { //$NON-NLS-1$
- reqBuilder.addPeerHas(ObjectId.fromString(line.substring(5)));
- } else if (line.equals("done")) { //$NON-NLS-1$
+ && line2.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$
+ reqBuilder.addWantedRef(
+ line2.substring(OPTION_WANT_REF.length() + 1));
+ } else if (line2.startsWith("have ")) { //$NON-NLS-1$
+ reqBuilder.addPeerHas(ObjectId.fromString(line2.substring(5)));
+ } else if (line2.equals("done")) { //$NON-NLS-1$
reqBuilder.setDoneReceived();
- } else if (line.equals(OPTION_THIN_PACK)) {
+ } else if (line2.equals(OPTION_THIN_PACK)) {
reqBuilder.addClientCapability(OPTION_THIN_PACK);
- } else if (line.equals(OPTION_NO_PROGRESS)) {
+ } else if (line2.equals(OPTION_NO_PROGRESS)) {
reqBuilder.addClientCapability(OPTION_NO_PROGRESS);
- } else if (line.equals(OPTION_INCLUDE_TAG)) {
+ } else if (line2.equals(OPTION_INCLUDE_TAG)) {
reqBuilder.addClientCapability(OPTION_INCLUDE_TAG);
- } else if (line.equals(OPTION_OFS_DELTA)) {
+ } else if (line2.equals(OPTION_OFS_DELTA)) {
reqBuilder.addClientCapability(OPTION_OFS_DELTA);
- } else if (line.startsWith("shallow ")) { //$NON-NLS-1$
+ } else if (line2.startsWith("shallow ")) { //$NON-NLS-1$
reqBuilder.addClientShallowCommit(
- ObjectId.fromString(line.substring(8)));
- } else if (line.startsWith("deepen ")) { //$NON-NLS-1$
- int parsedDepth = Integer.parseInt(line.substring(7));
+ ObjectId.fromString(line2.substring(8)));
+ } else if (line2.startsWith("deepen ")) { //$NON-NLS-1$
+ int parsedDepth = Integer.parseInt(line2.substring(7));
if (parsedDepth <= 0) {
throw new PackProtocolException(
MessageFormat.format(JGitText.get().invalidDepth,
@@ -181,19 +182,19 @@ final class ProtocolV2Parser {
JGitText.get().deepenNotWithDeepen);
}
reqBuilder.setDepth(parsedDepth);
- } else if (line.startsWith("deepen-not ")) { //$NON-NLS-1$
- reqBuilder.addDeepenNotRef(line.substring(11));
+ } else if (line2.startsWith("deepen-not ")) { //$NON-NLS-1$
+ reqBuilder.addDeepenNotRef(line2.substring(11));
if (reqBuilder.getDepth() != 0) {
throw new PackProtocolException(
JGitText.get().deepenNotWithDeepen);
}
- } else if (line.equals(OPTION_DEEPEN_RELATIVE)) {
+ } else if (line2.equals(OPTION_DEEPEN_RELATIVE)) {
reqBuilder.addClientCapability(OPTION_DEEPEN_RELATIVE);
- } else if (line.startsWith("deepen-since ")) { //$NON-NLS-1$
- int ts = Integer.parseInt(line.substring(13));
+ } else if (line2.startsWith("deepen-since ")) { //$NON-NLS-1$
+ int ts = Integer.parseInt(line2.substring(13));
if (ts <= 0) {
throw new PackProtocolException(MessageFormat
- .format(JGitText.get().invalidTimestamp, line));
+ .format(JGitText.get().invalidTimestamp, line2));
}
if (reqBuilder.getDepth() != 0) {
throw new PackProtocolException(
@@ -201,17 +202,17 @@ final class ProtocolV2Parser {
}
reqBuilder.setDeepenSince(ts);
} else if (transferConfig.isAllowFilter()
- && line.startsWith(OPTION_FILTER + ' ')) {
+ && line2.startsWith(OPTION_FILTER + ' ')) {
if (filterReceived) {
throw new PackProtocolException(
JGitText.get().tooManyFilters);
}
filterReceived = true;
reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(
- line.substring(OPTION_FILTER.length() + 1)));
+ line2.substring(OPTION_FILTER.length() + 1)));
} else {
throw new PackProtocolException(MessageFormat
- .format(JGitText.get().unexpectedPacketLine, line));
+ .format(JGitText.get().unexpectedPacketLine, line2));
}
}
@@ -253,16 +254,16 @@ final class ProtocolV2Parser {
.format(JGitText.get().unexpectedPacketLine, line));
}
- while ((line = pckIn.readString()) != PacketLineIn.END) {
- if (line.equals("peel")) { //$NON-NLS-1$
+ for (String line2 : pckIn.readStrings()) {
+ if (line2.equals("peel")) { //$NON-NLS-1$
builder.setPeel(true);
- } else if (line.equals("symrefs")) { //$NON-NLS-1$
+ } else if (line2.equals("symrefs")) { //$NON-NLS-1$
builder.setSymrefs(true);
- } else if (line.startsWith("ref-prefix ")) { //$NON-NLS-1$
- prefixes.add(line.substring("ref-prefix ".length())); //$NON-NLS-1$
+ } else if (line2.startsWith("ref-prefix ")) { //$NON-NLS-1$
+ prefixes.add(line2.substring("ref-prefix ".length())); //$NON-NLS-1$
} else {
throw new PackProtocolException(MessageFormat
- .format(JGitText.get().unexpectedPacketLine, line));
+ .format(JGitText.get().unexpectedPacketLine, line2));
}
}