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

51
LINES

< > BotCompany Repo | #1014154 // ByteCountingLineReader - uses UTF8

JavaX fragment (include)

final sclass ByteCountingLineReader implements Closeable { 
  InputStream in;
  long _byteCount;
  int bufferedByte = -1;
  bool ended;
  
  *(InputStream *in) {} // in should be buffered!
  
  ByteCountingLineReader(File f) {
    in = bufferedInputStream(f);
  }
  
  S readLine() ctex {
    new ByteArrayOutputStream baos;
    if (ended) null;
    while licensed {
      int c = read();
      if (ended && baos.size() == 0) null;
      if (ended || c == '\n') break;
      if (c == '\r') {
        c = read();
        if (c != '\n' && !ended)
          bufferedByte = c;
        break;
      }
      baos.write(c);
    }
    ret fromUtf8(baos.toByteArray());
  }
  
  int read() ctex {
    if (bufferedByte >= 0) {
      int b = bufferedByte;
      bufferedByte = -1;
      ret b;
    }
    int c = in.read();
    if (c < 0) ended = true; else ++_byteCount;
    ret c;
  }
  
  long byteCount() {
    ret bufferedByte >= 0 ? _byteCount-1 : _byteCount;
  }
  
  public void close() ctex {
    if (in != null) try {
      in.close();
    } finally { in = null; }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014154
Snippet name: ByteCountingLineReader - uses UTF8
Eternal ID of this version: #1014154/21
Text MD5: fb2ea985ed8c997794bf5caef477dcf0
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-04-15 14:10:26
Source code size: 1118 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 391 / 959
Version history: 20 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)