@@ -106,26 +106,45 @@ public boolean accept(File dir, String name) {
106
106
}
107
107
}
108
108
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 {
110
122
byte [] buffer1 = new byte [8192 ];
111
123
byte [] buffer2 = new byte [8192 ];
112
- int numRead1 = 0 ;
113
- int numRead2 = 0 ;
124
+ int numRead1 ;
125
+ int numRead2 ;
114
126
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
+ }
118
133
if (numRead2 != numRead1 ) {
119
- return false ;
134
+ return "Read size different (" + numRead1 + " vs " + numRead2 + ")" ;
120
135
}
121
136
// Otherwise same number of bytes read
122
137
if (!Arrays .equals (buffer1 , buffer2 )) {
123
- return false ;
138
+ return "Content differs" ;
124
139
}
125
140
// Otherwise same bytes read, so continue ...
126
141
} else {
127
142
// 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
+ }
129
148
}
130
149
}
131
150
}
@@ -157,7 +176,7 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
157
176
if (!extractedLckFile .exists ()) {
158
177
new FileOutputStream (extractedLckFile ).close ();
159
178
}
160
- OutputStream out = new BufferedOutputStream ( new FileOutputStream (extractedLibFile ) );
179
+ OutputStream out = new FileOutputStream (extractedLibFile );
161
180
try {
162
181
copy (in , out );
163
182
} finally {
@@ -180,8 +199,9 @@ private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, S
180
199
try {
181
200
InputStream extractedLibIn = new FileInputStream (extractedLibFile );
182
201
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 ));
185
205
}
186
206
} finally {
187
207
extractedLibIn .close ();
0 commit comments