如何在某些编码中检查字符串是否可编码?

时间:2013-06-03 17:03:02

标签: java string encoding character-encoding

以下测试在转换后的Latin1上失败,因为非法字符被替换为值为63(问号)的字节。问题是这些字符应该更好地导致一些例外...

  @Test
  public void testEncoding() throws UnsupportedEncodingException {
    final String czech = "Řízeček a šampáňo a žízeň";
    // okay
    final byte[] bytesInLatin2 = czech.getBytes("ISO8859-2");
    // different bytes, but okay
    final byte[] bytesInWin1250 = czech.getBytes("Windows-1250");
    // different bytes, but okay
    final byte[] bytesInUtf8 = czech.getBytes("UTF-8");
    // nonsense; Ř,č,... are not in Latin1 code set!!!
    final byte[] bytesInLatin1 = czech.getBytes("ISO8859-1");

    System.out.println(Arrays.toString(bytesInLatin2));
    System.out.println(Arrays.toString(bytesInWin1250));
    System.out.println(Arrays.toString(bytesInUtf8));
    System.out.println(Arrays.toString(bytesInLatin1));
    System.out.flush();

    final String latin2 = new String(bytesInLatin2, "ISO8859-2");
    final String win1250 = new String(bytesInWin1250, "Windows-1250");
    final String utf8 = new String(bytesInUtf8, "UTF-8");
    final String latin1 = new String(bytesInLatin1, "ISO8859-1");

    Assert.assertEquals("latin2", czech, latin2);
    Assert.assertEquals("win1250", czech, win1250);
    Assert.assertEquals("utf8", czech, utf8);
    Assert.assertEquals("latin1", czech, latin1); // this test will fail!
  }

由于Java的这种行为,有很多情况下数据最终被破坏。是否可以使用任何库来验证字符串是否可编码?

2 个答案:

答案 0 :(得分:8)

我怀疑你正在寻找CharsetEncoder.canEncode(CharSequence)

Charset latin2 = Charset.forName("ISO8859-2");
boolean validInLatin2 = latin2.newEncoder().canEncode(czech);
...

答案 1 :(得分:1)

作为Jon Skeet建议的替代方案,您还可以使用CharsetEncoder类直接进行编码(使用encode方法),但首先调用onMalformedInput和{{3指定编码器在遇到错误输入时应该做什么的方法。

这种方式大多数情况下你只是在做一个简单的编码调用,但是如果出现任何问题你就会得到一个例外。