Skip to content

Commit

Permalink
Read only first 8 bytes of class in JavaClassfileVersion
Browse files Browse the repository at this point in the history
We don't need to read whole class file, only first 8 bytes are used.
  • Loading branch information
slawekjaranowski committed Sep 25, 2024
1 parent 132aa7b commit abcb0a6
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.codehaus.plexus.languages.java.version;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -62,9 +63,19 @@ public static JavaClassfileVersion of(byte[] bytes) {
* @return the {@link JavaClassfileVersion} of the path java class
*/
public static JavaClassfileVersion of(Path path) {
try {
byte[] readAllBytes = Files.readAllBytes(path);
return of(readAllBytes);
try (InputStream is = Files.newInputStream(path)) {
byte[] bytes = new byte[8];
int total = 0;
while (total < 8) {
int l = is.read(bytes, total, 8 - total);
if (l > 0) {
total += l;
}
if (l == -1) {
break;
}
}
return of(bytes);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
Expand Down

0 comments on commit abcb0a6

Please sign in to comment.