use of com.amazon.ion.system.IonReaderBuilder in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeValueDetectedDuringSingleByteReadIncremental.
@Test
public void oversizeValueDetectedDuringSingleByteReadIncremental() throws Exception {
// Requires a 2-byte header.
byte[] bytes = toBinary("\"abcdefghijklmnopqrstuvwxyz\"");
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);
}
};
ResizingPipedInputStream pipe = new ResizingPipedInputStream(bytes.length);
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(5).withMaximumBufferSize(5).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
for (byte b : bytes) {
assertNull(reader.next());
pipe.receive(b);
}
// The maximum buffer size is 5, which will be exceeded after the IVM (4 bytes), the type ID (1 byte), and
// the length byte (1 byte).
assertNull(reader.next());
reader.close();
assertEquals(1, oversizedCounter.get());
assertEquals(bytes.length, byteCounter.get());
}
use of com.amazon.ion.system.IonReaderBuilder in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeValueWithSymbolTableIncremental.
@Test
public void oversizeValueWithSymbolTableIncremental() 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);
}
};
ResizingPipedInputStream pipe = new ResizingPipedInputStream(out.size());
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(48).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
byte[] bytes = out.toByteArray();
boolean foundValue = false;
for (byte b : bytes) {
pipe.receive(b);
IonType type = reader.next();
if (type != null) {
assertFalse(foundValue);
assertEquals(IonType.SYMBOL, type);
assertEquals("abcdefghijklmnopqrstuvwxyz", reader.stringValue());
assertEquals(1, oversizedCounter.get());
foundValue = true;
}
}
assertTrue(foundValue);
assertNull(reader.next());
reader.close();
assertEquals(1, oversizedCounter.get());
assertEquals(out.size(), byteCounter.get());
}
use of com.amazon.ion.system.IonReaderBuilder in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInHeaderIncremental.
private void oversizeSymbolTableDetectedInHeaderIncremental(int maximumBufferSize) throws Exception {
// The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
// for symbol 10.
byte[] bytes = toBinary("abcdefghijklmnopqrstuvwxyz");
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(bytes.length);
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(maximumBufferSize).withMaximumBufferSize(maximumBufferSize).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
for (byte b : bytes) {
assertNull(reader.next());
pipe.receive(b);
}
assertNull(reader.next());
assertEquals(1, oversizedCounter.get());
reader.close();
}
use of com.amazon.ion.system.IonReaderBuilder in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeValueDetectedDuringMultiByteReadIncremental.
@Test
public void oversizeValueDetectedDuringMultiByteReadIncremental() throws Exception {
byte[] bytes = toBinary(// Requires 32 bytes (4 IVM, 1 TID, 1 length, 26 chars)
"\"abcdefghijklmnopqrstuvwxyz\" " + "\"abc\" " + "\"abcdefghijklmnopqrstuvwxyz\" " + "\"def\"");
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);
}
};
ResizingPipedInputStream pipe = new ResizingPipedInputStream(bytes.length);
IonReaderBuilder builder = IonReaderBuilder.standard().withBufferConfiguration(IonBufferConfiguration.Builder.standard().withInitialBufferSize(8).withMaximumBufferSize(16).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, pipe);
int valueCounter = 0;
for (byte b : bytes) {
pipe.receive(b);
IonType type = reader.next();
if (type != null) {
valueCounter++;
assertTrue(valueCounter < 3);
if (valueCounter == 1) {
assertEquals(IonType.STRING, type);
assertEquals("abc", reader.stringValue());
assertEquals(1, oversizedCounter.get());
} else {
assertEquals(2, valueCounter);
assertEquals(IonType.STRING, type);
assertEquals("def", reader.stringValue());
assertEquals(2, oversizedCounter.get());
}
}
}
assertEquals(2, valueCounter);
assertNull(reader.next());
reader.close();
assertEquals(2, oversizedCounter.get());
assertEquals(bytes.length, byteCounter.get());
}
use of com.amazon.ion.system.IonReaderBuilder in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method oversizeSymbolTableDetectedInHeader.
private void oversizeSymbolTableDetectedInHeader(int maximumBufferSize) throws Exception {
// The system values require ~40 bytes (4 IVM, 5 symtab struct header, 1 'symbols' sid, 2 list header, 2 + 26
// for symbol 10.
byte[] bytes = toBinary("abcdefghijklmnopqrstuvwxyz");
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(maximumBufferSize).withMaximumBufferSize(maximumBufferSize).onOversizedValue(handler).onOversizedSymbolTable(handler).onData(handler).build());
IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(builder, new ByteArrayInputStream(bytes));
assertNull(reader.next());
assertEquals(1, oversizedCounter.get());
reader.close();
}
Aggregations