1 package org.simantics.scl.rest;
3 import java.io.FilterInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import javax.ws.rs.WebApplicationException;
8 import javax.ws.rs.ext.ReaderInterceptor;
9 import javax.ws.rs.ext.ReaderInterceptorContext;
11 import org.simantics.scl.runtime.function.Function1;
12 import org.simantics.scl.runtime.tuple.Tuple0;
14 public class ReadProgressInterceptor implements ReaderInterceptor {
16 private Function1<Long, Tuple0> callback;
18 public ReadProgressInterceptor(Function1<Long, Tuple0> callback) {
19 this.callback = callback;
23 public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
25 final InputStream is = context.getInputStream();
27 context.setInputStream(new FilterInputStream(is) {
32 public int read(byte b[], int off, int len) throws IOException {
33 int read = in.read(b, off, len);
36 callback.apply(count);
42 public int read() throws IOException {
46 callback.apply(count);
52 return context.proceed();