THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
CXF 3.2.0 has retained the implementation of the original JAX-RS 2.1 NIO API prototype and made it possible for the users to experiment with it.
NIO
...
Read
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(); } |
Even Easier NIO Write
Code Block | ||
---|---|---|
| ||
import org.apache.cxf.annotations.UseNio;
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/is")
@UseNio
public InputStream getBookStreamFromInputStream() throws IOException {
return getClass().getResourceAsStream("/files/books.txt");
} |