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).

1  
// InputStream you can cut off at a certain byte
2  
// (to test streaming input)
3  
sclass SimulatedPartialInputStream extends InputStreamPlusReadFully {
4  
  InputStream in;
5  
  bool streamEnded;
6  
  long position, cutOffPoint, mark;
7  
  
8  
  *(InputStream *in) {}
9  
  
10  
  public int read() throws IOException {
11  
    if (streamEnded || streamPaused()) ret -1;
12  
    int b = in.read();
13  
    if (b >= 0) ++position;
14  
    else set streamEnded;
15  
    ret b;
16  
  }
17  
  
18  
  public void setCutOffPoint(long point) {
19  
    cutOffPoint = point;
20  
  }
21  
  
22  
  public bool streamPaused() { ret position >= cutOffPoint; }
23  
  public bool streamEnded() { ret streamEnded; }
24  
  
25  
  public void incCutOffPoint(long bytes) {
26  
    cutOffPoint += bytes;
27  
  }
28  
  
29  
  @Override public bool markSupported() { true; }
30  
  
31  
  @Override public void mark(int readLimit) {
32  
    mark = position;
33  
    in.mark(readLimit);
34  
  }
35  
  
36  
  @Override public void reset() throws IOException {
37  
    if (mark < 0) fail();
38  
    position = mark;
39  
    mark = -1;
40  
    in.reset();
41  
  }
42  
  
43  
  toString { ret renderVars SimulatedPartialInputStream(+streamEnded, +position, +cutOffPoint, +mark); }
44  
}

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: 112 / 225
Version history: 12 change(s)
Referenced in: [show references]