Fix ParsableByteArrayTest.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=112693523
This commit is contained in:
parent
bed89730a7
commit
9d89d48f56
@ -346,6 +346,13 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
assertEquals(data.length, byteArray.getPosition());
|
assertEquals(data.length, byteArray.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testReadAsciiString() {
|
||||||
|
byte[] data = new byte[] {'t', 'e', 's', 't'};
|
||||||
|
ParsableByteArray testArray = new ParsableByteArray(data);
|
||||||
|
assertEquals("test", testArray.readString(data.length, Charset.forName("US-ASCII")));
|
||||||
|
assertEquals(data.length, testArray.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
public void testReadStringOutOfBoundsDoesNotMovePosition() {
|
public void testReadStringOutOfBoundsDoesNotMovePosition() {
|
||||||
byte[] data = {
|
byte[] data = {
|
||||||
(byte) 0xC3, (byte) 0xA4, (byte) 0x20
|
(byte) 0xC3, (byte) 0xA4, (byte) 0x20
|
||||||
@ -416,11 +423,4 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
assertNull(parser.readLine());
|
assertNull(parser.readLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadString() {
|
|
||||||
byte[] bytes = new byte[] {'t', 'e', 's', 't'};
|
|
||||||
ParsableByteArray testArray = new ParsableByteArray(bytes);
|
|
||||||
assertEquals("test", testArray.readString(bytes.length, Charset.forName("UTF-8")));
|
|
||||||
assertEquals(bytes.length, testArray.getPosition());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -310,13 +310,24 @@ public final class ParsableByteArray {
|
|||||||
/**
|
/**
|
||||||
* Reads the next {@code length} bytes as UTF-8 characters.
|
* Reads the next {@code length} bytes as UTF-8 characters.
|
||||||
*
|
*
|
||||||
* @param length the number of bytes to read.
|
* @param length The number of bytes to read.
|
||||||
* @return the UTF-8 {@code String} read.
|
* @return The string encoded by the bytes.
|
||||||
*/
|
*/
|
||||||
public String readString(int length) {
|
public String readString(int length) {
|
||||||
String utf8String = new String(data, position, length);
|
return readString(length, Charset.defaultCharset());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the next {@code length} bytes as characters in the specified {@link Charset}.
|
||||||
|
*
|
||||||
|
* @param length The number of bytes to read.
|
||||||
|
* @param charset The character set of the encoded characters.
|
||||||
|
* @return The string encoded by the bytes in the specified character set.
|
||||||
|
*/
|
||||||
|
public String readString(int length, Charset charset) {
|
||||||
|
String result = new String(data, position, length, charset);
|
||||||
position += length;
|
position += length;
|
||||||
return utf8String;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -359,17 +370,4 @@ public final class ParsableByteArray {
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads the next {@code bytes} bytes as characters in the specified {@link Charset}.
|
|
||||||
*
|
|
||||||
* @param bytes The number of bytes to read.
|
|
||||||
* @param charset The character set of the encoded characters.
|
|
||||||
* @return The string encoded by the bytes in the specified character set.
|
|
||||||
*/
|
|
||||||
public String readString(int bytes, Charset charset) {
|
|
||||||
String result = new String(data, position, bytes, charset);
|
|
||||||
position += bytes;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user