use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class AbstractStreamableTestCase method copyInstance.
/**
* Round trip {@code instance} through binary serialization, setting the wire compatibility version to {@code version}.
*/
protected T copyInstance(T instance, Version version) throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) {
output.setVersion(version);
instance.writeTo(output);
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), getNamedWriteableRegistry())) {
in.setVersion(version);
T newInstance = createBlankInstance();
newInstance.readFrom(in);
return newInstance;
}
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class PriorityTests method testSerialization.
public void testSerialization() throws IOException {
for (Priority p : Priority.values()) {
BytesStreamOutput out = new BytesStreamOutput();
Priority.writeTo(p, out);
Priority priority = Priority.readFrom(out.bytes().streamInput());
assertSame(p, priority);
}
assertSame(Priority.IMMEDIATE, Priority.fromByte((byte) 0));
assertSame(Priority.HIGH, Priority.fromByte((byte) 2));
assertSame(Priority.LANGUID, Priority.fromByte((byte) 5));
assertSame(Priority.LOW, Priority.fromByte((byte) 4));
assertSame(Priority.NORMAL, Priority.fromByte((byte) 3));
assertSame(Priority.URGENT, Priority.fromByte((byte) 1));
assertEquals(6, Priority.values().length);
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class CompositeBytesReferenceTests method testCompositeBuffer.
public void testCompositeBuffer() throws IOException {
List<BytesReference> referenceList = newRefList(randomIntBetween(1, PAGE_SIZE * 2));
BytesReference ref = new CompositeBytesReference(referenceList.toArray(new BytesReference[0]));
BytesRefIterator iterator = ref.iterator();
BytesRefBuilder builder = new BytesRefBuilder();
for (BytesReference reference : referenceList) {
// sometimes we have a paged ref - pull an iter and walk all pages!
BytesRefIterator innerIter = reference.iterator();
BytesRef scratch;
while ((scratch = innerIter.next()) != null) {
BytesRef next = iterator.next();
assertNotNull(next);
assertEquals(next, scratch);
builder.append(next);
}
}
assertNull(iterator.next());
int offset = 0;
for (BytesReference reference : referenceList) {
assertEquals(reference, ref.slice(offset, reference.length()));
int probes = randomIntBetween(Math.min(10, reference.length()), reference.length());
for (int i = 0; i < probes; i++) {
int index = randomIntBetween(0, reference.length() - 1);
assertEquals(ref.get(offset + index), reference.get(index));
}
offset += reference.length();
}
BytesArray array = new BytesArray(builder.toBytesRef());
assertEquals(array, ref);
assertEquals(array.hashCode(), ref.hashCode());
BytesStreamOutput output = new BytesStreamOutput();
ref.writeTo(output);
assertEquals(array, output.bytes());
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class DeflateCompressedXContentTests method testDifferentCompressedRepresentation.
public void testDifferentCompressedRepresentation() throws Exception {
byte[] b = "---\nf:abcdefghijabcdefghij".getBytes("UTF-8");
BytesStreamOutput bout = new BytesStreamOutput();
StreamOutput out = compressor.streamOutput(bout);
out.writeBytes(b);
out.flush();
out.writeBytes(b);
out.close();
final BytesReference b1 = bout.bytes();
bout = new BytesStreamOutput();
out = compressor.streamOutput(bout);
out.writeBytes(b);
out.writeBytes(b);
out.close();
final BytesReference b2 = bout.bytes();
// because of the intermediate flush, the two compressed representations
// are different. It can also happen for other reasons like if hash tables
// of different size are being used
assertFalse(b1.equals(b2));
// we used the compressed representation directly and did not recompress
assertArrayEquals(BytesReference.toBytes(b1), new CompressedXContent(b1).compressed());
assertArrayEquals(BytesReference.toBytes(b2), new CompressedXContent(b2).compressed());
// but compressedstring instances are still equal
assertEquals(new CompressedXContent(b1), new CompressedXContent(b2));
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class GeoDistanceTests method testGeoDistanceSerialization.
public void testGeoDistanceSerialization() throws IOException {
// make sure that ordinals don't change, because we rely on then in serialization
assertThat(GeoDistance.PLANE.ordinal(), equalTo(0));
assertThat(GeoDistance.ARC.ordinal(), equalTo(1));
assertThat(GeoDistance.values().length, equalTo(2));
GeoDistance geoDistance = randomFrom(GeoDistance.PLANE, GeoDistance.ARC);
try (BytesStreamOutput out = new BytesStreamOutput()) {
geoDistance.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
;
GeoDistance copy = GeoDistance.readFromStream(in);
assertEquals(copy.toString() + " vs. " + geoDistance.toString(), copy, geoDistance);
}
}
}
Aggregations