From 3857f9c8404c6f251135268e42c79bb83c13b2b0 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 2 Dec 2020 23:45:39 -0800 Subject: Hadoop: switch to ByteBuffer fix https://github.com/chrislusf/seaweedfs/issues/1645 --- .../java/seaweed/hdfs/SeaweedFileSystemStore.java | 2 +- .../main/java/seaweed/hdfs/SeaweedInputStream.java | 31 +++++++++++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) (limited to 'other/java/hdfs3/src') diff --git a/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystemStore.java b/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystemStore.java index 2ef1a7468..14b32528e 100644 --- a/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystemStore.java +++ b/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystemStore.java @@ -288,4 +288,4 @@ public class SeaweedFileSystemStore { } -} \ No newline at end of file +} diff --git a/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedInputStream.java b/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedInputStream.java index 2cf544162..752f06374 100644 --- a/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedInputStream.java +++ b/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedInputStream.java @@ -86,11 +86,29 @@ public class SeaweedInputStream extends FSInputStream implements ByteBufferReada throw new IllegalArgumentException("requested read length is more than will fit after requested offset in buffer"); } + ByteBuffer buf = ByteBuffer.wrap(b, off, len); + return read(buf); + + } + + // implement ByteBufferReadable + @Override + public synchronized int read(ByteBuffer buf) throws IOException { + + if (position < 0) { + throw new IllegalArgumentException("attempting to read from negative offset"); + } + if (position >= contentLength) { + return -1; // Hadoop prefers -1 to EOFException + } + long bytesRead = 0; - if (position+len <= entry.getContent().size()) { - entry.getContent().copyTo(b, (int) position, (int) off, len); + int len = buf.remaining(); + int start = (int) this.position; + if (start+len <= entry.getContent().size()) { + entry.getContent().substring(start, start+len).copyTo(buf); } else { - bytesRead = SeaweedRead.read(this.filerGrpcClient, this.visibleIntervalList, this.position, b, off, len, SeaweedRead.fileSize(entry)); + bytesRead = SeaweedRead.read(this.filerGrpcClient, this.visibleIntervalList, this.position, buf, SeaweedRead.fileSize(entry)); } if (bytesRead > Integer.MAX_VALUE) { @@ -105,13 +123,6 @@ public class SeaweedInputStream extends FSInputStream implements ByteBufferReada } return (int) bytesRead; - - } - - // implement ByteBufferReadable - @Override - public synchronized int read(ByteBuffer buf) throws IOException { - return read(buf.array(), buf.position(), buf.remaining()); } /** -- cgit v1.2.3