@Test public void canDecodeUtf16le() throws IOException, URISyntaxException { String filename = this.getClass().getResource(SAMPLE_UTF_16LE_TXT).toURI().getPath(); FileInputStream in = new FileInputStream(filename); byte data[] = new byte[ONE_KIBIBYTE]; in.read(data); in.close(); byte converted[] = UnicodeUtil.convert(data, "US-ASCII"); assertThat(new String(converted).trim(), is(EXPECTED_CONTENT)); }
public static byte[] convert(final byte[] bytes, final String desiredOutputEncoding) throws IOException { UnicodeInputStream uis = new UnicodeInputStream(new ByteArrayInputStream(bytes), "ASCII"); boolean unicodeOutputReqd = (getBOM(desiredOutputEncoding).equals("")) ? false : true; String enc = uis.getEncoding(); String BOM = getBOM(enc); if ("".equals(BOM)) { BOM = getBOM(desiredOutputEncoding); } uis.close(); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes, uis.getBomOffset(), bytes.length), enc)); Writer w = new BufferedWriter(new OutputStreamWriter(out, desiredOutputEncoding)); if (!"".equals(BOM) && unicodeOutputReqd) { w.write(BOM); } char[] buffer = new char[4096]; int len; while (true) { len = br.read(buffer); if (len == -1) { break; } w.write(buffer, 0, len); } br.close(); w.close(); return out.toByteArray(); }
UnicodeUtil { public static byte[] convert(final byte[] bytes, final String desiredOutputEncoding) throws IOException { UnicodeInputStream uis = new UnicodeInputStream(new ByteArrayInputStream(bytes), "ASCII"); boolean unicodeOutputReqd = (getBOM(desiredOutputEncoding).equals("")) ? false : true; String enc = uis.getEncoding(); String BOM = getBOM(enc); if ("".equals(BOM)) { BOM = getBOM(desiredOutputEncoding); } uis.close(); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes, uis.getBomOffset(), bytes.length), enc)); Writer w = new BufferedWriter(new OutputStreamWriter(out, desiredOutputEncoding)); if (!"".equals(BOM) && unicodeOutputReqd) { w.write(BOM); } char[] buffer = new char[4096]; int len; while (true) { len = br.read(buffer); if (len == -1) { break; } w.write(buffer, 0, len); } br.close(); w.close(); return out.toByteArray(); } }
UnicodeUtil { public static byte[] convert(final byte[] bytes, final String desiredOutputEncoding) throws IOException { UnicodeInputStream uis = new UnicodeInputStream(new ByteArrayInputStream(bytes), "ASCII"); boolean unicodeOutputReqd = (getBOM(desiredOutputEncoding).equals("")) ? false : true; String enc = uis.getEncoding(); String BOM = getBOM(enc); if ("".equals(BOM)) { BOM = getBOM(desiredOutputEncoding); } uis.close(); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes, uis.getBomOffset(), bytes.length), enc)); Writer w = new BufferedWriter(new OutputStreamWriter(out, desiredOutputEncoding)); if (!"".equals(BOM) && unicodeOutputReqd) { w.write(BOM); } char[] buffer = new char[4096]; int len; while (true) { len = br.read(buffer); if (len == -1) { break; } w.write(buffer, 0, len); } br.close(); w.close(); return out.toByteArray(); }  }
UnicodeUtil { public static byte[] convert(final byte[] bytes, final String desiredOutputEncoding) throws IOException { UnicodeInputStream uis = new UnicodeInputStream(new ByteArrayInputStream(bytes), "ASCII"); boolean unicodeOutputReqd = (getBOM(desiredOutputEncoding).equals("")) ? false : true; String enc = uis.getEncoding(); String BOM = getBOM(enc); if ("".equals(BOM)) { BOM = getBOM(desiredOutputEncoding); } uis.close(); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes, uis.getBomOffset(), bytes.length), enc)); Writer w = new BufferedWriter(new OutputStreamWriter(out, desiredOutputEncoding)); if (!"".equals(BOM) && unicodeOutputReqd) { w.write(BOM); } char[] buffer = new char[4096]; int len; while (true) { len = br.read(buffer); if (len == -1) { break; } w.write(buffer, 0, len); } br.close(); w.close(); return out.toByteArray(); }  static byte[] convert(final byte[] bytes, final String desiredOutputEncoding); static String getBOM(final String enc); static BufferedReader getUnicodeAwareBufferedReader(String file); static BufferedReader getUnicodeAwareBufferedReader(InputStream stream); }
UnicodeUtil { public static byte[] convert(final byte[] bytes, final String desiredOutputEncoding) throws IOException { UnicodeInputStream uis = new UnicodeInputStream(new ByteArrayInputStream(bytes), "ASCII"); boolean unicodeOutputReqd = (getBOM(desiredOutputEncoding).equals("")) ? false : true; String enc = uis.getEncoding(); String BOM = getBOM(enc); if ("".equals(BOM)) { BOM = getBOM(desiredOutputEncoding); } uis.close(); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes, uis.getBomOffset(), bytes.length), enc)); Writer w = new BufferedWriter(new OutputStreamWriter(out, desiredOutputEncoding)); if (!"".equals(BOM) && unicodeOutputReqd) { w.write(BOM); } char[] buffer = new char[4096]; int len; while (true) { len = br.read(buffer); if (len == -1) { break; } w.write(buffer, 0, len); } br.close(); w.close(); return out.toByteArray(); }  static byte[] convert(final byte[] bytes, final String desiredOutputEncoding); static String getBOM(final String enc); static BufferedReader getUnicodeAwareBufferedReader(String file); static BufferedReader getUnicodeAwareBufferedReader(InputStream stream);  }