Search in sources :

Example 16 with IonWriter

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

the class IonReaderBinaryIncrementalTest method timestamps.

@Test
public void timestamps() throws Exception {
    final List<Timestamp> timestamps = new ArrayList<Timestamp>();
    timestamps.add(Timestamp.valueOf("2000T"));
    timestamps.add(Timestamp.valueOf("2000-01T"));
    timestamps.add(Timestamp.valueOf("2000-01-02T"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.6Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.06Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.006Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.600Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.060Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.060-07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.060+07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05+07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04+07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.9999999Z"));
    IonReaderBinaryIncremental reader = readerFor(new WriterFunction() {

        @Override
        public void write(IonWriter writer) throws IOException {
            for (Timestamp timestamp : timestamps) {
                writer.writeTimestamp(timestamp);
            }
        }
    });
    for (Timestamp timestamp : timestamps) {
        assertEquals(IonType.TIMESTAMP, reader.next());
        assertEquals(timestamp, reader.timestampValue());
    }
    assertNull(reader.next());
    assertNull(reader.getType());
    reader.close();
}
Also used : ArrayList(java.util.ArrayList) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Example 17 with IonWriter

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

the class IonReaderLookaheadBufferTest method toBytes.

@Override
byte[] toBytes(String textIon) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    _Private_IonSystem system = (_Private_IonSystem) IonSystemBuilder.standard().build();
    IonReader reader = system.newSystemReader(textIon);
    IonWriter writer = IonBinaryWriterBuilder.standard().build(output);
    writer.writeValues(reader);
    reader.close();
    writer.close();
    return output.toByteArray();
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 18 with IonWriter

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

the class IonReaderBinaryRawStringTest method testReadShortStrings.

@Test
public void testReadShortStrings() throws Exception {
    // This test constructs some strings with non-ascii text that are short enough to fit in
    // IonBinaryReaderRawX's reusable decoding buffers and then round-trips them.
    // Laughing face with tears
    String adage = "Brevity is the soul of wit. \uD83D\uDE02";
    // Airplane/fork+knife+plate
    String observation = "What's the deal with airline food? \u2708\uFE0F\uD83C\uDF7D️";
    // Thumbs up
    String litotes = "Not bad. \uD83D\uDC4D";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
    writer.writeString(adage);
    writer.writeString(observation);
    writer.writeString(litotes);
    writer.close();
    byte[] data = out.toByteArray();
    IonReader reader = IonReaderBuilder.standard().build(data);
    reader.next();
    assertEquals(adage, reader.stringValue());
    reader.next();
    assertEquals(observation, reader.stringValue());
    reader.next();
    assertEquals(litotes, reader.stringValue());
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 19 with IonWriter

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

the class IonReaderBinaryRawStringTest method testReadLargeStrings.

@Test
public void testReadLargeStrings() throws Exception {
    // This test constructs some strings with non-ascii text that are large enough to exceed the size
    // of IonBinaryReaderRawX's reusable decoding buffers and then round-trips them.
    String verse = // Musical notes emoji
    "\uD83C\uDFB6 This is the song that never ends\n" + "\uD83C\uDFB5 Yes it goes on and on, my friends!\n" + "\uD83C\uDFB6 Some people started singing it not knowing what it was\n" + "\uD83C\uDFB5 And they'll continue singing it forever just because...\n";
    final int numberOfVerses = 1024;
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < numberOfVerses; i++) {
        stringBuilder.append(verse);
    }
    String longSong = stringBuilder.toString();
    for (int i = 0; i < numberOfVerses; i++) {
        stringBuilder.append(verse);
    }
    String longerSong = stringBuilder.toString();
    for (int i = 0; i < numberOfVerses; i++) {
        stringBuilder.append(verse);
    }
    String longestSong = stringBuilder.toString();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
    writer.writeString(longSong);
    writer.writeString(longerSong);
    writer.writeString(longestSong);
    writer.close();
    byte[] songData = out.toByteArray();
    IonReader reader = IonReaderBuilder.standard().build(songData);
    reader.next();
    assertEquals(longSong, reader.stringValue());
    reader.next();
    assertEquals(longerSong, reader.stringValue());
    reader.next();
    assertEquals(longestSong, reader.stringValue());
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 20 with IonWriter

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

the class IonMarkupWriterFilesTest method testPrintingStandard.

/**
 * Tests to make sure that the type, the output stream, and the
 * annotation/field name aren't null for all the files. This will fail if
 * the callback type isn't being set.
 * {@link IonMarkupWriterTest#testStandardCallback()} should verify they're
 * set correctly.
 */
@Test
public void testPrintingStandard() throws Exception {
    // Just ignore the output, since we're not checking it
    OutputStream out = new NullOutputStream();
    InputStream in = new FileInputStream(myTestFile);
    // Get a Test markup writer
    _Private_IonTextWriterBuilder builder = (_Private_IonTextWriterBuilder) IonTextWriterBuilder.standard();
    IonWriter ionWriter = builder.withCallbackBuilder(new TestMarkupCallback.Builder()).build(out);
    // Load file into a reader
    IonReader ionReader = system().newReader(in);
    assertNotNull("ionReader is null", ionReader);
    assertNotNull("ionWriter is null", ionWriter);
    // Write data to the writer
    ionWriter.writeValues(ionReader);
    // Close the writer and reader
    ionWriter.close();
    ionReader.close();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) OutputStream(java.io.OutputStream) NullOutputStream(com.amazon.ion.util.NullOutputStream) IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonWriter(com.amazon.ion.IonWriter) FileInputStream(java.io.FileInputStream) NullOutputStream(com.amazon.ion.util.NullOutputStream) 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