|
| 1 | +package org.duangsuse.telegramscanner.scanner; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.nio.charset.Charset; |
| 5 | + |
| 6 | +/** |
| 7 | + * Line based input stream |
| 8 | + * |
| 9 | + * @see java.io.DataInput |
| 10 | + * @see java.io.InputStream |
| 11 | + */ |
| 12 | +public class Utf8LineInputStream implements Closeable { |
| 13 | + /** |
| 14 | + * Delegated data input stream |
| 15 | + */ |
| 16 | + private DataInput target; |
| 17 | + |
| 18 | + /** |
| 19 | + * Construct using data input {@link DataInputStream} |
| 20 | + * |
| 21 | + * @param dataIn data input instance |
| 22 | + */ |
| 23 | + public Utf8LineInputStream(DataInput dataIn) { |
| 24 | + this.target = dataIn; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Construct using byte input stream |
| 29 | + * |
| 30 | + * @see InputStreamReader with utf-8 decoder support |
| 31 | + * @param in line-based input stream |
| 32 | + */ |
| 33 | + public Utf8LineInputStream(InputStream in) { |
| 34 | + final InputStreamReader utf8InputReader = new InputStreamReader(in, UTF_8); |
| 35 | + InputStream is = new InputStream() { |
| 36 | + @Override |
| 37 | + public int read() throws IOException { |
| 38 | + return utf8InputReader.read(); |
| 39 | + } /* readLine() uses this method only */ |
| 40 | + }; |
| 41 | + |
| 42 | + this.target = new DataInputStream(is); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean equals(Object o) { |
| 47 | + if (this == o) return true; |
| 48 | + if (o == null || getClass() != o.getClass()) return false; |
| 49 | + |
| 50 | + Utf8LineInputStream that = (Utf8LineInputStream) o; |
| 51 | + |
| 52 | + return target.equals(that.target); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Utf-8 charset |
| 57 | + */ |
| 58 | + @SuppressWarnings("WeakerAccess") |
| 59 | + protected static final Charset UTF_8 = Charset.forName("UTF-8"); |
| 60 | + |
| 61 | + /** |
| 62 | + * Reads the next line of text from the input stream. |
| 63 | + * It reads successive bytes, converting |
| 64 | + * each byte separately into a character, |
| 65 | + * until it encounters a line terminator or |
| 66 | + * end of |
| 67 | + * file; the characters read are then |
| 68 | + * returned as a {@code String}. Note |
| 69 | + * that because this |
| 70 | + * method processes bytes, |
| 71 | + * <b>it does not support input of the full Unicode |
| 72 | + * character set.</b> |
| 73 | + * <p> |
| 74 | + * If end of file is encountered |
| 75 | + * before even one byte can be read, then {@code null} |
| 76 | + * is returned. Otherwise, each byte that is |
| 77 | + * read is converted to type {@code char} |
| 78 | + * by zero-extension. If the character {@code '\n'} |
| 79 | + * is encountered, it is discarded and reading |
| 80 | + * ceases. If the character {@code '\r'} |
| 81 | + * is encountered, it is discarded and, if |
| 82 | + * the following byte converts  to the |
| 83 | + * character {@code '\n'}, then that is |
| 84 | + * discarded also; reading then ceases. If |
| 85 | + * end of file is encountered before either |
| 86 | + * of the characters {@code '\n'} and |
| 87 | + * {@code '\r'} is encountered, reading |
| 88 | + * ceases. Once reading has ceased, a {@code String} |
| 89 | + * is returned that contains all the characters |
| 90 | + * read and not discarded, taken in order. |
| 91 | + * Note that every character in this string |
| 92 | + * will have a value less than {@code \u005Cu0100}, |
| 93 | + * that is, {@code (char)256}. |
| 94 | + * |
| 95 | + * @return the next line of text from the input stream, |
| 96 | + * or {@code null} if the end of file is |
| 97 | + * encountered before a byte can be read. |
| 98 | + * @exception IOException if an I/O error occurs. |
| 99 | + */ |
| 100 | + public String readLine() throws IOException { |
| 101 | + return target.readLine(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void close() throws IOException { |
| 106 | + if (target instanceof Closeable) |
| 107 | + ((Closeable) target).close(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String toString() { |
| 112 | + return "Utf8LineInputStream(" + target.toString() + ")"; |
| 113 | + } |
| 114 | +} |
0 commit comments