Skip to Content
author's profile photo Former Member
Former Member

Reading mixed language content from a resource

Hello all,

I have a resource 'res' and I want to read its contents. I've written the following code to do this.

StringBuffer sb = new StringBuffer();
IContent con = res.getUnfilteredContent();
int len = (int)con.getContentLength();// It's a small resource
byte[] bArray = new byte[len];
InputStream is = con.getInputStream();
is.read(bArray, 0, len);
is.close();
for(int i=0;i<bArray.length;i++)
  sb.append((char)(bArray<i> & 0xFF));
return sb.toString();

This reads English fine but my resource has English and another language which comes back as garbage.

It's an XML Form that I'm reading and I'm trying to grab a specific element, an image link. The path is mixed language.

Any ideas what I can do to solve this?

Regards,

Patrick.

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on May 26, 2005 at 09:51 AM

    Hi Patrick,

    this could be an encoding issue. Other languages like German could contain umlauts that can mess up your content.

    Try the following code

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    InputStream input = aContent.getInputStream();

    while((b = input.read()) > 0 ) {

    bos.write(b);

    }

    if (aContent.getEncoding() != null)

    result = new String(bos.toByteArray( aContent.getEncoding());

    else

    result = new String(bos.toByteArray());

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Frank, thankyou!

      bos.toByteArray() doesn't take an encoding String, but bos.toString() can. So I changed the line

      result = new String(bos.toByteArray(aContent.getEncoding());

      to

      result = bos.toString(con.getEncoding());

      I haven't been able to award points all day, perhaps the service is under maintenance (I also owe Detlev points from this morning). I'll keep checking back until I can award you the 10 points.

      Thanks again Frank,

      Patrick.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.