|
1 | | -package net.siisise.io; |
2 | | - |
3 | | -import java.io.IOException; |
4 | | -import java.io.InputStream; |
5 | | -import java.io.Reader; |
6 | | -import java.nio.charset.StandardCharsets; |
7 | | -import net.siisise.lang.CodePoint; |
8 | | - |
9 | | -/** |
10 | | - * InputStreamReaderの逆 |
11 | | - * とりあえずUTF-8出力前提 |
12 | | - * 1000文字程度読んでおく |
13 | | - */ |
14 | | -public class ReaderInputStream extends InputStream { |
15 | | - |
16 | | - final PacketA pac = new PacketA(); |
17 | | - char[] pair; |
18 | | - final Reader rd; |
19 | | - boolean eof = false; |
20 | | - |
21 | | - ReaderInputStream(Reader r) { |
22 | | - rd = r; |
23 | | - } |
24 | | - |
25 | | - private void buffering() throws IOException { |
26 | | - if ( eof ) return; |
27 | | - int ch = rd.read(); |
28 | | - if ( ch < 0 ) { |
29 | | - eof = true; |
30 | | - return; |
31 | | - } |
32 | | - if ( ch >= 0xd800 && ch <= 0xdbff ) { |
33 | | - if ( pair != null ) { |
34 | | - ch = pair[0]; |
35 | | - byte[] bytes = CodePoint.utf8(ch); |
36 | | - pac.write(bytes); |
37 | | - } |
38 | | - pair = new char[] {(char)ch,0}; |
39 | | - buffering(); |
40 | | - return; |
41 | | - } else if ( ch >= 0xdc00 && ch <= 0xdfff ) { |
42 | | - byte[] bytes; |
43 | | - if ( pair != null ) { |
44 | | - pair[1] = (char)ch; |
45 | | - bytes = String.valueOf(pair).getBytes(StandardCharsets.UTF_8); |
46 | | - } else { |
47 | | - bytes = CodePoint.utf8(ch); |
48 | | - } |
49 | | - pac.write(bytes); |
50 | | - } else { |
51 | | - pair = null; |
52 | | - byte[] bytes = CodePoint.utf8(ch); |
53 | | - pac.write(bytes); |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - @Override |
58 | | - public int read() throws IOException { |
59 | | - while ( !eof && pac.size() < 1024 ) { |
60 | | - buffering(); |
61 | | - } |
62 | | - return pac.read(); |
63 | | - } |
64 | | - |
65 | | - @Override |
66 | | - public void close() throws IOException { |
67 | | - rd.close(); |
68 | | - } |
69 | | - |
70 | | - /** |
71 | | - * 正確な長さがわからない |
72 | | - * @return |
73 | | - * @throws IOException |
74 | | - */ |
75 | | - @Override |
76 | | - public int available() throws IOException { |
77 | | - return pac.size() + (rd.ready() ? 1 : 0); |
78 | | - } |
79 | | - |
80 | | -} |
| 1 | +package net.siisise.io; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.Reader; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import net.siisise.lang.CodePoint; |
| 8 | + |
| 9 | +/** |
| 10 | + * バイト列が必要な処理系にReader系を繋ぐ |
| 11 | + * InputStreamReaderの逆 |
| 12 | + * とりあえずUTF-8出力前提 |
| 13 | + * 100文字程度読んでおく |
| 14 | + */ |
| 15 | +public class ReaderInputStream extends InputStream { |
| 16 | + |
| 17 | + final PacketA pac = new PacketA(); |
| 18 | + char[] pair; |
| 19 | + final Reader rd; |
| 20 | + final int bufferSize; |
| 21 | + boolean eof = false; |
| 22 | + |
| 23 | + ReaderInputStream(Reader r) { |
| 24 | + rd = r; |
| 25 | + bufferSize = 100; |
| 26 | + } |
| 27 | + |
| 28 | + ReaderInputStream(Reader r, int size) { |
| 29 | + rd = r; |
| 30 | + bufferSize = size; |
| 31 | + } |
| 32 | + |
| 33 | + private void buffering() throws IOException { |
| 34 | + if ( eof ) return; |
| 35 | + int ch = rd.read(); |
| 36 | + if ( ch < 0 ) { |
| 37 | + eof = true; |
| 38 | + return; |
| 39 | + } |
| 40 | + if ( ch >= 0xd800 && ch <= 0xdbff ) { |
| 41 | + if ( pair != null ) { |
| 42 | + ch = pair[0]; |
| 43 | + byte[] bytes = CodePoint.utf8(ch); |
| 44 | + pac.write(bytes); |
| 45 | + } |
| 46 | + pair = new char[] {(char)ch,0}; |
| 47 | + buffering(); |
| 48 | + return; |
| 49 | + } else if ( ch >= 0xdc00 && ch <= 0xdfff ) { |
| 50 | + byte[] bytes; |
| 51 | + if ( pair != null ) { |
| 52 | + pair[1] = (char)ch; |
| 53 | + bytes = String.valueOf(pair).getBytes(StandardCharsets.UTF_8); |
| 54 | + } else { |
| 55 | + bytes = CodePoint.utf8(ch); |
| 56 | + } |
| 57 | + pac.write(bytes); |
| 58 | + } else { |
| 59 | + pair = null; |
| 60 | + byte[] bytes = CodePoint.utf8(ch); |
| 61 | + pac.write(bytes); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int read() throws IOException { |
| 67 | + while ( !eof && pac.size() < bufferSize ) { |
| 68 | + buffering(); |
| 69 | + } |
| 70 | + return pac.read(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void close() throws IOException { |
| 75 | + rd.close(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 正確な長さがわからない |
| 80 | + * @return |
| 81 | + * @throws IOException |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public int available() throws IOException { |
| 85 | + return pac.size() + (rd.ready() ? 1 : 0); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments