Skip to content

Commit 3d828cb

Browse files
committed
Try to be more resilient when verifying the extracted library
1 parent 8bafdf7 commit 3d828cb

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

src/main/java/org/fusesource/jansi/internal/JansiLoader.java

+32-12
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,45 @@ public boolean accept(File dir, String name) {
106106
}
107107
}
108108

109-
private static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException {
109+
private static int readNBytes(InputStream in, byte[] b) throws IOException {
110+
int n = 0;
111+
int len = b.length;
112+
while (n < len) {
113+
int count = in.read(b, n, len - n);
114+
if (count <= 0)
115+
break;
116+
n += count;
117+
}
118+
return n;
119+
}
120+
121+
private static String contentsEquals(InputStream in1, InputStream in2) throws IOException {
110122
byte[] buffer1 = new byte[8192];
111123
byte[] buffer2 = new byte[8192];
112-
int numRead1 = 0;
113-
int numRead2 = 0;
124+
int numRead1;
125+
int numRead2;
114126
while (true) {
115-
numRead1 = in1.read(buffer1);
116-
numRead2 = in2.read(buffer2);
117-
if (numRead1 > -1) {
127+
numRead1 = readNBytes(in1, buffer1);
128+
numRead2 = readNBytes(in2, buffer2);
129+
if (numRead1 > 0) {
130+
if (numRead2 <= 0) {
131+
return "EOF on second stream but not first";
132+
}
118133
if (numRead2 != numRead1) {
119-
return false;
134+
return "Read size different (" + numRead1 + " vs " + numRead2 + ")";
120135
}
121136
// Otherwise same number of bytes read
122137
if (!Arrays.equals(buffer1, buffer2)) {
123-
return false;
138+
return "Content differs";
124139
}
125140
// Otherwise same bytes read, so continue ...
126141
} else {
127142
// Nothing more in stream 1 ...
128-
return numRead2 < 0;
143+
if (numRead2 > 0) {
144+
return "EOF on first stream but not second";
145+
} else {
146+
return null;
147+
}
129148
}
130149
}
131150
}
@@ -157,7 +176,7 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
157176
if (!extractedLckFile.exists()) {
158177
new FileOutputStream(extractedLckFile).close();
159178
}
160-
OutputStream out = new BufferedOutputStream(new FileOutputStream(extractedLibFile));
179+
OutputStream out = new FileOutputStream(extractedLibFile);
161180
try {
162181
copy(in, out);
163182
} finally {
@@ -180,8 +199,9 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
180199
try {
181200
InputStream extractedLibIn = new FileInputStream(extractedLibFile);
182201
try {
183-
if (!contentsEquals(nativeIn, extractedLibIn)) {
184-
throw new RuntimeException(String.format("Failed to write a native library file at %s", extractedLibFile));
202+
String eq = contentsEquals(nativeIn, extractedLibIn);
203+
if (eq != null) {
204+
throw new RuntimeException(String.format("Failed to write a native library file at %s because %s", extractedLibFile, eq));
185205
}
186206
} finally {
187207
extractedLibIn.close();

0 commit comments

Comments
 (0)