Libraryless. Click here for Pure Java version (12862L/73K).
1 | sclass StringHead is Closeable { |
2 | settable bool readMode; |
3 | settable bool writeMode; |
4 | gettable BufferedReader reader; |
5 | gettable Writer writer; |
6 | |
7 | *() {} |
8 | *(Reader reader) { reader(reader); } |
9 | *(Writer writer) { writer(writer); } |
10 | |
11 | selfType reader(Reader reader) { |
12 | this.reader = bufferedReader(reader); |
13 | readMode(true); |
14 | this; |
15 | } |
16 | |
17 | selfType writer(Writer writer) { this.writer = writer; writeMode(true); this; } |
18 | |
19 | void finish {} |
20 | |
21 | S readLine() { |
22 | S line = readLineFromReader(reader); |
23 | if (line == null) close(); |
24 | ret line; |
25 | } |
26 | |
27 | void writeLine(S line) ctex { |
28 | writer.write(line); |
29 | writer.write('\n'); |
30 | } |
31 | |
32 | void exchangeLine(IF0<S> getter, IVF1<S> setter) { |
33 | if (writeMode()) |
34 | writeLine(getter!); |
35 | |
36 | if (readMode()) |
37 | setter.get(readLine()); |
38 | } |
39 | |
40 | close { |
41 | dispose reader; |
42 | dispose writer; |
43 | } |
44 | |
45 | replace ByteIO with StringIO. |
46 | |
47 | void exchange(ByteIO writable) { |
48 | if (writable != null) writable.readWrite(this); |
49 | } |
50 | |
51 | void exchangeAll(Iterable<? extends ByteIO> writables) { |
52 | if (writables != null) |
53 | for (writable : writables) |
54 | exchange(writable); |
55 | } |
56 | } |
Began life as a copy of #1035632
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035757 |
Snippet name: | StringHead - string I/O class (can read and write, dev.) |
Eternal ID of this version: | #1035757/10 |
Text MD5: | aad93ae69b47ac08c224c984ef5ea29e |
Transpilation MD5: | bff6141b86abcfd7d4b92de0f0070c44 |
Author: | stefan |
Category: | javax / io |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-07-22 04:21:55 |
Source code size: | 1210 bytes / 56 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 146 / 245 |
Version history: | 9 change(s) |
Referenced in: | [show references] |