de.felixschulze.gradle.util.ProgressHttpEntityWrapper.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-hockeyapp-plugin Show documentation
Show all versions of gradle-hockeyapp-plugin Show documentation
A Gradle plugin for uploading iOS and Android Apps to HockeyApp.
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Felix Schulze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.felixschulze.gradle.util;
import org.apache.http.HttpEntity;
import org.apache.http.entity.HttpEntityWrapper;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* HttpEntityWrapper with a progress callback
*
* @see http://stackoverflow.com/a/7319110/268795
*/
class ProgressHttpEntityWrapper extends HttpEntityWrapper {
private final ProgressCallback progressCallback
public static interface ProgressCallback {
public void progress(float progress)
}
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