Search in sources :

Example 1 with IonSystem

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

the class PrintApp method process.

protected void process(IonReader reader, OutputStream out) throws IOException, IonException {
    IonSystem system = this.mySystem;
    IonWriter writer = system.newTextWriter(out);
    writer.writeValues(reader);
    // Ensure there's a newline at the end and flush the buffer.
    out.write('\n');
    out.flush();
}
Also used : IonSystem(com.amazon.ion.IonSystem) IonWriter(com.amazon.ion.IonWriter)

Example 2 with IonSystem

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

the class SymbolTableTest method testMutateDomAfterLocalSymbolTableAppend.

@Test
public void testMutateDomAfterLocalSymbolTableAppend() throws IOException {
    String text = LocalSymbolTablePrefix + "{" + "   imports:[{name:\"foo\", version:1, max_id:1}], " + "   symbols:[\"s1\", \"s2\"]" + "}\n" + // Symbol with unknown text from "foo"
    "$10\n" + // s1
    "$11\n" + LocalSymbolTablePrefix + "{" + "   imports:" + ION_SYMBOL_TABLE + "," + "   symbols:[\"s3\"]" + "}\n" + // s2
    "$12";
    IonDatagram datagram = loader().load(text);
    datagram.add(1, system().newSymbol("abc"));
    // s3
    datagram.add(system().newSymbol(new SymbolTokenImpl(null, 13)));
    // Symbol with unknown text from "foo"
    datagram.add(system().newSymbol(new SymbolTokenImpl(null, 10)));
    ByteArrayOutputStream textOutput = new ByteArrayOutputStream();
    ByteArrayOutputStream binaryOutput = new ByteArrayOutputStream();
    List<String> fooSymbols = Collections.singletonList("bar");
    SymbolTable fooTable = system().newSharedSymbolTable("foo", 1, fooSymbols.iterator());
    IonWriter textWriter = IonTextWriterBuilder.standard().withImports(fooTable).build(textOutput);
    datagram.writeTo(textWriter);
    textWriter.close();
    IonWriter binaryWriter = IonBinaryWriterBuilder.standard().withImports(fooTable).build(binaryOutput);
    datagram.writeTo(binaryWriter);
    binaryWriter.close();
    SimpleCatalog catalog = new SimpleCatalog();
    catalog.putTable(fooTable);
    IonSystem system = IonSystemBuilder.standard().withCatalog(catalog).build();
    IonDatagram textRoundtrip = system.getLoader().load(textOutput.toByteArray());
    IonDatagram binaryRoundtrip = system.getLoader().load(binaryOutput.toByteArray());
    assertEquals(textRoundtrip, binaryRoundtrip);
    assertEquals("bar", ((IonSymbol) textRoundtrip.get(0)).stringValue());
    assertEquals("abc", ((IonSymbol) textRoundtrip.get(1)).stringValue());
    assertEquals("s1", ((IonSymbol) textRoundtrip.get(2)).stringValue());
    assertEquals("s2", ((IonSymbol) textRoundtrip.get(3)).stringValue());
    assertEquals("s3", ((IonSymbol) textRoundtrip.get(4)).stringValue());
    assertEquals("bar", ((IonSymbol) textRoundtrip.get(0)).stringValue());
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) IonSystem(com.amazon.ion.IonSystem) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with IonSystem

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

the class IteratorTiming method main.

public static void main(String[] args) {
    IonSystem ion = IonSystemBuilder.standard().build();
    System.out.println("Start at " + new Date());
    try {
        InputStream is = new FileInputStream(args[0]);
        Iterator<IonValue> itera = ion.iterate(new InputStreamReader(is));
        int count = 0;
        long start = System.currentTimeMillis();
        long millis = start;
        while (itera.hasNext()) {
            itera.next();
            if ((++count % 100) == 0) {
                long diff = System.currentTimeMillis() - millis;
                System.out.println(diff);
                millis = System.currentTimeMillis();
            }
        }
        long now = System.currentTimeMillis();
        long elapsed = now - start;
        System.out.println();
        System.out.println("End at " + new Date());
        System.out.println("Total millis: " + elapsed);
        System.out.println("# values: " + count);
        System.out.println("avg millis/value: " + ((float) elapsed) / count);
        Runtime rt = Runtime.getRuntime();
        System.out.println("Total memory: " + rt.totalMemory());
    } catch (IOException e) {
        System.err.println(e);
    }
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream)

Example 4 with IonSystem

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

the class BinaryStreamingTest 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);
    IonValue v2 = ((IonStruct) v).get("offline_store_only");
    SymbolTable sym = v.getSymbolTable();
    assert v2.getSymbolTable() == sym;
    IonReader ir = system().newReader(s);
    Iterator<String> symbols = sym.iterateDeclaredSymbolNames();
    SymbolTable u = system().newSharedSymbolTable("items", 1, symbols);
    IonBinaryWriter wr = system().newBinaryWriter(u);
    wr.writeValues(ir);
    byte[] buffer = wr.getBytes();
    dumpBuffer(buffer, buffer.length);
    ir = getStreamingMode().newIonReader(system().getCatalog(), buffer);
    wr = system().newBinaryWriter(u);
    wr.writeValues(ir);
    buffer = wr.getBytes();
    dumpBuffer(buffer, buffer.length);
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) IonBinaryWriter(com.amazon.ion.IonBinaryWriter) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 5 with IonSystem

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

the class SymbolTableTest method registerImportedV2.

public SymbolTable registerImportedV2() {
    IonSystem system = system();
    String importingText = SharedSymbolTablePrefix + "{" + "  name:'''imported''', version:2," + "  symbols:[" + "    '''imported 1'''," + "    '''imported 2'''," + "    '''fred3'''," + "    '''fred4'''," + "  ]" + "}";
    SymbolTable shared = registerSharedSymtab(importingText);
    assertEquals(IMPORTED_2_MAX_ID, shared.getMaxId());
    SymbolTable importedTable = system.getCatalog().getTable("imported", 2);
    assertSame(shared, importedTable);
    return importedTable;
}
Also used : IonSystem(com.amazon.ion.IonSystem) SymbolTable(com.amazon.ion.SymbolTable)

Aggregations

IonSystem (com.amazon.ion.IonSystem)39 Test (org.junit.Test)27 IonValue (com.amazon.ion.IonValue)12 SymbolTable (com.amazon.ion.SymbolTable)10 IonReader (com.amazon.ion.IonReader)8 IonWriter (com.amazon.ion.IonWriter)8 IonDatagram (com.amazon.ion.IonDatagram)6 IonStruct (com.amazon.ion.IonStruct)6 IonCatalog (com.amazon.ion.IonCatalog)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IonString (com.amazon.ion.IonString)4 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)4 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)4 BinaryTest (com.amazon.ion.BinaryTest)3 IonBinaryWriter (com.amazon.ion.IonBinaryWriter)2 IonType (com.amazon.ion.IonType)2 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)2 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 Date (java.util.Date)2