what does byte[] do while unzipping file ?

public static String unzip(File source) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPInputStream gis = new GZIPInputStream(new FileInputStream(source))) {
byte[] buffer = new byte[1024];
int len; while ((len = gis.read(buffer)) > 0) { output.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } return output.toString(StandardCharsets.UTF_8); }
public static String unzip(File source) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (GZIPInputStream gis = new GZIPInputStream(new FileInputStream(source))) {
byte[] buffer = new byte[1024];
int len; while ((len = gis.read(buffer)) > 0) { output.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } return output.toString(StandardCharsets.UTF_8); }
This code gets the string contained in a zim file. What does the byte[] buffer = new byte[1024]; do ? Why its size is 1024 ?
6 Replies
JavaBot
JavaBot3y ago
Hey, @tyranobast! Please remember to /close this post once your question has been answered!
Tyranobast
TyranobastOP3y ago
*zip not zim
straightface
straightface3y ago
it reads given length of bytes from the inputstream
Kyo-chan
Kyo-chan3y ago
It's pretty much a buffer, to receive the bytes being extracted, before writing them back to the destination. Doing that one byte at a time would have a lot of overhead, so it's done using a buffer in the middle It doesn't matter the size. It just needs to be "far more than 1, and not unrealistically gargantuan". Pick anything between 500 and 10000 It's 1024 because as it is close to a low-level concern, and there is this cargo cult that anything that can be described as such loves powers of 2 You don't have to handle that yourself. You could call the InputStream's readAllBytes() method. It will do the same internally.
Tyranobast
TyranobastOP3y ago
Okay thanks !
JavaBot
JavaBot3y ago
Post Closed
This post has been closed by <@428632258413330442>.
Want results from more Discord servers?
Add your server