cancel
Showing results for 
Search instead for 
Did you mean: 

Reading mixed language content from a resource

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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());

Former Member
0 Kudos

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.

Answers (0)