Skip to content

Commit 71a3bf2

Browse files
Isira-Seneviratnetheimpulson
authored andcommitted
Use InputStream#transferTo()
1 parent ebb9379 commit 71a3bf2

1 file changed

Lines changed: 9 additions & 19 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/ZipHelper.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
*/
3838

3939
public final class ZipHelper {
40-
41-
private static final int BUFFER_SIZE = 2048;
42-
4340
@FunctionalInterface
4441
public interface InputStreamConsumer {
4542
void acceptStream(InputStream inputStream) throws IOException;
@@ -80,13 +77,13 @@ public static void addFileToZip(final ZipOutputStream outZip,
8077
final String nameInZip,
8178
final OutputStreamConsumer streamConsumer) throws IOException {
8279
final byte[] bytes;
83-
try (ByteArrayOutputStream byteOutput = new ByteArrayOutputStream()) {
80+
try (var byteOutput = new ByteArrayOutputStream()) {
8481
streamConsumer.acceptStream(byteOutput);
8582
bytes = byteOutput.toByteArray();
8683
}
8784

88-
try (ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes)) {
89-
ZipHelper.addFileToZip(outZip, nameInZip, byteInput);
85+
try (var byteInput = new ByteArrayInputStream(bytes)) {
86+
addFileToZip(outZip, nameInZip, byteInput);
9087
}
9188
}
9289

@@ -97,19 +94,12 @@ public static void addFileToZip(final ZipOutputStream outZip,
9794
* @param nameInZip the path of the file inside the zip
9895
* @param inputStream the content to put inside the file
9996
*/
100-
public static void addFileToZip(final ZipOutputStream outZip,
101-
final String nameInZip,
102-
final InputStream inputStream) throws IOException {
103-
final byte[] data = new byte[BUFFER_SIZE];
104-
try (BufferedInputStream bufferedInputStream =
105-
new BufferedInputStream(inputStream, BUFFER_SIZE)) {
106-
final ZipEntry entry = new ZipEntry(nameInZip);
107-
outZip.putNextEntry(entry);
108-
int count;
109-
while ((count = bufferedInputStream.read(data, 0, BUFFER_SIZE)) != -1) {
110-
outZip.write(data, 0, count);
111-
}
112-
}
97+
private static void addFileToZip(final ZipOutputStream outZip,
98+
final String nameInZip,
99+
final InputStream inputStream) throws IOException {
100+
final var entry = new ZipEntry(nameInZip);
101+
outZip.putNextEntry(entry);
102+
inputStream.transferTo(outZip);
113103
}
114104

115105
/**

0 commit comments

Comments
 (0)