From d2425e16f465303905ba23309d876127faa93af1 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 30 May 2019 20:31:39 +0900 Subject: [PATCH] PacketLineIn: Add helper methods to check for END and DELIM These methods will allow clients to check for END and DELIM without doing a reference comparison on the String objects, which raises warnings from Error Prone. Change-Id: I9e7e59843553ed4488ee8e864033198bbb60d67c Signed-off-by: David Pursehouse --- .../jgit/transport/PacketLineInTest.java | 13 +++++----- .../eclipse/jgit/transport/PacketLineIn.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PacketLineInTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PacketLineInTest.java index 8b1d86005..1e5a03256 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PacketLineInTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PacketLineInTest.java @@ -44,7 +44,8 @@ package org.eclipse.jgit.transport; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; @@ -142,21 +143,21 @@ public class PacketLineInTest { init("0004"); final String act = in.readString(); assertEquals("", act); - assertNotSame(PacketLineIn.END, act); + assertFalse(PacketLineIn.isEnd(act)); assertEOF(); } @Test public void testReadString_End() throws IOException { init("0000"); - assertSame(PacketLineIn.END, in.readString()); + assertTrue(PacketLineIn.isEnd(in.readString())); assertEOF(); } @Test public void testReadString_Delim() throws IOException { init("0001"); - assertSame(PacketLineIn.DELIM, in.readString()); + assertTrue(PacketLineIn.isDelim(in.readString())); assertEOF(); } @@ -183,14 +184,14 @@ public class PacketLineInTest { init("0004"); final String act = in.readStringRaw(); assertEquals("", act); - assertNotSame(PacketLineIn.END, act); + assertFalse(PacketLineIn.isEnd(act)); assertEOF(); } @Test public void testReadStringRaw_End() throws IOException { init("0000"); - assertSame(PacketLineIn.END, in.readStringRaw()); + assertTrue(PacketLineIn.isEnd(in.readString())); assertEOF(); } 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 c6e19d576..15d3481f4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java @@ -224,6 +224,30 @@ public class PacketLineIn { return s; } + /** + * Check if a string is the delimiter marker. + * + * @param s + * the string to check + * @return true if the given string is {@link #DELIM}, otherwise false. + * @since 5.4 + */ + public static boolean isDelim(String s) { + return s == DELIM; + } + + /** + * Check if a string is the packet end marker. + * + * @param s + * the string to check + * @return true if the given string is {@link #END}, otherwise false. + * @since 5.4 + */ + public static boolean isEnd(String s) { + return s == END; + } + void discardUntilEnd() throws IOException { for (;;) { int n = readLength();