java - Can not convert Base64 String and unGzip it properly -
i have base64 string. trying decode it, decompress it.
string texttodecode = "h4siaaaaaaaaaaegan//0jtqtdgc0ldqu9c40lfqunga0l7qstcw0l3qvdgl0lmrcuyiiaaaaa==\n"; byte[] data = base64.decode(texttodecode, base64.default); string result = gziputil.decompress(data);
code using decompression:
public static string decompress(byte[] compressed) throws ioexception { final int buffer_size = 32; bytearrayinputstream = new bytearrayinputstream(compressed); gzipinputstream gis = new gzipinputstream(is, buffer_size); stringbuilder string = new stringbuilder(); byte[] data = new byte[buffer_size]; int bytesread; while ((bytesread = gis.read(data)) != -1) { string.append(new string(data, 0, bytesread)); } gis.close(); is.close(); return string.tostring(); }
i should string:
Детализированный
insteam of it, getting string question mark symbols:
Детализирован��ый
what mistake? , how solve it?
one problem when converting bytes string (internally unicode) encoding not given. , multi-byte encoding utf-8 1 cannot take fixed number of bytes (like 32) , @ end have valid sequence.
you experienced loss of evidently half sequence. hence encoding utf-8.
final int buffer_size = 32; bytearrayinputstream = new bytearrayinputstream(compressed); gzipinputstream gis = new gzipinputstream(is, buffer_size); bytearrayoutputstream baos = new bytearrayoutputstream(); byte[] data = new byte[buffer_size]; int bytesread; while ((bytesread = gis.read(data)) != -1) { baos.write(data, 0, bytesread); } gis.close(); return baos.tostring("utf-8"); // or "windows-1251" ...
the above away buffer boundary problems, , specifies encoding, same code runs on different computers.
and mind:
new string(bytes, encoding)
string.getbytes(encoding)
Comments
Post a Comment