| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.yaml.snakeyaml.reader; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
import java.io.IOException; |
| 41 | |
import java.io.InputStream; |
| 42 | |
import java.io.InputStreamReader; |
| 43 | |
import java.io.PushbackInputStream; |
| 44 | |
import java.io.Reader; |
| 45 | |
import java.nio.charset.Charset; |
| 46 | |
import java.nio.charset.CharsetDecoder; |
| 47 | |
import java.nio.charset.CodingErrorAction; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public class UnicodeReader extends Reader { |
| 54 | 1 | private static final Charset UTF8 = Charset.forName("UTF-8"); |
| 55 | 1 | private static final Charset UTF16BE = Charset.forName("UTF-16BE"); |
| 56 | 1 | private static final Charset UTF16LE = Charset.forName("UTF-16LE"); |
| 57 | |
|
| 58 | |
PushbackInputStream internalIn; |
| 59 | 1355 | InputStreamReader internalIn2 = null; |
| 60 | |
|
| 61 | |
private static final int BOM_SIZE = 3; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 1355 | public UnicodeReader(InputStream in) { |
| 68 | 1355 | internalIn = new PushbackInputStream(in, BOM_SIZE); |
| 69 | 1355 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public String getEncoding() { |
| 76 | 4 | return internalIn2.getEncoding(); |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
protected void init() throws IOException { |
| 84 | 2685 | if (internalIn2 != null) |
| 85 | 1330 | return; |
| 86 | |
|
| 87 | |
Charset encoding; |
| 88 | 1355 | byte bom[] = new byte[BOM_SIZE]; |
| 89 | |
int n, unread; |
| 90 | 1355 | n = internalIn.read(bom, 0, bom.length); |
| 91 | |
|
| 92 | 1354 | if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) { |
| 93 | 2 | encoding = UTF8; |
| 94 | 2 | unread = n - 3; |
| 95 | 1352 | } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { |
| 96 | 2 | encoding = UTF16BE; |
| 97 | 2 | unread = n - 2; |
| 98 | 1350 | } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { |
| 99 | 1 | encoding = UTF16LE; |
| 100 | 1 | unread = n - 2; |
| 101 | |
} else { |
| 102 | |
|
| 103 | 1349 | encoding = UTF8; |
| 104 | 1349 | unread = n; |
| 105 | |
} |
| 106 | |
|
| 107 | 1354 | if (unread > 0) |
| 108 | 1347 | internalIn.unread(bom, (n - unread), unread); |
| 109 | |
|
| 110 | |
|
| 111 | 1354 | CharsetDecoder decoder = encoding.newDecoder().onUnmappableCharacter( |
| 112 | |
CodingErrorAction.REPORT); |
| 113 | 1354 | internalIn2 = new InputStreamReader(internalIn, decoder); |
| 114 | 1354 | } |
| 115 | |
|
| 116 | |
public void close() throws IOException { |
| 117 | 1 | init(); |
| 118 | 1 | internalIn2.close(); |
| 119 | 1 | } |
| 120 | |
|
| 121 | |
public int read(char[] cbuf, int off, int len) throws IOException { |
| 122 | 2684 | init(); |
| 123 | 2683 | return internalIn2.read(cbuf, off, len); |
| 124 | |
} |
| 125 | |
} |