Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

60
LINES

< > BotCompany Repo | #1021216 // BoundedInputStream [limit number of bytes, from apache commons]

JavaX fragment (include)

1  
sclass BoundedInputStream extends InputStream {
2  
  InputStream in;
3  
  long max, pos;
4  
  long mark = -1;
5  
6  
  *(InputStream *in, long *max) {}
7  
  *(long *max, InputStream *in) {}
8  
9  
  public int read() throws IOException {
10  
    if (max >= 0 && pos >= max) ret -1;
11  
    int result = in.read();
12  
    pos++;
13  
    ret result;
14  
  }
15  
16  
  public int read(final byte[] b) throws IOException {
17  
    ret read(b, 0, b.length);
18  
  }
19  
20  
  public int read(final byte[] b, final int off, final int len) throws IOException {
21  
    if (max>=0 && pos>=max) ret -1;
22  
    long maxRead = max>=0 ? Math.min(len, max-pos) : len;
23  
    int bytesRead = in.read(b, off, (int)maxRead);
24  
25  
    if (bytesRead < 0) ret -1;
26  
27  
    pos+=bytesRead;
28  
    return bytesRead;
29  
  }
30  
31  
  public long skip(final long n) throws IOException {
32  
    long toSkip = max>=0 ? Math.min(n, max-pos) : n;
33  
    long skippedBytes = in.skip(toSkip);
34  
    pos+=skippedBytes;
35  
    return skippedBytes;
36  
  }
37  
38  
  public int available() throws IOException {
39  
    if (max>=0 && pos>=max) ret 0;
40  
    ret in.available();
41  
  }
42  
43  
  public void close() throws IOException {
44  
    in.close();
45  
  }
46  
47  
  public synchronized void reset() throws IOException {
48  
    in.reset();
49  
    pos = mark;
50  
  }
51  
52  
  public synchronized void mark(final int readlimit) {
53  
    in.mark(readlimit);
54  
    mark = pos;
55  
  }
56  
57  
  public bool markSupported() {
58  
    ret in.markSupported();
59  
  }
60  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021216
Snippet name: BoundedInputStream [limit number of bytes, from apache commons]
Eternal ID of this version: #1021216/3
Text MD5: d88e0a3a3629aebb413b582b5e5661dc
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-03 15:10:59
Source code size: 1406 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 195 / 684
Version history: 2 change(s)
Referenced in: [show references]