use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class BytesStreamsTests method testVLong.
public void testVLong() throws IOException {
final long value = randomLong();
{
// Read works for positive and negative numbers
BytesStreamOutput output = new BytesStreamOutput();
// Use NoCheck variant so we can write negative numbers
output.writeVLongNoCheck(value);
StreamInput input = output.bytes().streamInput();
assertEquals(value, input.readVLong());
}
if (value < 0) {
// Write doesn't work for negative numbers
BytesStreamOutput output = new BytesStreamOutput();
Exception e = expectThrows(IllegalStateException.class, () -> output.writeVLong(value));
assertEquals("Negative longs unsupported, use writeLong or writeZLong for negative numbers [" + value + "]", e.getMessage());
}
assertTrue("If we're not compatible with 5.1.1 we can drop the assertion below", Version.CURRENT.minimumCompatibilityVersion().onOrBefore(Version.V_5_1_1_UNRELEASED));
/* Read -1 as serialized by a version of Elasticsearch that supported writing negative numbers with writeVLong. Note that this
* should be the same test as the first case (when value is negative) but we've kept some bytes so no matter what we do to
* writeVLong in the future we can be sure we can read bytes as written by Elasticsearch before 5.1.2 */
StreamInput in = new BytesArray(Base64.getDecoder().decode("////////////AQAAAAAAAA==")).streamInput();
assertEquals(-1, in.readVLong());
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class StreamTests method testSpecificVLongSerialization.
public void testSpecificVLongSerialization() throws IOException {
List<Tuple<Long, byte[]>> values = Arrays.asList(new Tuple<>(0L, new byte[] { 0 }), new Tuple<>(-1L, new byte[] { 1 }), new Tuple<>(1L, new byte[] { 2 }), new Tuple<>(-2L, new byte[] { 3 }), new Tuple<>(2L, new byte[] { 4 }), new Tuple<>(Long.MIN_VALUE, new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, 1 }), new Tuple<>(Long.MAX_VALUE, new byte[] { -2, -1, -1, -1, -1, -1, -1, -1, -1, 1 }));
for (Tuple<Long, byte[]> value : values) {
BytesStreamOutput out = new BytesStreamOutput();
out.writeZLong(value.v1());
assertArrayEquals(Long.toString(value.v1()), value.v2(), BytesReference.toBytes(out.bytes()));
BytesReference bytes = new BytesArray(value.v2());
assertEquals(Arrays.toString(value.v2()), (long) value.v1(), bytes.streamInput().readZLong());
}
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class StreamTests method testBooleanSerialization.
public void testBooleanSerialization() throws IOException {
final BytesStreamOutput output = new BytesStreamOutput();
output.writeBoolean(false);
output.writeBoolean(true);
final BytesReference bytesReference = output.bytes();
final BytesRef bytesRef = bytesReference.toBytesRef();
assertThat(bytesRef.length, equalTo(2));
final byte[] bytes = bytesRef.bytes;
assertThat(bytes[0], equalTo((byte) 0));
assertThat(bytes[1], equalTo((byte) 1));
final StreamInput input = bytesReference.streamInput();
assertFalse(input.readBoolean());
assertTrue(input.readBoolean());
final Set<Byte> set = IntStream.range(Byte.MIN_VALUE, Byte.MAX_VALUE).mapToObj(v -> (byte) v).collect(Collectors.toSet());
set.remove((byte) 0);
set.remove((byte) 1);
final byte[] corruptBytes = new byte[] { randomFrom(set) };
final BytesReference corrupt = new BytesArray(corruptBytes);
final IllegalStateException e = expectThrows(IllegalStateException.class, () -> corrupt.streamInput().readBoolean());
final String message = String.format(Locale.ROOT, "unexpected byte [0x%02x]", corruptBytes[0]);
assertThat(e, hasToString(containsString(message)));
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class StreamsTests method testBytesStreamInput.
public void testBytesStreamInput() throws IOException {
byte[] stuff = new byte[] { 0, 1, 2, 3 };
BytesRef stuffRef = new BytesRef(stuff, 2, 2);
BytesArray stuffArray = new BytesArray(stuffRef);
StreamInput input = stuffArray.streamInput();
assertEquals(2, input.read());
assertEquals(3, input.read());
assertEquals(-1, input.read());
input.close();
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class BaseXContentTestCase method doTestRawValue.
void doTestRawValue(XContent source) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try (XContentGenerator generator = source.createGenerator(os)) {
generator.writeStartObject();
generator.writeFieldName("foo");
generator.writeNull();
generator.writeEndObject();
}
final byte[] rawData = os.toByteArray();
os = new ByteArrayOutputStream();
try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
generator.writeRawValue(new BytesArray(rawData));
}
XContentParser parser = xcontentType().xContent().createParser(NamedXContentRegistry.EMPTY, os.toByteArray());
assertEquals(Token.START_OBJECT, parser.nextToken());
assertEquals(Token.FIELD_NAME, parser.nextToken());
assertEquals("foo", parser.currentName());
assertEquals(Token.VALUE_NULL, parser.nextToken());
assertEquals(Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
os = new ByteArrayOutputStream();
try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
generator.writeStartObject();
generator.writeFieldName("test");
generator.writeRawValue(new BytesArray(rawData));
generator.writeEndObject();
}
parser = xcontentType().xContent().createParser(NamedXContentRegistry.EMPTY, os.toByteArray());
assertEquals(Token.START_OBJECT, parser.nextToken());
assertEquals(Token.FIELD_NAME, parser.nextToken());
assertEquals("test", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
assertEquals(Token.FIELD_NAME, parser.nextToken());
assertEquals("foo", parser.currentName());
assertEquals(Token.VALUE_NULL, parser.nextToken());
assertEquals(Token.END_OBJECT, parser.nextToken());
assertEquals(Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
Aggregations