Search in sources :

Example 11 with com.amazon.ion.impl.bin._Private_IonRawWriter

use of com.amazon.ion.impl.bin._Private_IonRawWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method ivmResetsImports.

@Test
public void ivmResetsImports() throws Exception {
    SimpleCatalog catalog = new SimpleCatalog();
    catalog.putTable(SYSTEM.newSharedSymbolTable("foo", 1, Arrays.asList("abc", "def").iterator()));
    readerBuilder = IonReaderBuilder.standard().withCatalog(catalog);
    IonReaderBinaryIncremental reader = readerFor(new RawWriterFunction() {

        @Override
        public void write(_Private_IonRawWriter writer, ByteArrayOutputStream out) throws IOException {
            SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1);
            writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table"));
            writer.stepIn(IonType.STRUCT);
            writer.setFieldNameSymbol(systemTable.findSymbol("symbols"));
            writer.stepIn(IonType.LIST);
            writer.writeString("ghi");
            writer.stepOut();
            writer.setFieldNameSymbol(systemTable.findSymbol("imports"));
            writer.stepIn(IonType.LIST);
            writer.stepIn(IonType.STRUCT);
            writer.setFieldNameSymbol(systemTable.findSymbol("name"));
            writer.writeString("foo");
            writer.setFieldNameSymbol(systemTable.findSymbol("version"));
            writer.writeInt(1);
            writer.setFieldNameSymbol(systemTable.findSymbol("max_id"));
            writer.writeInt(2);
            writer.stepOut();
            writer.stepOut();
            writer.stepOut();
            writer.writeSymbolToken(10);
            writer.writeSymbolToken(11);
            writer.writeSymbolToken(12);
            writer.close();
            out.write(_Private_IonConstants.BINARY_VERSION_MARKER_1_0);
            out.write(0x20);
        }
    });
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("abc", reader.stringValue());
    SymbolTable[] imports = reader.getSymbolTable().getImportedTables();
    assertEquals(1, imports.length);
    assertEquals("foo", imports[0].getName());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("def", reader.stringValue());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("ghi", reader.stringValue());
    assertEquals(IonType.INT, reader.next());
    assertTrue(reader.getSymbolTable().isSystemTable());
    assertEquals(0, reader.intValue());
    assertNull(reader.next());
    reader.close();
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) com.amazon.ion.impl.bin._Private_IonRawWriter(com.amazon.ion.impl.bin._Private_IonRawWriter) SymbolTable(com.amazon.ion.SymbolTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with com.amazon.ion.impl.bin._Private_IonRawWriter

use of com.amazon.ion.impl.bin._Private_IonRawWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method incrementalSymbolTables.

@Test
public void incrementalSymbolTables() throws Exception {
    ResizingPipedInputStream pipe = new ResizingPipedInputStream(128);
    ByteArrayOutputStream firstValue = new ByteArrayOutputStream();
    _Private_IonRawWriter writer = IonBinaryWriterBuilder.standard().build(firstValue).asFacet(_Private_IonManagedWriter.class).getRawWriter();
    firstValue.write(_Private_IonConstants.BINARY_VERSION_MARKER_1_0);
    writer.setTypeAnnotationSymbols(3);
    writer.stepIn(IonType.STRUCT);
    writer.setFieldNameSymbol(7);
    writer.stepIn(IonType.LIST);
    writer.writeString("abcdefghijklmnopqrstuvwxyz");
    writer.writeString("def");
    writer.stepOut();
    writer.stepOut();
    writer.stepIn(IonType.STRUCT);
    writer.setTypeAnnotationSymbols(11);
    writer.setFieldNameSymbol(10);
    writer.writeString("foo");
    writer.stepOut();
    writer.close();
    ByteArrayOutputStream secondValue = new ByteArrayOutputStream();
    writer = IonBinaryWriterBuilder.standard().build(secondValue).asFacet(_Private_IonManagedWriter.class).getRawWriter();
    writer.setTypeAnnotationSymbols(3);
    writer.stepIn(IonType.STRUCT);
    writer.setFieldNameSymbol(6);
    writer.writeSymbolToken(3);
    writer.setFieldNameSymbol(7);
    writer.stepIn(IonType.LIST);
    writer.writeString("foo");
    writer.writeString("bar");
    writer.stepOut();
    writer.stepOut();
    writer.stepIn(IonType.STRUCT);
    writer.setFieldNameSymbol(10);
    writer.setTypeAnnotationSymbols(12, 13);
    writer.writeString("fairlyLongString");
    writer.stepOut();
    writer.close();
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(STANDARD_READER_BUILDER, pipe);
    byte[] bytes = firstValue.toByteArray();
    for (byte b : bytes) {
        assertNull(reader.next());
        pipe.receive(b);
    }
    assertEquals(IonType.STRUCT, reader.next());
    reader.stepIn();
    assertEquals(IonType.STRING, reader.next());
    assertEquals(Collections.singletonList("def"), Arrays.asList(reader.getTypeAnnotations()));
    assertEquals("abcdefghijklmnopqrstuvwxyz", reader.getFieldName());
    assertEquals("foo", reader.stringValue());
    assertNull(reader.next());
    reader.stepOut();
    bytes = secondValue.toByteArray();
    for (byte b : bytes) {
        assertNull(reader.next());
        pipe.receive(b);
    }
    assertEquals(IonType.STRUCT, reader.next());
    reader.stepIn();
    assertEquals(IonType.STRING, reader.next());
    assertEquals("fairlyLongString", reader.stringValue());
    assertEquals("abcdefghijklmnopqrstuvwxyz", reader.getFieldName());
    assertEquals(Arrays.asList("foo", "bar"), Arrays.asList(reader.getTypeAnnotations()));
    assertNull(reader.next());
    reader.stepOut();
    assertNull(reader.next());
    reader.close();
}
Also used : com.amazon.ion.impl.bin._Private_IonRawWriter(com.amazon.ion.impl.bin._Private_IonRawWriter) com.amazon.ion.impl.bin._Private_IonManagedWriter(com.amazon.ion.impl.bin._Private_IonManagedWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 13 with com.amazon.ion.impl.bin._Private_IonRawWriter

use of com.amazon.ion.impl.bin._Private_IonRawWriter in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method incrementalRead.

@Test
public void incrementalRead() throws Exception {
    ResizingPipedInputStream pipe = new ResizingPipedInputStream(128);
    ByteArrayOutputStream firstValue = new ByteArrayOutputStream();
    _Private_IonRawWriter firstValueWriter = IonBinaryWriterBuilder.standard().build(firstValue).asFacet(_Private_IonManagedWriter.class).getRawWriter();
    firstValue.write(_Private_IonConstants.BINARY_VERSION_MARKER_1_0);
    firstValueWriter.setTypeAnnotationSymbols(3);
    firstValueWriter.stepIn(IonType.STRUCT);
    firstValueWriter.setFieldNameSymbol(7);
    firstValueWriter.stepIn(IonType.LIST);
    firstValueWriter.writeString("abc");
    firstValueWriter.writeString("def");
    firstValueWriter.stepOut();
    firstValueWriter.stepOut();
    firstValueWriter.stepIn(IonType.STRUCT);
    firstValueWriter.setTypeAnnotationSymbols(11);
    firstValueWriter.setFieldNameSymbol(10);
    firstValueWriter.writeString("foo");
    firstValueWriter.stepOut();
    firstValueWriter.close();
    IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(STANDARD_READER_BUILDER, pipe);
    assertNull(reader.getType());
    assertNull(reader.next());
    assertNull(reader.getType());
    byte[] firstValueBytes = firstValue.toByteArray();
    pipe.receive(firstValueBytes, 0, firstValueBytes.length / 2);
    assertNull(reader.next());
    assertNull(reader.getType());
    pipe.receive(firstValueBytes, firstValueBytes.length / 2, firstValueBytes.length - firstValueBytes.length / 2);
    assertEquals(IonType.STRUCT, reader.next());
    assertEquals(IonType.STRUCT, reader.getType());
    reader.stepIn();
    assertNull(reader.getType());
    assertEquals(IonType.STRING, reader.next());
    assertEquals("abc", reader.getFieldName());
    assertEquals(Collections.singletonList("def"), Arrays.asList(reader.getTypeAnnotations()));
    assertEquals("foo", reader.stringValue());
    assertEquals(IonType.STRING, reader.getType());
    assertNull(reader.next());
    assertNull(reader.getType());
    reader.stepOut();
    assertNull(reader.getType());
    assertNull(reader.next());
    assertNull(reader.getType());
    pipe.receive(0x71);
    assertNull(reader.next());
    assertNull(reader.getType());
    pipe.receive(0x0B);
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals(IonType.SYMBOL, reader.getType());
    assertEquals("def", reader.stringValue());
    assertNull(reader.next());
    assertNull(reader.getType());
    reader.close();
}
Also used : com.amazon.ion.impl.bin._Private_IonRawWriter(com.amazon.ion.impl.bin._Private_IonRawWriter) com.amazon.ion.impl.bin._Private_IonManagedWriter(com.amazon.ion.impl.bin._Private_IonManagedWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

com.amazon.ion.impl.bin._Private_IonRawWriter (com.amazon.ion.impl.bin._Private_IonRawWriter)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Test (org.junit.Test)11 IOException (java.io.IOException)7 SymbolTable (com.amazon.ion.SymbolTable)6 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)5 com.amazon.ion.impl.bin._Private_IonManagedWriter (com.amazon.ion.impl.bin._Private_IonManagedWriter)4 SymbolToken (com.amazon.ion.SymbolToken)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 BigInteger (java.math.BigInteger)1