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

44
LINES

< > BotCompany Repo | #1032907 // SimulatedPartialInputStream

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3975L/23K).

// InputStream you can cut off at a certain byte
// (to test streaming input)
sclass SimulatedPartialInputStream extends InputStreamPlusReadFully {
  InputStream in;
  bool streamEnded;
  long position, cutOffPoint, mark;
  
  *(InputStream *in) {}
  
  public int read() throws IOException {
    if (streamEnded || streamPaused()) ret -1;
    int b = in.read();
    if (b >= 0) ++position;
    else set streamEnded;
    ret b;
  }
  
  public void setCutOffPoint(long point) {
    cutOffPoint = point;
  }
  
  public bool streamPaused() { ret position >= cutOffPoint; }
  public bool streamEnded() { ret streamEnded; }
  
  public void incCutOffPoint(long bytes) {
    cutOffPoint += bytes;
  }
  
  @Override public bool markSupported() { true; }
  
  @Override public void mark(int readLimit) {
    mark = position;
    in.mark(readLimit);
  }
  
  @Override public void reset() throws IOException {
    if (mark < 0) fail();
    position = mark;
    mark = -1;
    in.reset();
  }
  
  toString { ret renderVars SimulatedPartialInputStream(+streamEnded, +position, +cutOffPoint, +mark); }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032907
Snippet name: SimulatedPartialInputStream
Eternal ID of this version: #1032907/13
Text MD5: 93102703c9074bd21a9be488445dc837
Transpilation MD5: 508768d11105bcabb09421bbcf1284fa
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-10 21:33:22
Source code size: 1138 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 186 / 315
Version history: 12 change(s)
Referenced in: #1032911 - ByteArraysPartialInputStream
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)