Need help with String encoding issue

Discussion in 'Java' started by rich.manalang@gmail.com, Sep 22, 2006.

  1. Guest

    I'm writting a servlet filter that manipulates the http response body
    (injecting HTML). It works fine with pages using the English charset,
    but when processing a page with double-byte chars, some of the
    characters are junk.

    When processing the OutputStream, I create a ByteArrayOutputStream

    baStream = new ByteArrayOutputStream();

    then I create a string (forcing it to UTF-8) with that stream:

    String str = new String(baStream.toByteArray(), "UTF-8");

    I then manipulate that string using standard regex, then output it back
    to the browser:

    outStream.write(str.getBytes());

    The problem is I don't know a lot about how charsets work in Java. I
    do know that Java's native string charset is UTF-16, but beyond that,
    I'm not sure how to make sure that what comes into my servlet filter is
    what goes out.

    Thanks in advance!

    Rich
     
    , Sep 22, 2006
    #1
    1. Advertising

  2. wrote:

    > outStream.write(str.getBytes());


    here you should use str.getBytes("UTF-8");

    Alternatively use a Writer instead of an OutputStream, that
    you can get from the servlet as well. Then you can write
    String direclty without coping with the encoding to be used.

    Or you wrap an OutputStreamWriter around your OutputStream
    with specifying the encoding you want to use within the
    constructor.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Sep 23, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hardy Wang

    Encoding.Default and Encoding.UTF8

    Hardy Wang, Jun 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    18,996
    Jon Skeet [C# MVP]
    Jun 9, 2004
  2. Replies:
    1
    Views:
    23,524
    Real Gagnon
    Oct 8, 2004
  3. Angus
    Replies:
    3
    Views:
    351
  4. howa

    CGI query string encoding issue...

    howa, Mar 4, 2009, in forum: Perl Misc
    Replies:
    3
    Views:
    425
    Eric Pozharski
    Mar 6, 2009
  5. Replies:
    2
    Views:
    398
Loading...

Share This Page