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

// We effectively turn the stream into a non-blocking stream.
// There is the addition isEOF() to check whether an actual
// stream end has been reached (true) or whether we are just
// waiting for more data (false).

// To make from ByteArraysPartialInputStream:
//   Drop @Override
//   short => byte
//   ubyteToInt => id
//   noElement => Int.MIN_VALUE

sclass ArraysShortStream extends ShortInputStream {
  sclass State {
    long absolutePosition;
    int ofs;
    MinimalChain<short[]> chain;
    
    *(long *absolutePosition, int *ofs, MinimalChain<short[]> *chain) {}
    
    short[] data() { ret chain!; }
    int nBytes() { ret l(data()); }
    bool exhausted() { ret ofs >= nBytes(); }
    
    toString {
      ret withIdentity(this, "State" + bracketed(
        renderVars(+ofs, +nBytes(), next := chain.next))); }
  }
  
  bool debug, eof;
  MinimalChain<short[]> last = new MinimalChain(null);
  State state = new(0, 0, last);
  long remaining;
  State mark;

  public synchronized void readFully(short[] buf, int off, int n) throws IOException {
    if (debug) printFunctionCall readFully(buf, off, n);
    if (n == 0) ret;
    if (n > available())
      throw new EOFException();
    assertEquals(n, read(buf, off, n));
  }
  
  public synchronized int read(short[] buf, int off, int len) throws IOException {
    if (debug) printFunctionCall read(buf, off, len);
    len = min(len, available());
    if (len == 0) ret -1;
    int n = 0;
    
    while (state.chain != last) {
      int nChunk = state.nBytes();
      int inChunk = min(len, nChunk-state.ofs);
      int copiedFrom = state.ofs, nBytes = state.nBytes();
      arraycopy(state.data(), copiedFrom, buf, off, inChunk);
      n += inChunk;
      off += inChunk;
      len -= inChunk;
      state.ofs += inChunk;
      state.absolutePosition += inChunk;
      remaining -= inChunk;
      if (debug) printVars(nBytes := state.nBytes(), +copiedFrom, +nChunk, +inChunk, +n, +off, +len);
      if (state.exhausted())
        state = new State(state.absolutePosition, 0, state.chain.next);
      else
        break;
    }

    ret n;
  }
  
  public synchronized int available() { ret clampToInt(remaining); }
  
  public synchronized int read() throws IOException {
    if (debug) printFunctionCall read();
    while ping (true) {
      if (!state.exhausted()) {
        ++state.absolutePosition;
        --remaining;
        ret state.chain![state.ofs++];
      }
    
      if (state.chain.next == null)
        ret noElement();
      
      state = new State(0, 0, state.chain.next);
    }
  }
  
  static int noElement() { ret Int.MIN_VALUE; }
  
  public synchronized void add aka write(short[] buffer) {
    if (empty(buffer)) ret;
    remaining += l(buffer);
    last.setNext(new MinimalChain(null));
    last.setElement(buffer);
    last = last.next;
  }
  
  public synchronized void addEOF() {
    eof = true;
  }
  
  public synchronized bool isEOF() {
    ret state.chain == last && eof;
  }
  
  public synchronized bool markSupported() { true; }
  
  public synchronized void mark(int readLimit) {
    if (debug) printFunctionCall mark(readLimit);
    // clone state
    mark = new State(state.absolutePosition, state.ofs, state.chain);
    ifdef ByteArraysPartialInputStream_debug
      printVars(+mark);
    endifdef
 }
  
  public synchronized void reset() throws IOException {
    if (debug) printFunctionCall reset();
    if (mark == null) fail("Reset without mark");
    remaining += state.absolutePosition-mark.absolutePosition;
    state = mark;
    mark = null;
  }
  
  synchronized S stats() { ret toStringWithFields(this); }
  
  synchronized long remaining() { ret remaining; }
  synchronized long absolutePosition() { ret state.absolutePosition; }
  synchronized long endOfQueue() { ret absolutePosition()+remaining(); }
}

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: 114 / 239
Version history: 20 change(s)
Referenced in: [show references]