Search in sources :

Example 1 with NioReadEntity

use of org.apache.cxf.jaxrs.nio.NioReadEntity in project cxf by apache.

the class NioBookStore method uploadBookStream.

@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");
    });
}
Also used : NioReadEntity(org.apache.cxf.jaxrs.nio.NioReadEntity) LongAdder(java.util.concurrent.atomic.LongAdder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 LongAdder (java.util.concurrent.atomic.LongAdder)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 NioReadEntity (org.apache.cxf.jaxrs.nio.NioReadEntity)1