This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 92bd09fe0 chore: fine tune OperatorInputStream (#7089)
92bd09fe0 is described below
commit 92bd09fe00c50297a7fe5cd1bf4b5e79b2c658d8
Author: tison <[email protected]>
AuthorDate: Tue Dec 23 00:45:50 2025 +0800
chore: fine tune OperatorInputStream (#7089)
Signed-off-by: tison <[email protected]>
---
.../org/apache/opendal/OperatorInputStream.java | 49 +++++++++++++---------
.../org/apache/opendal/OperatorOutputStream.java | 12 +++---
2 files changed, 36 insertions(+), 25 deletions(-)
diff --git
a/bindings/java/src/main/java/org/apache/opendal/OperatorInputStream.java
b/bindings/java/src/main/java/org/apache/opendal/OperatorInputStream.java
index 0bf774c8f..870b73a03 100644
--- a/bindings/java/src/main/java/org/apache/opendal/OperatorInputStream.java
+++ b/bindings/java/src/main/java/org/apache/opendal/OperatorInputStream.java
@@ -19,8 +19,8 @@
package org.apache.opendal;
-import java.io.IOException;
import java.io.InputStream;
+import java.util.Objects;
public class OperatorInputStream extends InputStream {
private static class Reader extends NativeObject {
@@ -45,7 +45,7 @@ public class OperatorInputStream extends InputStream {
}
@Override
- public int read() throws IOException {
+ public int read() {
if (bytes != null && offset >= bytes.length) {
bytes = readNextBytes(reader.nativeHandle);
offset = 0;
@@ -59,34 +59,43 @@ public class OperatorInputStream extends InputStream {
}
@Override
- public int read(byte[] b, int off, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
+ public int read(byte[] b, int off, int len) {
+ Objects.requireNonNull(b);
+ if ((b.length | off | len) < 0 || len > b.length - off) {
+ // Objects.checkFromIndexSize has only been available since Java 9
+ throw new IndexOutOfBoundsException(
+ String.format("Range [%s, %<s + %s) out of bounds for
length %s", off, len, b.length));
}
- if (off < 0 || len < 0 || len > b.length - off) {
- throw new IndexOutOfBoundsException();
- }
- if (len == 0) {
- return 0;
+
+ int read = 0;
+ while (len > 0) {
+ if (bytes != null && offset >= bytes.length) {
+ bytes = readNextBytes(reader.nativeHandle);
+ offset = 0;
+ }
+
+ if (bytes == null) {
+ return read != 0 ? read : -1;
+ }
+
+ final int n = Math.min(len, bytes.length - offset);
+ System.arraycopy(bytes, offset, b, off, n);
+ offset += n;
+ off += n;
+ read += n;
+ len -= n;
}
- while (bytes != null && offset >= bytes.length) {
+ if (bytes != null && offset >= bytes.length) {
bytes = readNextBytes(reader.nativeHandle);
offset = 0;
}
- if (bytes == null) {
- return -1;
- }
-
- final int n = Math.min(len, bytes.length - offset);
- System.arraycopy(bytes, offset, b, off, n);
- offset += n;
- return n;
+ return bytes != null ? read : (read != 0 ? read : -1);
}
@Override
- public void close() throws IOException {
+ public void close() {
reader.close();
}
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 05afac416..6e2a53541 100644
--- a/bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java
+++ b/bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java
@@ -57,15 +57,17 @@ public class OperatorOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
bytes[offset++] = (byte) b;
- if (offset >= maxBytes) {
- flush();
+ if (offset != maxBytes) {
+ return;
}
+ flush();
}
@Override
- public void flush() throws IOException {
+ public void flush() {
if (offset > maxBytes) {
- throw new IOException("INTERNAL ERROR: " + offset + " > " +
maxBytes);
+ // unreachable
+ throw new IllegalStateException("INTERNAL ERROR: " + offset + " >
" + maxBytes);
} else if (offset < maxBytes) {
final byte[] bytes = Arrays.copyOf(this.bytes, offset);
writeBytes(writer.nativeHandle, bytes);
@@ -76,7 +78,7 @@ public class OperatorOutputStream extends OutputStream {
}
@Override
- public void close() throws IOException {
+ public void close() {
flush();
writer.close();
}