Add readString to ParsableByteArray
This commit is contained in:
parent
eda8ac4e01
commit
cef1f12f1d
@ -284,6 +284,7 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
0x00, 0x00, 0x00, (byte) 0xFF
|
0x00, 0x00, 0x00, (byte) 0xFF
|
||||||
});
|
});
|
||||||
assertEquals(0xFF00000000000001L, byteArray.readLittleEndianLong());
|
assertEquals(0xFF00000000000001L, byteArray.readLittleEndianLong());
|
||||||
|
assertEquals(8, byteArray.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLittleEndianUnsignedInt() {
|
public void testReadLittleEndianUnsignedInt() {
|
||||||
@ -291,6 +292,7 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
0x10, 0x00, 0x00, (byte) 0xFF
|
0x10, 0x00, 0x00, (byte) 0xFF
|
||||||
});
|
});
|
||||||
assertEquals(0xFF000010L, byteArray.readLittleEndianUnsignedInt());
|
assertEquals(0xFF000010L, byteArray.readLittleEndianUnsignedInt());
|
||||||
|
assertEquals(4, byteArray.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLittleEndianInt() {
|
public void testReadLittleEndianInt() {
|
||||||
@ -298,12 +300,14 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
0x01, 0x00, 0x00, (byte) 0xFF
|
0x01, 0x00, 0x00, (byte) 0xFF
|
||||||
});
|
});
|
||||||
assertEquals(0xFF000001, byteArray.readLittleEndianInt());
|
assertEquals(0xFF000001, byteArray.readLittleEndianInt());
|
||||||
|
assertEquals(4, byteArray.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLittleEndianUnsignedInt24() {
|
public void testReadLittleEndianUnsignedInt24() {
|
||||||
byte[] data = { 0x01, 0x02, (byte) 0xFF };
|
byte[] data = { 0x01, 0x02, (byte) 0xFF };
|
||||||
ParsableByteArray byteArray = new ParsableByteArray(data);
|
ParsableByteArray byteArray = new ParsableByteArray(data);
|
||||||
assertEquals(0xFF0201, byteArray.readLittleEndianUnsignedInt24());
|
assertEquals(0xFF0201, byteArray.readLittleEndianUnsignedInt24());
|
||||||
|
assertEquals(3, byteArray.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLittleEndianUnsignedShort() {
|
public void testReadLittleEndianUnsignedShort() {
|
||||||
@ -311,7 +315,9 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
0x01, (byte) 0xFF, 0x02, (byte) 0xFF
|
0x01, (byte) 0xFF, 0x02, (byte) 0xFF
|
||||||
});
|
});
|
||||||
assertEquals(0xFF01, byteArray.readLittleEndianUnsignedShort());
|
assertEquals(0xFF01, byteArray.readLittleEndianUnsignedShort());
|
||||||
|
assertEquals(2, byteArray.getPosition());
|
||||||
assertEquals(0xFF02, byteArray.readLittleEndianUnsignedShort());
|
assertEquals(0xFF02, byteArray.readLittleEndianUnsignedShort());
|
||||||
|
assertEquals(4, byteArray.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLittleEndianShort() {
|
public void testReadLittleEndianShort() {
|
||||||
@ -319,7 +325,37 @@ public class ParsableByteArrayTest extends TestCase {
|
|||||||
0x01, (byte) 0xFF, 0x02, (byte) 0xFF
|
0x01, (byte) 0xFF, 0x02, (byte) 0xFF
|
||||||
});
|
});
|
||||||
assertEquals((short) 0xFF01, byteArray.readLittleEndianShort());
|
assertEquals((short) 0xFF01, byteArray.readLittleEndianShort());
|
||||||
|
assertEquals(2, byteArray.getPosition());
|
||||||
assertEquals((short) 0xFF02, byteArray.readLittleEndianShort());
|
assertEquals((short) 0xFF02, byteArray.readLittleEndianShort());
|
||||||
|
assertEquals(4, byteArray.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testReadString() {
|
||||||
|
byte[] data = {
|
||||||
|
(byte) 0xC3, (byte) 0xA4, (byte) 0x20,
|
||||||
|
(byte) 0xC3, (byte) 0xB6, (byte) 0x20,
|
||||||
|
(byte) 0xC2, (byte) 0xAE, (byte) 0x20,
|
||||||
|
(byte) 0xCF, (byte) 0x80, (byte) 0x20,
|
||||||
|
(byte) 0xE2, (byte) 0x88, (byte) 0x9A, (byte) 0x20,
|
||||||
|
(byte) 0xC2, (byte) 0xB1, (byte) 0x20,
|
||||||
|
(byte) 0xE8, (byte) 0xB0, (byte) 0xA2, (byte) 0x20,
|
||||||
|
};
|
||||||
|
ParsableByteArray byteArray = new ParsableByteArray(data);
|
||||||
|
assertEquals("ä ö ® π √ ± 谢 ", byteArray.readString(data.length));
|
||||||
|
assertEquals(data.length, byteArray.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testReadStringOutOfBoundsDoesNotMovePosition() {
|
||||||
|
byte[] data = {
|
||||||
|
(byte) 0xC3, (byte) 0xA4, (byte) 0x20
|
||||||
|
};
|
||||||
|
ParsableByteArray byteArray = new ParsableByteArray(data);
|
||||||
|
try {
|
||||||
|
byteArray.readString(data.length + 1);
|
||||||
|
fail();
|
||||||
|
} catch (StringIndexOutOfBoundsException e) {
|
||||||
|
assertEquals(0, byteArray.getPosition());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadEmptyString() {
|
public void testReadEmptyString() {
|
||||||
|
@ -306,6 +306,18 @@ public final class ParsableByteArray {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the next {@code length} bytes as UTF-8 characters.
|
||||||
|
*
|
||||||
|
* @param length the number of bytes to read.
|
||||||
|
* @return the UTF-8 {@code String} read.
|
||||||
|
*/
|
||||||
|
public String readString(int length) {
|
||||||
|
String utf8String = new String(data, position, length);
|
||||||
|
position += length;
|
||||||
|
return utf8String;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a line of text.
|
* Reads a line of text.
|
||||||
* <p>
|
* <p>
|
||||||
@ -346,5 +358,4 @@ public final class ParsableByteArray {
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user