@Override public void close() throws IOException { if (closed.compareAndSet(false, true)) { closeQueue(); // we can now safely clear the queue - any blocking read waiting for a buffer // has been resumed by the EOF buffer buffers.clear(); } checkThrowable(); }
@Override public void close() throws IOException { if (mode == Mode.SYNCHRONOUS) { synchronousStream.close(); } }
@Override public int read(byte[] b, int off, int len) throws IOException { return tryRead(b, off, len, true); }
private synchronized void commitToMode() { // return if the mode has already been committed if (mode != Mode.UNDECIDED) { return; } // go asynchronous, if the user has made any move suggesting asynchronous mode if (readListener != null || listenerExecutor != null) { mode = Mode.ASYNCHRONOUS; return; } // go synchronous, if the user has not made any move suggesting asynchronous mode mode = Mode.SYNCHRONOUS; synchronousStream = new ByteBufferInputStream(); // move all buffered data to synchronous stream for (ByteBuffer b : data) { if (b == EOF) { synchronousStream.closeQueue(); } else if (b == ERROR) { synchronousStream.closeQueue(t); } else { try { synchronousStream.put(b); } catch (InterruptedException e) { synchronousStream.closeQueue(e); } } } }
private int tryRead(boolean block) throws IOException { checkThrowable(); checkNotClosed(); if (eof) { return -1; } if (current != null && current.hasRemaining()) { return current.get() & 0xFF; } try { // try to fetch, but don't block && check if something has been fetched if (fetchChunk(block) && current != null) { return current.get() & 0xFF; } else if (block) { return -1; } } catch (final InterruptedException e) { Thread.currentThread().interrupt(); if (block) { throw new IOException(e); } } return (eof) ? -1 : NOTHING; } }
@Override public Void onCompleted() throws Exception { entityStream.closeQueue(); return null; }
@Override public void onContent(final Response jettyResponse, final ByteBuffer content) { try { // content must be consumed before returning from this method. if (content.hasArray()) { byte[] array = content.array(); byte[] buff = new byte[content.remaining()]; System.arraycopy(array, content.arrayOffset(), buff, 0, content.remaining()); entityStream.put(ByteBuffer.wrap(buff)); } else { byte[] buff = new byte[content.remaining()]; content.get(buff); entityStream.put(ByteBuffer.wrap(buff)); } } catch (final InterruptedException ex) { final ProcessingException pe = new ProcessingException(ex); entityStream.closeQueue(pe); // try to complete the future with an exception responseFuture.completeExceptionally(pe); Thread.currentThread().interrupt(); } }
@Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; }
final ByteBufferInputStream entityStream = new ByteBufferInputStream(); final AtomicBoolean futureSet = new AtomicBoolean(false);
@Override public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { entityStream.put(bodyPart.getBodyByteBuffer()); return STATE.CONTINUE; }
private int tryRead(boolean block) throws IOException { checkThrowable(); checkNotClosed(); if (eof) { return -1; } if (current != null && current.hasRemaining()) { return current.get() & 0xFF; } try { // try to fetch, but don't block && check if something has been fetched if (fetchChunk(block) && current != null) { return current.get() & 0xFF; } else if (block) { return -1; } } catch (final InterruptedException e) { Thread.currentThread().interrupt(); if (block) { throw new IOException(e); } } return (eof) ? -1 : NOTHING; } }
@Override public Void onCompleted() throws Exception { entityStream.closeQueue(); return null; }
synchronized void notifyDataAvailable(ByteBuffer availableData) { assertClosedForInput(); if (!availableData.hasRemaining()) { return; } if (mode == Mode.SYNCHRONOUS) { try { synchronousStream.put(availableData); } catch (InterruptedException e) { synchronousStream.closeQueue(e); } return; } data.add(availableData); if (readListener != null && callReadListener) { callDataAvailable(); } }
@Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; }
final Request connectorRequest = translate(request); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest); final ByteBufferInputStream entityStream = new ByteBufferInputStream(); final AtomicBoolean callbackInvoked = new AtomicBoolean(false);
@Override public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { entityStream.put(bodyPart.getBodyByteBuffer()); return STATE.CONTINUE; }
private int tryRead(final byte[] b, final int off, final int len, boolean block) throws IOException { checkThrowable(); checkNotClosed(); if (!fetchChunk(block) || current == null) { break; // eof or no data
@Override public int tryRead(final byte[] b, final int off, final int len) throws IOException { return tryRead(b, off, len, false); }
@Override public void close() throws IOException { if (closed.compareAndSet(false, true)) { closeQueue(); // we can now safely clear the queue - any blocking read waiting for a buffer // has been resumed by the EOF buffer buffers.clear(); } checkThrowable(); }
@Override public void onComplete(final Result result) { entityStream.closeQueue(); callback.response(jerseyResponse.get()); responseFuture.complete(jerseyResponse.get()); }