Improve FileState.ofFile() overheads 13/104813/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Mar 2023 19:12:17 +0000 (20:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Mar 2023 19:13:24 +0000 (20:13 +0100)
We do not need the actual bytes read, hence we loop using a small-ish
buffer.

JIRA: YANGTOOLS-745
Change-Id: I2e35e3965c0a30a3d8368eeee45f5485a1765d29
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/FileState.java

index f3601a5b002d89e5963c6700a3dbb1e0562a16f6..dea2822197c02c5c10f2c07c6f05da58516ee803 100644 (file)
@@ -35,6 +35,8 @@ record FileState(@NonNull String path, long size, int crc32) {
         void writeTo(@NonNull OutputStream out) throws IOException;
     }
 
+    private static final int READ_BUFFER_SIZE = 8192;
+
     FileState {
         requireNonNull(path);
     }
@@ -45,7 +47,11 @@ record FileState(@NonNull String path, long size, int crc32) {
 
     static @NonNull FileState ofFile(final Path file) throws IOException {
         try (var cis = new CapturingInputStream(Files.newInputStream(file))) {
-            cis.readAllBytes();
+            // Essentially cis.readAllBytes() except we do not need the actual bytes
+            final var bytes = new byte[READ_BUFFER_SIZE];
+            while (cis.readNBytes(bytes, 0, READ_BUFFER_SIZE) != 0) {
+                // Nothing else
+            }
             return new FileState(file.toString(), cis.size(), cis.crc32c());
         }
     }