Skip to content

Commit

Permalink
Try to be more resilient when verifying the extracted library
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 26, 2020
1 parent 8bafdf7 commit 3d828cb
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/main/java/org/fusesource/jansi/internal/JansiLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,45 @@ public boolean accept(File dir, String name) {
}
}

private static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException {
private static int readNBytes(InputStream in, byte[] b) throws IOException {
int n = 0;
int len = b.length;
while (n < len) {
int count = in.read(b, n, len - n);
if (count <= 0)
break;
n += count;
}
return n;
}

private static String contentsEquals(InputStream in1, InputStream in2) throws IOException {
byte[] buffer1 = new byte[8192];
byte[] buffer2 = new byte[8192];
int numRead1 = 0;
int numRead2 = 0;
int numRead1;
int numRead2;
while (true) {
numRead1 = in1.read(buffer1);
numRead2 = in2.read(buffer2);
if (numRead1 > -1) {
numRead1 = readNBytes(in1, buffer1);
numRead2 = readNBytes(in2, buffer2);
if (numRead1 > 0) {
if (numRead2 <= 0) {
return "EOF on second stream but not first";
}
if (numRead2 != numRead1) {
return false;
return "Read size different (" + numRead1 + " vs " + numRead2 + ")";
}
// Otherwise same number of bytes read
if (!Arrays.equals(buffer1, buffer2)) {
return false;
return "Content differs";
}
// Otherwise same bytes read, so continue ...
} else {
// Nothing more in stream 1 ...
return numRead2 < 0;
if (numRead2 > 0) {
return "EOF on first stream but not second";
} else {
return null;
}
}
}
}
Expand Down Expand Up @@ -157,7 +176,7 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
if (!extractedLckFile.exists()) {
new FileOutputStream(extractedLckFile).close();
}
OutputStream out = new BufferedOutputStream(new FileOutputStream(extractedLibFile));
OutputStream out = new FileOutputStream(extractedLibFile);
try {
copy(in, out);
} finally {
Expand All @@ -180,8 +199,9 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
try {
InputStream extractedLibIn = new FileInputStream(extractedLibFile);
try {
if (!contentsEquals(nativeIn, extractedLibIn)) {
throw new RuntimeException(String.format("Failed to write a native library file at %s", extractedLibFile));
String eq = contentsEquals(nativeIn, extractedLibIn);
if (eq != null) {
throw new RuntimeException(String.format("Failed to write a native library file at %s because %s", extractedLibFile, eq));
}
} finally {
extractedLibIn.close();
Expand Down

0 comments on commit 3d828cb

Please sign in to comment.