/src/main/java/com/soundcloud/api/CountingMultipartEntity.java
Java | 81 lines | 64 code | 17 blank | 0 comment | 4 complexity | 9073e86b9ff4c38d93813054fb996c92 MD5 | raw file
Possible License(s): MIT
1package com.soundcloud.api; 2 3import java.io.FilterOutputStream; 4import java.io.IOException; 5import java.io.InputStream; 6import java.io.OutputStream; 7 8import org.apache.http.Header; 9import org.apache.http.HttpEntity; 10 11class CountingMultipartEntity implements HttpEntity { 12 private HttpEntity mDelegate; 13 private Request.TransferProgressListener mListener; 14 15 public CountingMultipartEntity(HttpEntity delegate, 16 Request.TransferProgressListener listener) { 17 super(); 18 mDelegate = delegate; 19 mListener = listener; 20 } 21 22 public void consumeContent() throws IOException { 23 mDelegate.consumeContent(); 24 } 25 26 public InputStream getContent() throws IOException, IllegalStateException { 27 return mDelegate.getContent(); 28 } 29 30 public Header getContentEncoding() { 31 return mDelegate.getContentEncoding(); 32 } 33 34 public long getContentLength() { 35 return mDelegate.getContentLength(); 36 } 37 38 public Header getContentType() { 39 return mDelegate.getContentType(); 40 } 41 42 public boolean isChunked() { 43 return mDelegate.isChunked(); 44 } 45 46 public boolean isRepeatable() { 47 return mDelegate.isRepeatable(); 48 } 49 50 public boolean isStreaming() { 51 return mDelegate.isStreaming(); 52 } 53 54 public void writeTo(OutputStream outstream) throws IOException { 55 mDelegate.writeTo(new CountingOutputStream(outstream, mListener)); 56 } 57 58 private static class CountingOutputStream extends FilterOutputStream { 59 private final Request.TransferProgressListener mListener; 60 private long mTransferred = 0; 61 62 public CountingOutputStream(final OutputStream out, final Request.TransferProgressListener listener) { 63 super(out); 64 mListener = listener; 65 } 66 67 @Override 68 public void write(byte[] b, int off, int len) throws IOException { 69 out.write(b, off, len); 70 mTransferred += len; 71 if (mListener != null) mListener.transferred(mTransferred); 72 } 73 74 @Override 75 public void write(int b) throws IOException { 76 out.write(b); 77 mTransferred++; 78 if (mListener != null) mListener.transferred(mTransferred); 79 } 80 } 81}