|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright for portions of unirest-java are held by Kong Inc (c) 2013. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | + * a copy of this software and associated documentation files (the |
| 8 | + * "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to |
| 12 | + * the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +package kong.unirest; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.nio.file.Path; |
| 31 | + |
| 32 | +class MonitoringInputStream extends InputStream { |
| 33 | + private final InputStream content; |
| 34 | + private final ProgressMonitor downloadMonitor; |
| 35 | + private long totalSize; |
| 36 | + private long byteCount = 0; |
| 37 | + private String fileName; |
| 38 | + |
| 39 | + MonitoringInputStream(InputStream content, ProgressMonitor downloadMonitor, Path target, RawResponse contentSize) { |
| 40 | + this.content = content; |
| 41 | + this.downloadMonitor = downloadMonitor; |
| 42 | + this.fileName = target.getFileName().toString(); |
| 43 | + this.totalSize = getBodySize(contentSize); |
| 44 | + } |
| 45 | + |
| 46 | + private Long getBodySize(RawResponse r) { |
| 47 | + String header = r.getHeaders().getFirst("Content-Length"); |
| 48 | + if (header != null && header.length() > 0) { |
| 49 | + return Long.valueOf(header); |
| 50 | + } |
| 51 | + return 0L; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int read() throws IOException { |
| 56 | + return content.read(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int read(byte[] b) throws IOException { |
| 61 | + int read = super.read(b); |
| 62 | + monitor(read); |
| 63 | + return read; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int read(byte[] b, int off, int len) throws IOException { |
| 68 | + int read = super.read(b, off, len); |
| 69 | + monitor(read); |
| 70 | + return read; |
| 71 | + } |
| 72 | + |
| 73 | + private void monitor(int bytesRead) { |
| 74 | + byteCount = byteCount + bytesRead; |
| 75 | + downloadMonitor.accept("body", fileName, byteCount, totalSize); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void close() throws IOException { |
| 80 | + content.close(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public long skip(long n) throws IOException { |
| 85 | + return content.skip(n); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int available() throws IOException { |
| 90 | + return content.available(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public synchronized void mark(int readlimit) { |
| 95 | + content.mark(readlimit); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean markSupported() { |
| 100 | + return content.markSupported(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public synchronized void reset() throws IOException { |
| 105 | + content.reset(); |
| 106 | + } |
| 107 | +} |
0 commit comments