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>
This commit is contained in:
David Pursehouse 2019-05-30 20:31:39 +09:00
parent 9f5d271b12
commit d2425e16f4
2 changed files with 31 additions and 6 deletions

View File

@ -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 void testReadString_Len0004() throws IOException {
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 void testReadStringRaw3() throws IOException {
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();
}

View File

@ -224,6 +224,30 @@ public String readStringRaw() throws IOException {
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();