import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.Collection;
private final Deque<InputStream> stack = new ArrayDeque<>();
private Reader reader;
+ private ReadableByteChannel channel;
private boolean eof;
@Override
public int read(final byte[] array, final int off, final int len) throws IOException {
- return super.read(array, off, len);
+ if (eof) {
+ return -1;
+ }
+ if (channel == null) {
+ generator.writeStartObject();
+ generator.flush();
+ channel = Channels.newChannel(new ByteArrayInputStream(stream.toByteArray()));
+ stream.reset();
+ }
+
+ var read = channel.read(ByteBuffer.wrap(array, off, len));
+ while (read == -1) {
+ if (stack.isEmpty()) {
+ generator.writeEndObject();
+ generator.flush();
+ channel = Channels.newChannel(new ByteArrayInputStream(stream.toByteArray()));
+ stream.reset();
+ eof = true;
+ return channel.read(ByteBuffer.wrap(array, off, len));
+ }
+ channel = Channels.newChannel(stack.pop());
+ read = channel.read(ByteBuffer.wrap(array, off, len));
+ }
+
+ return read;
}
}