append part of java byte array to StringBuilder -
how append portion of byte array stringbuilder object under java? have segment of function reads inputstream byte array. want append whatever read stringbuilder object:
byte[] buffer = new byte[4096]; inputstream is; // //some setup code // while (is.available() > 0) { int len = is.read(buffer); //i want append buffer[0] buffer[len] stringbuilder @ point }
you should not use stringbuilder
this, since can cause encoding errors variable-width encodings. can use java.io.bytearrayoutputstream
instead, , convert string when data has been read:
byte[] buffer = new byte[4096]; bytearrayoutputstream out = new bytearrayoutputstream(); inputstream is; // //some setup code // while (is.available() > 0) { int len = is.read(buffer); out.write(buffer, 0, len); } string result = out.tostring("utf-8"); // instance
if encoding known not contain multi-byte sequences (you working ascii data, instance), using stringbuilder
work.
Comments
Post a Comment