From f9307c7110b0775763ed88ea0bbaa108bb36edeb Mon Sep 17 00:00:00 2001 From: Winston Li Date: Wed, 2 Aug 2017 21:40:00 +0100 Subject: [PATCH] Check file size on tar input path and use stream copying instead of buffering fully --- .../java/uk/ac/ic/wlgitbridge/util/Tar.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java index 4aed2a43ce..5a257616ab 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java @@ -100,11 +100,7 @@ public class Tar { continue; } long size = e.getSize(); - Preconditions.checkArgument( - size >= 0 && size <= Integer.MAX_VALUE, - "file too big (" + size + " B): " + - "tarTo should have thrown an IOException" - ); + checkFileSize(size); try (OutputStream out = new FileOutputStream(f)) { /* TarInputStream pretends each entry's EOF is the stream's EOF */ @@ -113,6 +109,14 @@ public class Tar { } } + private static void checkFileSize(long size) { + Preconditions.checkArgument( + size >= 0 && size <= Integer.MAX_VALUE, + "file too big (" + size + " B): " + + "tarTo should have thrown an IOException" + ); + } + private static void addTarEntry( TarArchiveOutputStream tout, Path base, @@ -151,13 +155,19 @@ public class Tar { Path base, File file ) throws IOException { - Preconditions.checkArgument(file.isFile()); + Preconditions.checkArgument( + file.isFile(), + "given file" + + " is not file: %s", file); + checkFileSize(file.length()); String name = base.relativize( Paths.get(file.getAbsolutePath()) ).toString(); ArchiveEntry entry = tout.createArchiveEntry(file, name); tout.putArchiveEntry(entry); - tout.write(FileUtils.readFileToByteArray(file)); + try (InputStream in = new FileInputStream(file)) { + IOUtils.copy(in, tout); + } tout.closeArchiveEntry(); }