-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from Kong/progres
Add download progress monitor
- Loading branch information
Showing
11 changed files
with
194 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
unirest/src/main/java/kong/unirest/MonitoringInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013. | ||
* | ||
* 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 kong.unirest; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
|
||
class MonitoringInputStream extends InputStream { | ||
private final InputStream content; | ||
private final ProgressMonitor downloadMonitor; | ||
private long totalSize; | ||
private long byteCount = 0; | ||
private String fileName; | ||
|
||
MonitoringInputStream(InputStream content, ProgressMonitor downloadMonitor, Path target, RawResponse contentSize) { | ||
this.content = content; | ||
this.downloadMonitor = downloadMonitor; | ||
this.fileName = target.getFileName().toString(); | ||
this.totalSize = getBodySize(contentSize); | ||
} | ||
|
||
private Long getBodySize(RawResponse r) { | ||
String header = r.getHeaders().getFirst("Content-Length"); | ||
if (header != null && header.length() > 0) { | ||
return Long.valueOf(header); | ||
} | ||
return 0L; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return content.read(); | ||
} | ||
|
||
@Override | ||
public int read(byte[] b) throws IOException { | ||
int read = super.read(b); | ||
monitor(read); | ||
return read; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
int read = super.read(b, off, len); | ||
monitor(read); | ||
return read; | ||
} | ||
|
||
private void monitor(int bytesRead) { | ||
byteCount = byteCount + bytesRead; | ||
downloadMonitor.accept("body", fileName, byteCount, totalSize); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
content.close(); | ||
} | ||
|
||
@Override | ||
public long skip(long n) throws IOException { | ||
return content.skip(n); | ||
} | ||
|
||
@Override | ||
public int available() throws IOException { | ||
return content.available(); | ||
} | ||
|
||
@Override | ||
public synchronized void mark(int readlimit) { | ||
content.mark(readlimit); | ||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
return content.markSupported(); | ||
} | ||
|
||
@Override | ||
public synchronized void reset() throws IOException { | ||
content.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters