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

73
LINES

< > BotCompany Repo | #1014159 // ByteCountingLineReader SSCCE for StackOverflow

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (109L/1K).

1  
!7
2  
3  
set flag LeanMode.
4  
5  
p {
6  
  ByteCountingLineReader r = new(new ByteArrayInputStream(toUtf8("Hello\r\nWorld\n")));
7  
  
8  
  S line = null;
9  
  do {
10  
    long count = r.byteCount();
11  
    line = r.readLine();
12  
    System.out.println("Line at byte " + count + ": " + line);
13  
  } while (line != null);
14  
  
15  
  r.close();
16  
}
17  
18  
static class ByteCountingLineReader implements Closeable { 
19  
  InputStream in;
20  
  long _byteCount;
21  
  int bufferedByte = -1;
22  
  boolean ended;
23  
  
24  
  // in should be a buffered stream!
25  
  ByteCountingLineReader(InputStream in) {
26  
    this.in = in;
27  
  }
28  
  
29  
  ByteCountingLineReader(File f) throws IOException {
30  
    in = new BufferedInputStream(new FileInputStream(f), 65536);
31  
  }
32  
  
33  
  String readLine() throws IOException {
34  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
35  
    if (ended) null;
36  
    while true {
37  
      int c = read();
38  
      if (ended && baos.size() == 0) null;
39  
      if (ended || c == '\n') break;
40  
      if (c == '\r') {
41  
        c = read();
42  
        if (c != '\n' && !ended)
43  
          bufferedByte = c;
44  
        break;
45  
      }
46  
      baos.write(c);
47  
    }
48  
    return fromUtf8(baos.toByteArray());
49  
  }
50  
  
51  
  int read() throws IOException {
52  
    if (bufferedByte >= 0) {
53  
      int b = bufferedByte;
54  
      bufferedByte = -1;
55  
      return b;
56  
    }
57  
    int c = in.read();
58  
    if (c < 0) ended = true; else ++_byteCount;
59  
    return c;
60  
  }
61  
  
62  
  long byteCount() {
63  
    return bufferedByte >= 0 ? _byteCount-1 : _byteCount;
64  
  }
65  
  
66  
  public void close() throws IOException {
67  
    if (in != null) try {
68  
      in.close();
69  
    } finally { in = null; }
70  
  }
71  
  
72  
  bool ended() { ret ended; }
73  
}

Author comment

Posted at https://stackoverflow.com/questions/15092869/how-to-know-bytes-readoffset-of-bufferedreader?rq=1

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: #1014159
Snippet name: ByteCountingLineReader SSCCE for StackOverflow
Eternal ID of this version: #1014159/12
Text MD5: 8cbb17011896b4764e057a01a6b1387d
Transpilation MD5: 02b08f4a3dc953d0372068ac3485320a
Author: stefan
Category: javax / io
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-04-15 14:13:03
Source code size: 1668 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 377 / 918
Version history: 11 change(s)
Referenced in: [show references]