Quellcode durchsuchen

Check file size on tar input path and use stream copying instead of buffering fully

Winston Li vor 9 Jahren
Ursprung
Commit
f9307c7110
1 geänderte Dateien mit 17 neuen und 7 gelöschten Zeilen
  1. 17 7
      services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java

+ 17 - 7
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java

@@ -100,11 +100,7 @@ public class Tar {
                 continue;
                 continue;
             }
             }
             long size = e.getSize();
             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)) {
             try (OutputStream out = new FileOutputStream(f)) {
                 /* TarInputStream pretends each
                 /* TarInputStream pretends each
                    entry's EOF is the stream's EOF */
                    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(
     private static void addTarEntry(
             TarArchiveOutputStream tout,
             TarArchiveOutputStream tout,
             Path base,
             Path base,
@@ -151,13 +155,19 @@ public class Tar {
             Path base,
             Path base,
             File file
             File file
     ) throws IOException {
     ) throws IOException {
-        Preconditions.checkArgument(file.isFile());
+        Preconditions.checkArgument(
+                file.isFile(),
+                "given file" +
+                " is not file: %s", file);
+        checkFileSize(file.length());
         String name = base.relativize(
         String name = base.relativize(
                 Paths.get(file.getAbsolutePath())
                 Paths.get(file.getAbsolutePath())
         ).toString();
         ).toString();
         ArchiveEntry entry = tout.createArchiveEntry(file, name);
         ArchiveEntry entry = tout.createArchiveEntry(file, name);
         tout.putArchiveEntry(entry);
         tout.putArchiveEntry(entry);
-        tout.write(FileUtils.readFileToByteArray(file));
+        try (InputStream in = new FileInputStream(file)) {
+            IOUtils.copy(in, tout);
+        }
         tout.closeArchiveEntry();
         tout.closeArchiveEntry();
     }
     }