0

Some character not support by certain charset, so below test fail. I would like to use html entity to encode ONLY those not supported character. How, in java?

public void testWriter() throws IOException{
    String c = "\u00A9";
    String encoding = "gb2312";
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Writer writer  = new BufferedWriter(new OutputStreamWriter(outStream, encoding));
    writer.write(c);
    writer.close();
    String result = new String(outStream.toByteArray(), encoding);
    assertEquals(c, result);
}

3 Answers 3

4

I'm not positive I understand the question, but something like this might help:

import java.nio.charset.CharsetEncoder;

...

  StringBuilder buf = new StringBuilder(c.length());
  CharsetEncoder enc = Charset.forName("gb2312");
  for (int idx = 0; idx < c.length(); ++idx) {
    char ch = c.charAt(idx);
    if (enc.canEncode(ch))
      buf.append(ch);
    else {
      buf.append("&#");
      buf.append((int) ch);
      buf.append(';');
    }
  }
  String result = buf.toString();

This code is not robust, because it doesn't handle characters beyond the Basic Multilingual Plane. But iterating over code points in the String, and using the canEncode(CharSequence) method of the CharsetEncoder, you should be able to handle any character.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I believe this canEncode() form CharsetEncoder is what I am looking for.
0

Try using StringEscapeUtils from apache commons.

1 Comment

StringEscapeUtils escape everything non-ASCII (not only what cannot be encoded).
0

Just use utf-8, and that way there is no reason to use entities. If there is an argument that some clients need gb2312 because they don't understand Unicode, then entities are not much use either, because the numeric entities represent Unicode code points.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.