
org.brutusin.rpc.client.http.ProgressHttpEntityWrapper Maven / Gradle / Ivy
/*
* Copyright 2016 Ignacio del Valle Alles [email protected].
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.brutusin.rpc.client.http;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.entity.HttpEntityWrapper;
import org.brutusin.rpc.client.ProgressCallback;
/**
* Copied from https://github.com/x2on/gradle-hockeyapp-plugin/blob/master/src/main/groovy/de/felixschulze/gradle/util/ProgressHttpEntityWrapper.groovy
* @author Ignacio del Valle Alles [email protected]
*/
public class ProgressHttpEntityWrapper extends HttpEntityWrapper {
private final ProgressCallback progressCallback;
public ProgressHttpEntityWrapper(final HttpEntity entity, final ProgressCallback progressCallback) {
super(entity);
this.progressCallback = progressCallback;
}
@Override
public void writeTo(final OutputStream out) throws IOException {
this.wrappedEntity.writeTo(out instanceof ProgressFilterOutputStream ? out : new ProgressFilterOutputStream(out, this.progressCallback, getContentLength()));
}
static class ProgressFilterOutputStream extends FilterOutputStream {
private final ProgressCallback progressCallback;
private long transferred;
private long totalBytes;
ProgressFilterOutputStream(final OutputStream out, final ProgressCallback progressCallback, final long totalBytes) {
super(out);
this.progressCallback = progressCallback;
this.transferred = 0;
this.totalBytes = totalBytes;
}
@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
//super.write(byte b[], int off, int len) calls write(int b)
out.write(b, off, len);
this.transferred += len;
this.progressCallback.progress(getCurrentProgress());
}
@Override
public void write(final int b) throws IOException {
out.write(b);
this.transferred++;
this.progressCallback.progress(getCurrentProgress());
}
private float getCurrentProgress() {
return ((float) this.transferred / this.totalBytes) * 100;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy