Can someone help me figure out what I am doing wrong here? This method does nothing except produce empty lines when calling new InputStreamReader(stream, getSet(stream);
Thank you all!
private static final byte[] UTF8_BOM = new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
private static final byte[] UTF16LE_BOM = new byte[] {(byte) 0xFF, (byte) 0xFE};
private static final byte[] UTF16BE_BOM = new byte[] {(byte) 0xFE, (byte) 0xFF};
public static Charset getSet(final InputStream stream) throws IOException {
final byte[] UTF8_Buffer = new byte[3];
final byte[] UTF16LE_Buffer = new byte[2];
final byte[] UTF16BE_Buffer = new byte[2];
int byteValue;
InputStream bufferedStream = new BufferedInputStream(stream);
bufferedStream.mark(3);
byteValue = stream.read();
UTF16LE_Buffer[0] = (byte) byteValue;
UTF16BE_Buffer[0] = (byte) byteValue;
UTF8_Buffer[0] = (byte) byteValue;
byteValue = bufferedStream.read();
UTF16LE_Buffer[1] = (byte) byteValue;
UTF16BE_Buffer[1] = (byte) byteValue;
UTF8_Buffer[1] = (byte) byteValue;
if (Arrays.equals(UTF16LE_Buffer, UTF16LE_BOM)) {
return StandardCharsets.UTF_16LE;
} else if (Arrays.equals(UTF16BE_Buffer, UTF16LE_BOM)) {
return StandardCharsets.UTF_16BE;
} else {
byteValue = bufferedStream.read();
UTF8_Buffer[2] = (byte) byteValue;
if (Arrays.equals(UTF8_Buffer, UTF8_BOM)) {
return StandardCharsets.UTF_8;
}
}
bufferedStream.reset();
return StandardCharsets.UTF_8;
}