You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by ti...@apache.org on 2024/12/18 02:09:50 UTC
(opendal) 01/02: feat: impl configurable OperatorOutputStream maxBytes
This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch configurable-OperatorOutputStream-maxBytes
in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 66eff2de45474fd44161ec44bee21dc1da680a73
Author: tison <wa...@gmail.com>
AuthorDate: Wed Dec 18 09:53:48 2024 +0800
feat: impl configurable OperatorOutputStream maxBytes
Signed-off-by: tison <wa...@gmail.com>
---
.../java/org/apache/opendal/OperatorOutputStream.java | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java b/bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java
index 0f315174f..05afac416 100644
--- a/bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java
+++ b/bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java
@@ -35,31 +35,38 @@ public class OperatorOutputStream extends OutputStream {
}
}
- private static final int MAX_BYTES = 16384;
+ private static final int DEFAULT_MAX_BYTES = 16384;
private final Writer writer;
- private final byte[] bytes = new byte[MAX_BYTES];
+ private final byte[] bytes;
+ private final int maxBytes;
private int offset = 0;
public OperatorOutputStream(Operator operator, String path) {
+ this(operator, path, DEFAULT_MAX_BYTES);
+ }
+
+ public OperatorOutputStream(Operator operator, String path, int maxBytes) {
final long op = operator.nativeHandle;
this.writer = new Writer(constructWriter(op, path));
+ this.maxBytes = maxBytes;
+ this.bytes = new byte[maxBytes];
}
@Override
public void write(int b) throws IOException {
bytes[offset++] = (byte) b;
- if (offset >= MAX_BYTES) {
+ if (offset >= maxBytes) {
flush();
}
}
@Override
public void flush() throws IOException {
- if (offset > MAX_BYTES) {
- throw new IOException("INTERNAL ERROR: " + offset + " > " + MAX_BYTES);
- } else if (offset < MAX_BYTES) {
+ if (offset > maxBytes) {
+ throw new IOException("INTERNAL ERROR: " + offset + " > " + maxBytes);
+ } else if (offset < maxBytes) {
final byte[] bytes = Arrays.copyOf(this.bytes, offset);
writeBytes(writer.nativeHandle, bytes);
} else {