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)

sclass BoundedInputStream extends InputStream {
  InputStream in;
  long max, pos;
  long mark = -1;

  *(InputStream *in, long *max) {}
  *(long *max, InputStream *in) {}

  public int read() throws IOException {
    if (max >= 0 && pos >= max) ret -1;
    int result = in.read();
    pos++;
    ret result;
  }

  public int read(final byte[] b) throws IOException {
    ret read(b, 0, b.length);
  }

  public int read(final byte[] b, final int off, final int len) throws IOException {
    if (max>=0 && pos>=max) ret -1;
    long maxRead = max>=0 ? Math.min(len, max-pos) : len;
    int bytesRead = in.read(b, off, (int)maxRead);

    if (bytesRead < 0) ret -1;

    pos+=bytesRead;
    return bytesRead;
  }

  public long skip(final long n) throws IOException {
    long toSkip = max>=0 ? Math.min(n, max-pos) : n;
    long skippedBytes = in.skip(toSkip);
    pos+=skippedBytes;
    return skippedBytes;
  }

  public int available() throws IOException {
    if (max>=0 && pos>=max) ret 0;
    ret in.available();
  }

  public void close() throws IOException {
    in.close();
  }

  public synchronized void reset() throws IOException {
    in.reset();
    pos = mark;
  }

  public synchronized void mark(final int readlimit) {
    in.mark(readlimit);
    mark = pos;
  }

  public bool markSupported() {
    ret in.markSupported();
  }
}

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: 192 / 680
Version history: 2 change(s)
Referenced in: [show references]