Browse Source

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 <david.pursehouse@gmail.com>
stable-5.5
David Pursehouse 5 years ago
parent
commit
d2425e16f4
  1. 13
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PacketLineInTest.java
  2. 24
      org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java

13
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PacketLineInTest.java

@ -44,7 +44,8 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import static org.junit.Assert.assertEquals; 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.assertSame;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -142,21 +143,21 @@ public class PacketLineInTest {
init("0004"); init("0004");
final String act = in.readString(); final String act = in.readString();
assertEquals("", act); assertEquals("", act);
assertNotSame(PacketLineIn.END, act); assertFalse(PacketLineIn.isEnd(act));
assertEOF(); assertEOF();
} }
@Test @Test
public void testReadString_End() throws IOException { public void testReadString_End() throws IOException {
init("0000"); init("0000");
assertSame(PacketLineIn.END, in.readString()); assertTrue(PacketLineIn.isEnd(in.readString()));
assertEOF(); assertEOF();
} }
@Test @Test
public void testReadString_Delim() throws IOException { public void testReadString_Delim() throws IOException {
init("0001"); init("0001");
assertSame(PacketLineIn.DELIM, in.readString()); assertTrue(PacketLineIn.isDelim(in.readString()));
assertEOF(); assertEOF();
} }
@ -183,14 +184,14 @@ public class PacketLineInTest {
init("0004"); init("0004");
final String act = in.readStringRaw(); final String act = in.readStringRaw();
assertEquals("", act); assertEquals("", act);
assertNotSame(PacketLineIn.END, act); assertFalse(PacketLineIn.isEnd(act));
assertEOF(); assertEOF();
} }
@Test @Test
public void testReadStringRaw_End() throws IOException { public void testReadStringRaw_End() throws IOException {
init("0000"); init("0000");
assertSame(PacketLineIn.END, in.readStringRaw()); assertTrue(PacketLineIn.isEnd(in.readString()));
assertEOF(); assertEOF();
} }

24
org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java

@ -224,6 +224,30 @@ public class PacketLineIn {
return s; 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 { void discardUntilEnd() throws IOException {
for (;;) { for (;;) {
int n = readLength(); int n = readLength();

Loading…
Cancel
Save