X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.rest%2Fsrc%2Forg%2Fsimantics%2Fscl%2Frest%2FReadProgressInterceptor.java;fp=bundles%2Forg.simantics.scl.rest%2Fsrc%2Forg%2Fsimantics%2Fscl%2Frest%2FReadProgressInterceptor.java;h=6b3d72ffc42e9931afb4e67c2873d0e18cd023cd;hb=fd0240aa431974075a4053c763994310cebcb342;hp=0000000000000000000000000000000000000000;hpb=8daf1d98d61e83fc6450ddac8e800476a9d52ab3;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/ReadProgressInterceptor.java b/bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/ReadProgressInterceptor.java new file mode 100644 index 000000000..6b3d72ffc --- /dev/null +++ b/bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/ReadProgressInterceptor.java @@ -0,0 +1,55 @@ +package org.simantics.scl.rest; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.ReaderInterceptor; +import javax.ws.rs.ext.ReaderInterceptorContext; + +import org.simantics.scl.runtime.function.Function1; +import org.simantics.scl.runtime.tuple.Tuple0; + +public class ReadProgressInterceptor implements ReaderInterceptor { + + private Function1 callback; + + public ReadProgressInterceptor(Function1 callback) { + this.callback = callback; + } + + @Override + public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException { + + final InputStream is = context.getInputStream(); + + context.setInputStream(new FilterInputStream(is) { + + private long count; + + @Override + public int read(byte b[], int off, int len) throws IOException { + int read = in.read(b, off, len); + if (read > 0) { + count += read; + callback.apply(count); + } + return read; + } + + @Override + public int read() throws IOException { + int read = in.read(); + if (read > 0) { + count++; + callback.apply(count); + } + return read; + } + }); + + return context.proceed(); + } + +}