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)

1  
final sclass ByteCountingLineReader implements Closeable { 
2  
  InputStream in;
3  
  long _byteCount;
4  
  int bufferedByte = -1;
5  
  bool ended;
6  
  
7  
  *(InputStream *in) {} // in should be buffered!
8  
  
9  
  ByteCountingLineReader(File f) {
10  
    in = bufferedInputStream(f);
11  
  }
12  
  
13  
  S readLine() ctex {
14  
    new ByteArrayOutputStream baos;
15  
    if (ended) null;
16  
    while licensed {
17  
      int c = read();
18  
      if (ended && baos.size() == 0) null;
19  
      if (ended || c == '\n') break;
20  
      if (c == '\r') {
21  
        c = read();
22  
        if (c != '\n' && !ended)
23  
          bufferedByte = c;
24  
        break;
25  
      }
26  
      baos.write(c);
27  
    }
28  
    ret fromUtf8(baos.toByteArray());
29  
  }
30  
  
31  
  int read() ctex {
32  
    if (bufferedByte >= 0) {
33  
      int b = bufferedByte;
34  
      bufferedByte = -1;
35  
      ret b;
36  
    }
37  
    int c = in.read();
38  
    if (c < 0) ended = true; else ++_byteCount;
39  
    ret c;
40  
  }
41  
  
42  
  long byteCount() {
43  
    ret bufferedByte >= 0 ? _byteCount-1 : _byteCount;
44  
  }
45  
  
46  
  public void close() ctex {
47  
    if (in != null) try {
48  
      in.close();
49  
    } finally { in = null; }
50  
  }
51  
}

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