Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I introduced a derived <strong><code>FileEntity</code></strong> that just counts the written bytes. It uses <strong><code>OutputStreamProgress</code></strong> that does the actual counting (kind of a <strong>decorator</strong> to the actual <code>OutputStream</code>).</p> <p>The <strong>advantage</strong> of this (and decoration in general) is that I do <strong>not</strong> need to copy the actual implementation, like the the actual copying from the file stream to the output stream. I can also change to use a different (newer) implementation, like the <code>NFileEntity</code>.</p> <p>Enjoy...</p> <h2>FileEntity.java</h2> <pre><code>public class FileEntity extends org.apache.http.entity.FileEntity { private OutputStreamProgress outstream; public FileEntity(File file, String contentType) { super(file, contentType); } @Override public void writeTo(OutputStream outstream) throws IOException { this.outstream = new OutputStreamProgress(outstream); super.writeTo(this.outstream); } /** * Progress: 0-100 */ public int getProgress() { if (outstream == null) { return 0; } long contentLength = getContentLength(); if (contentLength &lt;= 0) { // Prevent division by zero and negative values return 0; } long writtenLength = outstream.getWrittenLength(); return (int) (100*writtenLength/contentLength); } } </code></pre> <h2>OutputStreamProgress.java</h2> <pre><code>public class OutputStreamProgress extends OutputStream { private final OutputStream outstream; private volatile long bytesWritten=0; public OutputStreamProgress(OutputStream outstream) { this.outstream = outstream; } @Override public void write(int b) throws IOException { outstream.write(b); bytesWritten++; } @Override public void write(byte[] b) throws IOException { outstream.write(b); bytesWritten += b.length; } @Override public void write(byte[] b, int off, int len) throws IOException { outstream.write(b, off, len); bytesWritten += len; } @Override public void flush() throws IOException { outstream.flush(); } @Override public void close() throws IOException { outstream.close(); } public long getWrittenLength() { return bytesWritten; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload