on 05-22-2005 12:21 PM
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.
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());
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
User | Count |
---|---|
79 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.