Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/main/java/com/couchbase/jsonskiff/JsonStreamParser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2019 Couchbase, Inc.
* Copyright 2025 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -24,6 +24,7 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -81,6 +82,24 @@ private JsonStreamParser(PathTree pathTree) {
}
}

/**
* @throws UncheckedIOException if malformed JSON is detected in this chunk of input
* @throws RuntimeException if a value consumer throws an exception
*/
public void feed(InputStream is, int bufferSizeInBytes) {
// borrow the scratch buffer!
scratchBuffer.clear().ensureWritable(bufferSizeInBytes);

try {
int bytesRead;
while ((bytesRead = is.read(scratchBuffer.array())) != -1) {
feed(scratchBuffer.array(), 0, bytesRead);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Consumes all readable bytes from the given buffer. Searches for values matching
* the configured JSON pointers and invokes callbacks for any matches.
Expand All @@ -102,8 +121,7 @@ public void feed(byte[] array, int offset, int len) {
}

public void feed(ByteBuffer input) {
scratchBuffer.clear();
scratchBuffer.writeBytes(input);
scratchBuffer.clear().writeBytes(input);
feed(scratchBuffer.array(), 0, scratchBuffer.readableBytes());
}

Expand Down
Loading