ObjectDownloadListener#onWritePossible: Make code spec compatible

Current code violates the ServletOutputStream contract. For every
out.isReady() == true either write or close of that ServletOutputStream
should be called.

See also this issue upstream for more context: [1].

[1] https://github.com/eclipse/jetty.project/issues/2911

Change-Id: Ied575f3603a6be0d2dafc6c3329d685fc212c7a3
Signed-off-by: David Ostrovsky <david@ostrovsky.org>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
David Ostrovsky 2018-09-15 21:09:51 +02:00 committed by David Ostrovsky
parent f8e514c74a
commit 5c134f4d42
1 changed files with 19 additions and 13 deletions

View File

@ -80,7 +80,7 @@ public class ObjectDownloadListener implements WriteListener {
private final WritableByteChannel outChannel; private final WritableByteChannel outChannel;
private final ByteBuffer buffer = ByteBuffer.allocateDirect(8192); private ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
/** /**
* @param repository * @param repository
@ -115,20 +115,26 @@ public ObjectDownloadListener(FileLfsRepository repository,
@Override @Override
public void onWritePossible() throws IOException { public void onWritePossible() throws IOException {
while (out.isReady()) { while (out.isReady()) {
if (in.read(buffer) != -1) { try {
buffer.flip(); buffer.clear();
outChannel.write(buffer); if (in.read(buffer) < 0) {
buffer.compact(); buffer = null;
} else { } else {
in.close();
buffer.flip(); buffer.flip();
while (out.isReady()) {
if (buffer.hasRemaining()) {
outChannel.write(buffer);
} else {
context.complete();
return;
} }
} catch(Throwable t) {
LOG.log(Level.SEVERE, t.getMessage(), t);
buffer = null;
} finally {
if (buffer != null) {
outChannel.write(buffer);
} else {
try {
out.close();
} finally {
context.complete();
}
return;
} }
} }
} }