use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInTheMiddle.
@Test
public void oversizeSymbolTableDetectedInTheMiddle() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
writer.writeString("12345678");
writer.finish();
// Requires 32 bytes (4 IVM, 1 TID, 1 length, 26 chars)
writer.writeSymbol("abcdefghijklmnopqrstuvwxyz");
writer.close();
// The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
// for symbol 10.
final AtomicInteger oversizedCounter = new AtomicInteger();
UnifiedTestHandler handler = new UnifiedTestHandler() {
@Override
public void onOversizedSymbolTable() {
oversizedCounter.incrementAndGet();
}
@Override
public void onOversizedValue() {
throw new IllegalStateException("not expected");
}
@Override
public void onData(int numberOfBytes) {
}
};
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(32).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(out.toByteArray()));
assertEquals(IonType.STRING, reader.next());
assertEquals("12345678", reader.stringValue());
assertNull(reader.next());
assertEquals(1, oversizedCounter.get());
reader.close();
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeValueWithSymbolTable.
@Test
public void oversizeValueWithSymbolTable() throws Exception {
// The first value is oversized because it cannot be buffered at the same time as the preceding symbol table
// without exceeding the maximum buffer size. The next value fits.
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
writer.writeString("12345678");
// Requires 32 bytes (4 IVM, 1 TID, 1 length, 26 chars)
writer.writeSymbol("abcdefghijklmnopqrstuvwxyz");
writer.close();
// The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
// for symbol 10.
// The string "12345678" requires 9 bytes, bringing the total to ~49, above the max of 48.
final AtomicInteger oversizedCounter = new AtomicInteger();
final AtomicInteger byteCounter = new AtomicInteger();
UnifiedTestHandler handler = new UnifiedTestHandler() {
@Override
public void onOversizedSymbolTable() {
throw new IllegalStateException("not expected");
}
@Override
public void onOversizedValue() {
oversizedCounter.incrementAndGet();
}
@Override
public void onData(int numberOfBytes) {
byteCounter.addAndGet(numberOfBytes);
}
};
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(48).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(out.toByteArray()));
assertEquals(IonType.SYMBOL, reader.next());
assertEquals(1, oversizedCounter.get());
assertEquals("abcdefghijklmnopqrstuvwxyz", reader.stringValue());
assertNull(reader.next());
reader.close();
assertEquals(1, oversizedCounter.get());
assertEquals(out.size(), byteCounter.get());
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method structsWithFloat32AndPreallocatedLength.
@Test
public void structsWithFloat32AndPreallocatedLength() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = _Private_IonManagedBinaryWriterBuilder.create(_Private_IonManagedBinaryWriterBuilder.AllocatorMode.BASIC).withPaddedLengthPreallocation(2).withFloatBinary32Enabled().newWriter(out);
writeFirstStruct(writer);
writeSecondStruct(writer);
writer.close();
IonReader reader = newBoundedIncrementalReader(out.toByteArray(), 64);
assertFirstStruct(reader);
assertSecondStruct(reader);
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInTheMiddleIncremental.
@Test
public void oversizeSymbolTableDetectedInTheMiddleIncremental() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
writer.writeString("12345678");
writer.finish();
// Requires 32 bytes (4 IVM, 1 TID, 1 length, 26 chars)
writer.writeSymbol("abcdefghijklmnopqrstuvwxyz");
writer.close();
// The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
// for symbol 10.
final AtomicInteger oversizedCounter = new AtomicInteger();
UnifiedTestHandler handler = new UnifiedTestHandler() {
@Override
public void onOversizedSymbolTable() {
oversizedCounter.incrementAndGet();
}
@Override
public void onOversizedValue() {
throw new IllegalStateException("not expected");
}
@Override
public void onData(int numberOfBytes) {
}
};
ResizingPipedInputStream pipe = new ResizingPipedInputStream(out.size());
byte[] bytes = out.toByteArray();
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(32).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
boolean foundValue = false;
for (byte b : bytes) {
IonType type = reader.next();
if (type != null) {
assertFalse(foundValue);
assertEquals(IonType.STRING, type);
assertEquals("12345678", reader.stringValue());
foundValue = true;
}
pipe.receive(b);
}
assertTrue(foundValue);
assertNull(reader.next());
assertEquals(1, oversizedCounter.get());
reader.close();
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class BinaryWriterTest method testManyAnnotations.
@Test
public void testManyAnnotations() throws Exception {
// TT72090065 - >= 14 bytes of annotation causes a length miscalculation
final IonWriter out = makeWriter();
out.stepIn(IonType.SEXP);
{
for (int i = 0; i < MANY_ANNOTATION_LENGTH; i++) {
out.addTypeAnnotation(MANY_ANNOTATION_SYMBOL_TEXT);
}
out.writeSymbol(MANY_ANNOTATION_SYMBOL_TEXT);
}
out.stepOut();
out.close();
final IonReader in = reread();
assertEquals(IonType.SEXP, in.next());
in.stepIn();
{
assertEquals(IonType.SYMBOL, in.next());
final String[] ann = in.getTypeAnnotations();
assertEquals(MANY_ANNOTATION_LENGTH, ann.length);
for (int i = 0; i < MANY_ANNOTATION_LENGTH; i++) {
assertEquals(MANY_ANNOTATION_SYMBOL_TEXT, ann[i]);
}
assertEquals(null, in.next());
}
in.stepOut();
assertEquals(null, in.next());
}
Aggregations