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

130
LINES

< > BotCompany Repo | #1032918 // ArraysShortStream - made from ByteArraysPartialInputStream

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

Libraryless. Click here for Pure Java version (4986L/28K).

1  
// We effectively turn the stream into a non-blocking stream.
2  
// There is the addition isEOF() to check whether an actual
3  
// stream end has been reached (true) or whether we are just
4  
// waiting for more data (false).
5  
6  
// To make from ByteArraysPartialInputStream:
7  
//   Drop @Override
8  
//   short => byte
9  
//   ubyteToInt => id
10  
//   noElement => Int.MIN_VALUE
11  
12  
sclass ArraysShortStream extends ShortInputStream {
13  
  sclass State {
14  
    long absolutePosition;
15  
    int ofs;
16  
    MinimalChain<short[]> chain;
17  
    
18  
    *(long *absolutePosition, int *ofs, MinimalChain<short[]> *chain) {}
19  
    
20  
    short[] data() { ret chain!; }
21  
    int nBytes() { ret l(data()); }
22  
    bool exhausted() { ret ofs >= nBytes(); }
23  
    
24  
    toString {
25  
      ret withIdentity(this, "State" + bracketed(
26  
        renderVars(+ofs, +nBytes(), next := chain.next))); }
27  
  }
28  
  
29  
  bool debug, eof;
30  
  MinimalChain<short[]> last = new MinimalChain(null);
31  
  State state = new(0, 0, last);
32  
  long remaining;
33  
  State mark;
34  
35  
  public synchronized void readFully(short[] buf, int off, int n) throws IOException {
36  
    if (debug) printFunctionCall readFully(buf, off, n);
37  
    if (n == 0) ret;
38  
    if (n > available())
39  
      throw new EOFException();
40  
    assertEquals(n, read(buf, off, n));
41  
  }
42  
  
43  
  public synchronized int read(short[] buf, int off, int len) throws IOException {
44  
    if (debug) printFunctionCall read(buf, off, len);
45  
    len = min(len, available());
46  
    if (len == 0) ret -1;
47  
    int n = 0;
48  
    
49  
    while (state.chain != last) {
50  
      int nChunk = state.nBytes();
51  
      int inChunk = min(len, nChunk-state.ofs);
52  
      int copiedFrom = state.ofs, nBytes = state.nBytes();
53  
      arraycopy(state.data(), copiedFrom, buf, off, inChunk);
54  
      n += inChunk;
55  
      off += inChunk;
56  
      len -= inChunk;
57  
      state.ofs += inChunk;
58  
      state.absolutePosition += inChunk;
59  
      remaining -= inChunk;
60  
      if (debug) printVars(nBytes := state.nBytes(), +copiedFrom, +nChunk, +inChunk, +n, +off, +len);
61  
      if (state.exhausted())
62  
        state = new State(state.absolutePosition, 0, state.chain.next);
63  
      else
64  
        break;
65  
    }
66  
67  
    ret n;
68  
  }
69  
  
70  
  public synchronized int available() { ret clampToInt(remaining); }
71  
  
72  
  public synchronized int read() throws IOException {
73  
    if (debug) printFunctionCall read();
74  
    while ping (true) {
75  
      if (!state.exhausted()) {
76  
        ++state.absolutePosition;
77  
        --remaining;
78  
        ret state.chain![state.ofs++];
79  
      }
80  
    
81  
      if (state.chain.next == null)
82  
        ret noElement();
83  
      
84  
      state = new State(0, 0, state.chain.next);
85  
    }
86  
  }
87  
  
88  
  static int noElement() { ret Int.MIN_VALUE; }
89  
  
90  
  public synchronized void add aka write(short[] buffer) {
91  
    if (empty(buffer)) ret;
92  
    remaining += l(buffer);
93  
    last.setNext(new MinimalChain(null));
94  
    last.setElement(buffer);
95  
    last = last.next;
96  
  }
97  
  
98  
  public synchronized void addEOF() {
99  
    eof = true;
100  
  }
101  
  
102  
  public synchronized bool isEOF() {
103  
    ret state.chain == last && eof;
104  
  }
105  
  
106  
  public synchronized bool markSupported() { true; }
107  
  
108  
  public synchronized void mark(int readLimit) {
109  
    if (debug) printFunctionCall mark(readLimit);
110  
    // clone state
111  
    mark = new State(state.absolutePosition, state.ofs, state.chain);
112  
    ifdef ByteArraysPartialInputStream_debug
113  
      printVars(+mark);
114  
    endifdef
115  
 }
116  
  
117  
  public synchronized void reset() throws IOException {
118  
    if (debug) printFunctionCall reset();
119  
    if (mark == null) fail("Reset without mark");
120  
    remaining += state.absolutePosition-mark.absolutePosition;
121  
    state = mark;
122  
    mark = null;
123  
  }
124  
  
125  
  synchronized S stats() { ret toStringWithFields(this); }
126  
  
127  
  synchronized long remaining() { ret remaining; }
128  
  synchronized long absolutePosition() { ret state.absolutePosition; }
129  
  synchronized long endOfQueue() { ret absolutePosition()+remaining(); }
130  
}

Author comment

Began life as a copy of #1032911

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1032918
Snippet name: ArraysShortStream - made from ByteArraysPartialInputStream
Eternal ID of this version: #1032918/21
Text MD5: 1def5133a355e224ce493a7fc73a576f
Transpilation MD5: dea4676b1cdb47c16c6c75e2c4a90bf8
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-11 14:50:20
Source code size: 3955 bytes / 130 lines
Pitched / IR pitched: No / No
Views / Downloads: 173 / 329
Version history: 20 change(s)
Referenced in: [show references]