Skip to content

Fixes #468 #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2017, Joyent, Inc. All rights reserved.
* Copyright (c) 2016-2019, Joyent, Inc. All rights reserved.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2020

*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -30,7 +30,8 @@
* Manta. Connection opening to the remote server happens lazily upon the
* first read() or size() method invoked.
*
* @author Elijah Zupancic
* @author <a href="https://github.com/dekobon">Elijah Zupancic</a>
* @author <a href="https://github.com/nairashwin952013">Ashwin A Nair</a>
*/
public class MantaSeekableByteChannel extends InputStream
implements SeekableByteChannel {
Expand Down Expand Up @@ -186,7 +187,7 @@ protected MantaSeekableByteChannel(final AtomicReference<HttpUriRequest> request
}

@Override
public int read(final ByteBuffer dst) throws IOException {
public int read(final ByteBuffer dst) throws IOException, UnsupportedOperationException {
if (!open) {
throw new ClosedChannelException();
}
Expand All @@ -198,12 +199,28 @@ public int read(final ByteBuffer dst) throws IOException {
return EOF;
}

final byte[] buff = dst.array();
final int bytesRead = stream.read(buff);
if (dst.isReadOnly()) {
throw new MantaClientException("Read-only buffer", new IllegalArgumentException());
}

if (!dst.hasArray()) {
throw new MantaClientException("Buffer read not backed by an accessible\n"
+ "byte array", new UnsupportedOperationException());
} else {

final byte[] buff = new byte[dst.remaining()];
ByteBuffer dstCopy = dst.get(buff);
dst.position(dstCopy.position() - buff.length); // Restores the buffer position

position.addAndGet(bytesRead);
final int startIndex = dstCopy.arrayOffset();
final int endIndex = startIndex + dstCopy.position() + dstCopy.remaining();
final int buffLength = endIndex - startIndex;
final int bytesRead = stream.read(buff, startIndex, buffLength);

return bytesRead;
this.position.addAndGet(bytesRead);

return bytesRead;
}
}

/**
Expand Down