use of java.io.DataInput in project geode by apache.
the class DataSerializableJUnitTest method testStatArchiveCompactValueSerialization.
/**
* Test for {@link StatArchiveWriter#writeCompactValue} and
* {@link StatArchiveWriter#readCompactValue}. Also added test for
* ByteBufferInputStream#readUnsigned* methods (bug #41197).
*/
@Test
public void testStatArchiveCompactValueSerialization() throws Exception {
// test all combos of valueToTest and + and -offsets
long[] valuesToTest = new long[] { 0, Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE };
int[] offsets = new int[] { 0, 1, 4, 9, 14, 15, 16, -1, -4, -9, -14, -15, -16 };
// write all combos of longs to the outputstream
HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
DataOutput out = hdos;
for (long valueToTest : valuesToTest) {
for (int offset : offsets) {
long val = valueToTest + offset;
StatArchiveWriter.writeCompactValue(val, out);
}
}
// now read all the combos
byte[] bytes = hdos.toByteArray();
DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
for (long valueToTest : valuesToTest) {
for (int offset : offsets) {
long expectedVal = valueToTest + offset;
long val = StatArchiveWriter.readCompactValue(in);
assertEquals(expectedVal, val);
}
}
// also test using ByteBufferInputStream (bug #41197)
in = new ByteBufferInputStream(ByteBuffer.wrap(bytes));
for (long valueToTest : valuesToTest) {
for (int offset : offsets) {
long expectedVal = valueToTest + offset;
long val = StatArchiveWriter.readCompactValue(in);
assertEquals(expectedVal, val);
}
}
// now check ByteBufferInputStream#readUnsignedShort explicitly
// readUnsignedByte is already tested in StatArchiveWriter.readCompactValue
// above likely in a more thorough manner than a simple explicit test would
short[] shortValuesToTest = new short[] { 0, Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE };
ByteBufferOutputStream bos = new ByteBufferOutputStream();
out = new DataOutputStream(bos);
for (short valueToTest : shortValuesToTest) {
for (int offset : offsets) {
short val = (short) (valueToTest + offset);
out.writeShort(val);
}
}
// now read all the combos
in = new ByteBufferInputStream(bos.getContentBuffer());
for (short valueToTest : shortValuesToTest) {
for (int offset : offsets) {
int expectedVal = (valueToTest + offset) & 0xffff;
int val = in.readUnsignedShort();
assertEquals(expectedVal, val);
}
}
}
use of java.io.DataInput in project geode by apache.
the class DataSerializableJUnitTest method testLongObject.
/**
* Tests data serializing a {@link Long} using {@link DataSerializer#writeObject}.
*/
@Test
public void testLongObject() throws Exception {
Long value = new Long(getRandom().nextLong());
DataOutputStream out = getDataOutput();
DataSerializer.writeObject(value, out);
out.flush();
DataInput in = getDataInput();
Long value2 = (Long) DataSerializer.readObject(in);
assertEquals(value, value2);
}
use of java.io.DataInput in project geode by apache.
the class DataSerializableJUnitTest method testBigUtfString.
/**
* Tests data serializing a non-<code>null</code> {@link String} longer than 64k.
*/
@Test
public void testBigUtfString() throws Exception {
StringBuffer sb = new StringBuffer(100000);
for (int i = 0; i < 100000; i++) {
if ((i % 1) == 0) {
sb.append(Character.MAX_VALUE);
} else {
sb.append(Character.MIN_VALUE);
}
}
String value = sb.toString();
DataOutputStream out = getDataOutput();
DataSerializer.writeString(value, out);
DataSerializer.writeObject(value, out);
out.flush();
DataInput in = getDataInput();
String value2 = DataSerializer.readString(in);
assertEquals(value, value2);
value2 = (String) DataSerializer.readObject(in);
assertEquals(value, value2);
}
use of java.io.DataInput in project asterixdb by apache.
the class BinaryEntry method print.
// Inefficient. Just for debugging.
@SuppressWarnings("rawtypes")
public String print(ISerializerDeserializer serde) throws HyracksDataException {
ByteArrayInputStream inStream = new ByteArrayInputStream(buf, off, len);
DataInput dataIn = new DataInputStream(inStream);
return serde.deserialize(dataIn).toString();
}
use of java.io.DataInput in project asterixdb by apache.
the class InMemoryInvertedListCursor method printInvList.
@SuppressWarnings("rawtypes")
@Override
public String printInvList(ISerializerDeserializer[] serdes) throws HyracksDataException {
StringBuilder strBuilder = new StringBuilder();
try {
while (btreeCursor.hasNext()) {
btreeCursor.next();
ITupleReference tuple = btreeCursor.getTuple();
ByteArrayInputStream inStream = new ByteArrayInputStream(tuple.getFieldData(1), tuple.getFieldStart(1), tuple.getFieldLength(1));
DataInput dataIn = new DataInputStream(inStream);
Object o = serdes[0].deserialize(dataIn);
strBuilder.append(o.toString() + " ");
}
} finally {
btreeCursor.close();
btreeCursor.reset();
}
btreeAccessor.search(btreeCursor, btreePred);
return strBuilder.toString();
}
Aggregations