CharsetConverter { public ByteBuffer convert(CharBuffer from) throws FileEncoderException { ByteBuffer converted = null; try { converted = encoder.encode(from); if (checkBOM) { if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) { try { byte[] bytes = (bomChar + from.rewind().toString()).getBytes("UTF-16LE"); converted = ByteBuffer.wrap(bytes); converted.order(ByteOrder.LITTLE_ENDIAN); } catch (UnsupportedEncodingException uee) { Log.exception(uee); throw new ClientError("Translation of file content failed", uee); } } if (ignoreBOM) { int limit = converted.limit(); if (limit > 2) { byte[] bom = new byte[2]; converted.get(bom); if ((bom[0] == -2 && bom[1] == -1) || (bom[0] == -1 && bom[1] == -2)) { converted.position(2); converted.limit(limit - 2); } else { converted.rewind(); } } } else { ignoreBOM = true; } } else { converted.position(0); } } catch (CharacterCodingException cce) { Log.exception(cce); throw new FileEncoderException("Translation of file content failed", cce); } return converted; } CharsetConverter(Charset fromCharset, Charset toCharset,
	                        boolean ignoreBOM);  CharsetConverter(Charset fromCharset, Charset toCharset); String getToCharsetName(); ByteBuffer convert(CharBuffer from); ByteBuffer convert(ByteBuffer from);  }
@Test public void testCharsetConverterCharsetCharsetValid() throws IOException, UnsupportedCharsetException, ConnectionException, RequestException, AccessException, NoSuchObjectException, ConfigException, ResourceException, URISyntaxException, FileDecoderException, FileEncoderException { File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/shift_jis.txt"); CharsetConverter convert = new CharsetConverter(PerforceCharsets.getP4Charset("shiftjis"), CharsetDefs.UTF8); InputStream inStream = new FileInputStream(testResourceFile); byte[] bytes = new byte[2048]; int read = inStream.read(bytes); inStream.close(); byte[] trueIn = new byte[read]; System.arraycopy(bytes, 0, trueIn, 0, read); ByteBuffer bufIn = ByteBuffer.wrap(bytes, 0, read); ByteBuffer bufOut = convert.convert(bufIn); byte[] out = bufOut.array(); CharsetConverter convert2 = new CharsetConverter(CharsetDefs.UTF8, PerforceCharsets.getP4Charset("shiftjis")); ByteBuffer bufIn2 = ByteBuffer.wrap(out, 0, bufOut.limit()); ByteBuffer bufOut2 = convert2.convert(bufIn2); byte[] out2 = bufOut2.array(); byte[] trueOut = new byte[bufOut2.limit()]; System.arraycopy(out2, 0, trueOut, 0, bufOut2.limit()); Assert.assertArrayEquals(trueIn, trueOut); }
@Test(expected=FileDecoderException.class) public void testCharsetConverterCharsetCharsetInvalid() throws IOException, UnsupportedCharsetException, ConnectionException, RequestException, AccessException, NoSuchObjectException, ConfigException, ResourceException, URISyntaxException, FileDecoderException, FileEncoderException { File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/euc-jp.txt"); CharsetConverter convert = new CharsetConverter(PerforceCharsets.getP4Charset("shiftjis"), CharsetDefs.UTF8); InputStream inStream = new FileInputStream(testResourceFile); byte[] bytes = new byte[2048]; int read = inStream.read(bytes); inStream.close(); byte[] trueIn = new byte[read]; System.arraycopy(bytes, 0, trueIn, 0, read); ByteBuffer bufIn = ByteBuffer.wrap(bytes, 0, read); convert.convert(bufIn); }