cancel
Showing results for 
Search instead for 
Did you mean: 

StringBuffer Encoding

Former Member
0 Kudos

byte[] message=outMail.getMessage();

String msg=new String(message,"UTF-8");

System.out.println("msg");

if i want to change to StringBuffer..how do i encode this..

StringBuffer sb = new StringBuffer();

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

I am not sure what you meant, but would this work?

byte[] message=outMail.getMessage();

String msg=new String(message,"UTF-8");

StringBuffer sb = new StringBuffer(msg);

Thanks

Sajesh

Answers (1)

Answers (1)

Former Member
0 Kudos

Try this:


import java.io.*;
...
byte[] message = outMail.getMessage();

// set up an InputStream and convert to a character Reader
InputStream in = new ByteArrayInputStream(message);
Reader reader = new InputStreamReader(in, "UTF-8");

// now copy the Reader's content to a Writer
Writer writer = new StringWriter();
char[] buf = new char[4096];
int len;
while ((len = reader.read(buf)) >= 0)
  writer.write(buf, 0, len);

// and finally get the StringBuffer from the Writer
StringBuffer sb = writer.getBuffer();

Regards,

Jens

Former Member
0 Kudos

// and finally get the StringBuffer from the Writer

StringBuffer sb = writer.getBuffer();

error ...

i can get this

writer.getBuffer();

pls advice

prashil
Advisor
Advisor
0 Kudos

Hi Yzme,

Please post the error you are facing.

Thanks,

Prashil

Former Member
0 Kudos

// and finally get the StringBuffer from the Writer

StringBuffer sb = writer.getBuffer();

there is no writer.getBuffer() .....getBuffer is not in the intellisense

pls advice

Former Member
0 Kudos

I'm very sorry; the line after the second comment should read


StringWriter writer = new StringWriter();

(I had tried out the code with BeanShell and didn't get any errors there.)

Regards,

Jens