Repository: commons-compress Updated Branches: refs/heads/master d9fe818ee -> a2976b66b
modify SeekableInMemoryByteChannel based on feedback by Maciej and Bernd Eckenfels Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/a2976b66 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/a2976b66 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/a2976b66 Branch: refs/heads/master Commit: a2976b66b5590c7d0225ca111dabd2b048d25951 Parents: d9fe818 Author: Stefan Bodewig <bode...@apache.org> Authored: Fri Oct 21 17:51:10 2016 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Fri Oct 21 17:51:10 2016 +0200 ---------------------------------------------------------------------- .../utils/SeekableInMemoryByteChannel.java | 30 +++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/a2976b66/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java index 87c2111..61aaf71 100644 --- a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java +++ b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java @@ -24,16 +24,18 @@ import java.nio.channels.ClosedChannelException; import java.nio.channels.NonWritableChannelException; import java.nio.channels.SeekableByteChannel; import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; /** * A {@link SeekableByteChannel} implementation that wraps a byte[]. * @since 1.13 + * @NotThreadSafe */ public class SeekableInMemoryByteChannel implements SeekableByteChannel { - private volatile byte[] data; - private volatile boolean closed; - private volatile int position, size; + private byte[] data; + private final AtomicBoolean closed = new AtomicBoolean(); + private int position, size; public SeekableInMemoryByteChannel(byte[] data) { this.data = data; @@ -79,26 +81,24 @@ public class SeekableInMemoryByteChannel implements SeekableByteChannel { if (!isOpen()) { throw new ClosedChannelException(); } - int pos = position; - int sz = size; int wanted = buf.remaining(); - int possible = sz - pos; + int possible = size - position; if (wanted > possible) { wanted = possible; } - buf.put(data, pos, wanted); - position = pos + wanted; + buf.put(data, position, wanted); + position += wanted; return wanted; } @Override public void close() { - closed = true; + closed.set(true); } @Override public boolean isOpen() { - return !closed; + return !closed.get(); } @Override @@ -106,15 +106,13 @@ public class SeekableInMemoryByteChannel implements SeekableByteChannel { if (!isOpen()) { throw new ClosedChannelException(); } - int pos = position; - int sz = data.length; int wanted = b.remaining(); - int possibleWithoutResize = sz - pos; + int possibleWithoutResize = size - position; if (wanted > possibleWithoutResize) { - resize(pos + wanted); + resize(position + wanted); } - b.get(data, pos, wanted); - position = pos + wanted; + b.get(data, position, wanted); + position += wanted; if (size < position) { size = position; }