THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block | ||
---|---|---|
| ||
@POST @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN) public void uploadBookStream(@Suspended AsyncResponse response) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final byte[] buffer = new byte[4096]; final LongAdder adder = new LongAdder(); new NioReadEntity( // read handler in -> { final int n = in.read(buffer); if (n > 0) { adder.add(n); out.write(buffer, 0, n); } }, // completion handler () -> { closeOutputStream(out); response.resume("Book Store uploaded: " + adder.longValue() + " bytes"); } // by default the runtime will resume AsyncResponse with Throwable itself // if the error handler is not provided //, // error handler //t -> { // response.resume(t); //} ); } |
NIO Write
Code Block | ||
---|---|---|
| ||
@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(); } |
...