Skip to content

Commit 1991e5b

Browse files
authored
Update IonReader/IonReaderBuilder docs w/ details on obtaining instances (#1099)
1 parent 0100402 commit 1991e5b

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎src/main/java/com/amazon/ion/IonReader.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@
6363
* {@link IonValue} hierarchy. For example, to get the text of a symbol one
6464
* would use {@link #stringValue()}, mirroring {@link IonSymbol#stringValue()}.
6565
*
66+
* <h2>Obtaining and Usage</h2>
67+
* Instances of {@code IonReader} may be constructed through builders, which
68+
* implement {@code IonReaderBuilder}. An {@code IonReaderBuilder} with the
69+
* default configuration may be constructed as follows. This builder will
70+
* construct {@code IonReader} instances which can read both text and binary
71+
* Ion data.
72+
* {@snippet :
73+
* IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
74+
* }
75+
* See {@link com.amazon.ion.system.IonReaderBuilder} for more information
76+
* on configuring {@code IonReaderBuilder}.
77+
* <p>
78+
* An {@code IonReader} may be obtained from this builder by calling
79+
* {@code build} over the appropriate data source. Below is an example of
80+
* a reader being constructed over a string containing Ion text.
81+
* {@snippet :
82+
* final String helloWorld = "{hello: \"world\"}";
83+
* try (final IonReader reader = readerBuilder.build(helloWorld)) {
84+
* reader.next();
85+
* reader.stepIn();
86+
* reader.next();
87+
* System.out.println(reader.getFieldName() + " " + reader.stringValue()); // prints "hello world"
88+
* }
89+
* }
90+
* Below is an example of a reader being constructed over a byte array
91+
* containing the same data in Ion binary.
92+
* {@snippet :
93+
* final byte[] helloWorld = new byte[] { (byte) 0xe0, 0x01, /* ... *​/, 0x6c, 0x64 };
94+
* try (final IonReader reader = readerBuilder.build(helloWorld)) {
95+
* reader.next();
96+
* reader.stepIn();
97+
* reader.next();
98+
* System.out.println(reader.getFieldName() + " " + reader.stringValue()); // prints "hello world"
99+
* }
100+
* }
101+
* See {@link com.amazon.ion.system.IonReaderBuilder} to obtain a reader
102+
* over other data sources.
103+
*
66104
* <h2>Exception Handling</h2>
67105
* {@code IonReader} is a generic interface for traversing Ion data, and it's
68106
* not possible to fully specify the set of exceptions that could be thrown

‎src/main/java/com/amazon/ion/system/IonReaderBuilder.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,57 @@
3131
* {@link IonReader}s parse incrementally, so syntax errors in the input data
3232
* will not be detected as side effects of any of the {@code build} methods
3333
* in this class.
34+
*
35+
* <h2>Obtaining and Usage</h2>
36+
* An {@code IonReaderBuilder} with the default configuration may be constructed
37+
* as follows. This builder will construct {@code IonReader} instances which
38+
* can read both text and binary Ion data and is appropriate for simple use
39+
* cases or when no IonCatalog is in use.
40+
* {@snippet :
41+
* IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
42+
* }
43+
* {@code IonReaderBuilder}s can be configured by chaining calls to {@code with*()}
44+
* configuration methods. Below is an example of a builder configured to
45+
* incrementally read Ion binary data, use a custom initial buffer size, throw
46+
* on inputs that would require a buffer over a specified size, and use a
47+
* user-provided IonCatalog.
48+
* {@snippet :
49+
* // Create an IonCatalog and IonBufferConfiguration to use with IonReaderBuilder
50+
* final IonCatalog catalog = new SimpleCatalog();
51+
* final IonBufferConfiguration bufferConfiguration = IonBufferConfiguration.Builder.standard()
52+
* .onOversizedSymbolTable(() -> { throw new IllegalStateException("Oversized system table encountered"); })
53+
* .onOversizedValue(() -> { throw new IllegalStateException("Oversized value encountered"); })
54+
* .withInitialBufferSize(1024)
55+
* .withMaximumBufferSize(1024 * 1024)
56+
* .build();
57+
*
58+
* IonReaderBuilder readerBuilder = IonReaderBuilder.standard()
59+
* .withIncrementalReadingEnabled(true)
60+
* .withBufferConfiguration(bufferConfiguration)
61+
* .withCatalog(catalog);
62+
* }
63+
*
64+
* <h3>Building a Reader over a Data Source</h3>
65+
* An {@code IonReader} may be obtained from a builder by calling {@code build}
66+
* over the appropriate data source. Below is an example of a reader being constructed
67+
* over a string containing Ion text from a builder with the default configuration.
68+
* {@snippet :
69+
* IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
70+
* final String helloWorld = "{hello: \"world\"}";
71+
* try (final IonReader reader = readerBuilder.build(helloWorld)) {
72+
* reader.next();
73+
* reader.stepIn();
74+
* reader.next();
75+
* System.out.println(reader.getFieldName() + " " + reader.stringValue()); // prints "hello world"
76+
* }
77+
* }
78+
* Builders can build an IonReader over a string, {@code byte[]} array,
79+
* {@link java.io.InputStream}, {@link java.io.Reader}, or existing {@link com.amazon.ion.IonValue}
80+
* data model. Building a reader over a byte array allows specifying the start
81+
* index and length of the data to be read. Readers built over {@code byte[]}
82+
* arrays or {@code InputStream}s are capable of reading both binary and text
83+
* Ion; readers built over strings and {@code Reader} instances are only capable of
84+
* reading text Ion.
3485
*/
3586
@SuppressWarnings("deprecation")
3687
public abstract class IonReaderBuilder

0 commit comments

Comments
 (0)