use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method incrementalMultipleValuesLoadFromReader.
@Test
public void incrementalMultipleValuesLoadFromReader() throws Exception {
ResizingPipedInputStream pipe = new ResizingPipedInputStream(128);
final IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(STANDARD_READER_BUILDER, pipe);
final IonLoader loader = SYSTEM.getLoader();
byte[] bytes = toBinary("value_type::\"StringValueLong\"");
for (byte b : bytes) {
IonDatagram empty = loader.load(reader);
assertTrue(empty.isEmpty());
pipe.receive(b);
}
IonDatagram firstValue = loader.load(reader);
assertEquals(1, firstValue.size());
IonString string = (IonString) firstValue.get(0);
assertEquals("StringValueLong", string.stringValue());
assertEquals(Collections.singletonList("value_type"), Arrays.asList(string.getTypeAnnotations()));
bytes = toBinary("{foobar: \"StringValueLong\"}");
for (byte b : bytes) {
IonDatagram empty = loader.load(reader);
assertTrue(empty.isEmpty());
pipe.receive(b);
}
IonDatagram secondValue = loader.load(reader);
assertEquals(1, secondValue.size());
IonStruct struct = (IonStruct) secondValue.get(0);
string = (IonString) struct.get("foobar");
assertEquals("StringValueLong", string.stringValue());
IonDatagram empty = loader.load(reader);
assertTrue(empty.isEmpty());
reader.close();
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method incrementalMultipleValuesLoadFromInputStreamFails.
@Test
public void incrementalMultipleValuesLoadFromInputStreamFails() throws Exception {
final ResizingPipedInputStream pipe = new ResizingPipedInputStream(1);
final IonLoader loader = IonSystemBuilder.standard().withReaderBuilder(STANDARD_READER_BUILDER).build().getLoader();
IonDatagram empty = loader.load(pipe);
assertTrue(empty.isEmpty());
pipe.receive(_Private_IonConstants.BINARY_VERSION_MARKER_1_0[0]);
// Because reader does not persist across load invocations, the loader must throw an exception if the reader
// had an incomplete value buffered.
thrown.expect(IonException.class);
loader.load(pipe);
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class BinaryWriterWithLocalSymtabsTest method testWritingWithImports.
@Test
public void testWritingWithImports() throws Exception {
final int fredSidOffset = systemMaxId();
final int localSidOffset = fredSidOffset + FRED_MAX_IDS[1];
SymbolTable fred1 = Symtabs.register("fred", 1, catalog());
SymbolTable localSymtab = makeLocalSymtab(system(), LOCAL_SYMBOLS_ABC, fred1);
makeBinaryWriterWithLocalSymbols(localSymtab);
// write test data
myWriter.writeSymbol("a");
// Builder makes a copy of the symtab
assertNotSame(localSymtab, myWriter.getSymbolTable());
localSymtab = myWriter.getSymbolTable();
myWriter.writeSymbol("not_interned");
assertSame(localSymtab, myWriter.getSymbolTable());
myWriter.writeSymbol("fred_1");
assertSame(localSymtab, myWriter.getSymbolTable());
myWriter.writeInt(1234);
assertSame(localSymtab, myWriter.getSymbolTable());
// check written bytes
byte[] bytes = outputByteArray();
IonDatagram dg = loader().load(bytes);
assertEquals(6, dg.systemSize());
IonValue val = dg.systemGet(2);
checkSymbol("a", localSidOffset + 1, val);
val = dg.systemGet(3);
checkSymbol("not_interned", localSidOffset + 4, val);
val = dg.systemGet(4);
checkSymbol("fred_1", fredSidOffset + 1, val);
val = dg.systemGet(5);
checkInt(1234, val);
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class BinaryWriterWithLocalSymtabsTest method testWritingWithNoImports.
@Test
public void testWritingWithNoImports() throws Exception {
final int localSidOffset = systemMaxId();
SymbolTable localSymtab = makeLocalSymtab(system(), LOCAL_SYMBOLS_ABC);
makeBinaryWriterWithLocalSymbols(localSymtab);
// write test data
myWriter.writeSymbol("a");
// Builder makes a copy of the symtab
assertNotSame(localSymtab, myWriter.getSymbolTable());
localSymtab = myWriter.getSymbolTable();
myWriter.writeSymbol("not_interned");
assertSame(localSymtab, myWriter.getSymbolTable());
myWriter.writeInt(1234);
assertSame(localSymtab, myWriter.getSymbolTable());
byte[] bytes = outputByteArray();
IonDatagram dg = loader().load(bytes);
// check written bytes
assertEquals(5, dg.systemSize());
IonValue val = dg.systemGet(2);
checkSymbol("a", localSidOffset + 1, val);
val = dg.systemGet(3);
checkSymbol("not_interned", localSidOffset + 4, val);
val = dg.systemGet(4);
checkInt(1234, val);
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class MiscStreamingTest method testValue2.
@Test
public void testValue2() throws Exception {
String s = "item_view::{item_id:\"B00096H8Q4\",marketplace_id:2," + "product:{item_name:[" + "{value:'''Method 24CT Leather Wipes''',lang:EN_CA}," + "{value:'''Method 24CT Chiffons de Cuir''',lang:FR_CA}]," + "list_price:{value:18.23,unit:EUR},}" + ",index_suppressed:true," + "offline_store_only:true,version:2,}";
IonSystem sys = system();
IonDatagram dg = sys.getLoader().load(s);
IonValue v = dg.get(0);
IonType t = v.getType();
assertSame("should be a struct", IonType.STRUCT, t);
int tree_count = ((IonStruct) v).size();
IonReader it = system().newReader(s);
t = it.next();
assertSame("should be a struct", IonType.STRUCT, t);
int string_count = 0;
it.stepIn();
while (it.next() != null) {
string_count++;
}
it.stepOut();
assertSame("tree and string iterator should have the same size", tree_count, string_count);
byte[] buf = dg.getBytes();
it = system().newReader(buf);
t = it.next();
assertSame("should be a struct", IonType.STRUCT, t);
int bin_count = 0;
it.stepIn();
while (it.next() != null) {
bin_count++;
}
it.stepOut();
assertSame("tree and binary iterator should have the same size", tree_count, bin_count);
}
Aggregations