diff options
author | Joe Robinson <joe@lc8n.com> | 2013-02-04 20:51:05 +0000 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2013-02-04 20:51:05 +0000 |
commit | eb7c5e7d7b332fb488658bb84066b0b3c516896e (patch) | |
tree | d89a251adb13bd9a45d1df945d60b91169ad10d5 /src/com/lc8n/util/ProgressMultipartEntity.java | |
parent | da2df98f789f2531142835dc43566ccda9583c22 (diff) |
"Added http upload functionality
Diffstat (limited to 'src/com/lc8n/util/ProgressMultipartEntity.java')
-rw-r--r-- | src/com/lc8n/util/ProgressMultipartEntity.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/com/lc8n/util/ProgressMultipartEntity.java b/src/com/lc8n/util/ProgressMultipartEntity.java new file mode 100644 index 0000000..fefe053 --- /dev/null +++ b/src/com/lc8n/util/ProgressMultipartEntity.java @@ -0,0 +1,71 @@ +package com.lc8n.util; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntity; + +public class ProgressMultipartEntity extends MultipartEntity +{ + + private final ProgressListener listener; + + public ProgressMultipartEntity(final ProgressListener listener) + { + super(); + this.listener = listener; + } + + public ProgressMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener) + { + super(mode); + this.listener = listener; + } + + public ProgressMultipartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) + { + super(mode, boundary, charset); + this.listener = listener; + } + + @Override + public void writeTo(final OutputStream outstream) throws IOException + { + super.writeTo(new CountingOutputStream(outstream, this.listener)); + } + + public static interface ProgressListener + { + void transferred(long num); + } + + public static class CountingOutputStream extends FilterOutputStream + { + + private final ProgressListener listener; + private long transferred; + + public CountingOutputStream(final OutputStream out, final ProgressListener listener) + { + super(out); + this.listener = listener; + this.transferred = 0; + } + + public void write(byte[] b, int off, int len) throws IOException + { + out.write(b, off, len); + this.transferred += len; + this.listener.transferred(this.transferred); + } + + public void write(int b) throws IOException + { + out.write(b); + this.transferred++; + this.listener.transferred(this.transferred); + } + } +}
\ No newline at end of file |