Search in sources :

Example 46 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class SharedSymbolTableTest method roundTrip.

public void roundTrip(String serializedSymbolTable) throws IOException {
    IonReader reader = system().newReader(serializedSymbolTable);
    SymbolTable stFromReader = myMaker.newSharedSymtab(system(), reader);
    assertTrue(stFromReader.isSharedTable());
    StringBuilder buf = new StringBuilder();
    IonWriter out = system().newTextWriter(buf);
    stFromReader.writeTo(out);
    reader = system().newReader(buf.toString());
    SymbolTable reloaded = myMaker.newSharedSymtab(system(), reader);
    Symtabs.assertEqualSymtabs(stFromReader, reloaded);
}
Also used : IonReader(com.amazon.ion.IonReader) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter)

Example 47 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class IonManagedBinaryWriterTestCase method createWriter.

@Override
protected IonWriter createWriter(final OutputStream out) throws IOException {
    final IonMutableCatalog catalog = ((IonMutableCatalog) system().getCatalog());
    final List<SymbolTable> symbolTables = new ArrayList<SymbolTable>();
    int i = 1;
    for (final List<String> symbols : SHARED_SYMBOLS) {
        final SymbolTable table = system().newSharedSymbolTable("test_" + (i++), 1, symbols.iterator());
        symbolTables.add(table);
        catalog.putTable(table);
    }
    final _Private_IonManagedBinaryWriterBuilder builder = _Private_IonManagedBinaryWriterBuilder.create(_Private_IonManagedBinaryWriterBuilder.AllocatorMode.POOLED).withImports(importedSymbolResolverMode, symbolTables).withPreallocationMode(preallocationMode).withFloatBinary32Enabled();
    if (lstAppendMode.isEnabled()) {
        builder.withLocalSymbolTableAppendEnabled();
    } else {
        builder.withLocalSymbolTableAppendDisabled();
    }
    final IonWriter writer = builder.newWriter(out);
    final SymbolTable locals = writer.getSymbolTable();
    assertEquals(14, locals.getImportedMaxId());
    return writer;
}
Also used : ArrayList(java.util.ArrayList) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) IonMutableCatalog(com.amazon.ion.IonMutableCatalog)

Example 48 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class SymbolTableTest method testLocalSymbolTableAppendImportBoundary.

@Test
public void testLocalSymbolTableAppendImportBoundary() throws IOException {
    String text = LocalSymbolTablePrefix + "{" + "  symbols:[ \"s11\"]" + "}\n" + "1\n" + LocalSymbolTablePrefix + "{" + "  imports:" + ION_SYMBOL_TABLE + "," + "  symbols:[ \"s21\"]" + "}\n" + "null";
    IonDatagram datagram = loader().load(text);
    SymbolTable symbolTable = datagram.get(0).getSymbolTable();
    assertSame(symbolTable, datagram.get(1).getSymbolTable());
    symbolTable.intern("o1");
    symbolTable.intern("a1");
    // new symbols don't influence SIDs for existing symbols; they are appended
    checkSymbol("s11", systemMaxId() + 1, symbolTable);
    checkSymbol("s21", systemMaxId() + 2, symbolTable);
    checkSymbol("o1", systemMaxId() + 3, symbolTable);
    checkSymbol("a1", systemMaxId() + 4, symbolTable);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
    datagram.writeTo(writer);
    writer.close();
    IonDatagram roundtripped = loader().load(out.toByteArray());
    assertTrue(Equivalence.ionEquals(datagram, roundtripped));
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 49 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class SymbolTableTest method testWriteWithSymbolTable.

@Test
public void testWriteWithSymbolTable() throws IOException {
    // this example code is the fix for a previous defect
    // which resulted in a bad assertion in
    // IonWriterUser.close_local_symbol_table_copy
    IonDatagram data;
    data = system().newDatagram();
    insert_local_symbol_table(data);
    append_some_data(data);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = system().newTextWriter(out);
    writer.writeValue(data);
    writer.close();
    // dataMap.put("value", out.toByteArray());
    byte[] bytes = out.toByteArray();
    assertNotNull(bytes);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 50 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class SymbolTableTest method testSymtabBinaryInjection.

@Test
public void testSymtabBinaryInjection() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IonWriter writer = system().newBinaryWriter(baos);
    writer.stepIn(IonType.LIST);
    writer.writeNull();
    writer.writeInt(10);
    writer.writeFloat(10.0);
    writer.writeTimestamp(new Timestamp(2013, 1, 1));
    // this is where symbol table injection happens
    writer.writeSymbol("abc");
    writer.writeString("abc");
    writer.stepOut();
    writer.finish();
    IonReader reader = system().newReader(baos.toByteArray());
    assertEquals(IonType.LIST, reader.next());
    reader.stepIn();
    assertEquals(IonType.NULL, reader.next());
    assertEquals(IonType.INT, reader.next());
    assertEquals(IonType.FLOAT, reader.next());
    assertEquals(IonType.TIMESTAMP, reader.next());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals(IonType.STRING, reader.next());
    assertEquals(null, reader.next());
    reader.stepOut();
    assertEquals(null, reader.next());
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Aggregations

IonWriter (com.amazon.ion.IonWriter)71 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)35 IonReader (com.amazon.ion.IonReader)19 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)16 SymbolTable (com.amazon.ion.SymbolTable)12 IonDatagram (com.amazon.ion.IonDatagram)11 IOException (java.io.IOException)11 IonTextWriterBuilder (com.amazon.ion.system.IonTextWriterBuilder)10 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)9 IonSystem (com.amazon.ion.IonSystem)8 OutputStream (java.io.OutputStream)8 IonCatalog (com.amazon.ion.IonCatalog)4 IonReaderBuilder (com.amazon.ion.system.IonReaderBuilder)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IonException (com.amazon.ion.IonException)3 IonStruct (com.amazon.ion.IonStruct)3 StringWriter (java.io.StringWriter)3