@Override public void onDataAvailable() throws IOException { while (in.isReady() && !in.isFinished()) { entity.getReader().read(in); } }
@Override public void onAllDataRead() throws IOException { entity.getCompletion().complete(); }
public NioWriteListenerImpl(Continuation cont, NioWriteEntity entity, OutputStream out) { this.cont = cont; this.entity = entity; this.out = new NioOutputStream(out); }
protected void copyUsingNio(InputStream is, OutputStream os, Continuation cont) { NioWriteListenerImpl listener = new NioWriteListenerImpl(cont, new NioWriteEntity(getNioHandler(is), null), new NioOutputStream(os)); Message m = JAXRSUtils.getCurrentMessage(); m.put(WriteListener.class, listener); cont.suspend(0); }
@GET @Produces(MediaType.TEXT_PLAIN) public Response getBookStream() throws IOException { final ByteArrayInputStream in = new ByteArrayInputStream( IOUtils.readBytesFromStream(getClass().getResourceAsStream("/files/books.txt"))); final byte[] buffer = new byte[4096]; return Response .ok() .entity( new NioWriteEntity( out -> { final int n = in.read(buffer); if (n >= 0) { out.write(buffer, 0, n); return true; } closeInputStream(in); return false; } // by default the runtime will throw the exception itself // if the error handler is not provided //, //throwable -> { // throw throwable; //} )) .build(); }
@Override public void onError(Throwable t) { if (entity.getError() == null) { getAsyncResponse().resume(t); return; } try { entity.getError().error(t); } catch (final Throwable ex) { LOG.warning("NIO NioReadListener error: " + ExceptionUtils.getStackTrace(ex)); } }
@Override public boolean write(NioOutputStream out) { try { final int n = in.read(buffer); if (n >= 0) { out.write(buffer, 0, n); return true; } if (closeResponseInputStream) { try { in.close(); } catch (IOException ex) { /* do nothing */ } } return false; } catch (IOException ex) { throw new WebApplicationException(ex); } } };
@Override public void writeTo(NioWriteEntity entity, Class<?> cls, Type t, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { Continuation cont = getContinuationProvider().getContinuation(); NioWriteListenerImpl listener = new NioWriteListenerImpl(cont, entity, os); Message m = JAXRSUtils.getCurrentMessage(); m.put(WriteListener.class, listener); // return the current thread to the pool cont.suspend(0); }
final LongAdder adder = new LongAdder(); new NioReadEntity( final int n = in.read(buffer); if (n > 0) { adder.add(n);
public NioReadListenerImpl(NioReadEntity entity, ServletInputStream in) { this.entity = entity; this.in = new NioInputStream(in); }
@Override public boolean isWriteable(Class<?> cls, Type type, Annotation[] anns, MediaType mt) { return is31 && NioWriteEntity.class.isAssignableFrom(cls) && getContinuationProvider() != null; }
public NioReadEntity(NioReadHandler reader, NioReadCompletionHandler completion, NioErrorHandler error) { this.reader = reader; this.completion = completion; this.error = error; final Message m = JAXRSUtils.getCurrentMessage(); try { if (m.get(AsyncResponse.class) == null) { throw new IllegalStateException("AsyncResponse is not available"); } final HttpServletRequest request = (HttpServletRequest)m.get(AbstractHTTPDestination.HTTP_REQUEST); request.getInputStream().setReadListener(new NioReadListenerImpl(this, request.getInputStream())); } catch (final Throwable ex) { throw new RuntimeException("Unable to initialize NIO entity", ex); } }
public static ServerProviderFactory createInstance(Bus bus) { if (bus == null) { bus = BusFactory.getThreadDefaultBus(); } ServerProviderFactory factory = new ServerProviderFactory(bus); ProviderFactory.initFactory(factory); factory.setProviders(false, false, new WebApplicationExceptionMapper(), new NioMessageBodyWriter()); factory.setBusProviders(); return factory; }