Skip to content

Commit 2d3424a

Browse files
authored
Merge pull request #220 from warownia1/master
Fix SimpleHttpClient#toBytes, make sure return value is always byte[].
2 parents 82d5509 + 1bb1206 commit 2d3424a

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

sources/net.sf.j2s.java.core/src/javajs/http/SimpleHttpClient.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,27 @@ public HttpRequest clearFormParts(String name) {
287287
}
288288

289289
private byte[] toBytes(Object data) {
290-
try {
291-
if (data == null || data instanceof byte[]) {
292-
} else if (data instanceof File) {
293-
FileInputStream fis = new FileInputStream((File) data);
294-
data = getBytes(fis);
295-
fis.close();
296-
} else if (data instanceof InputStream) {
297-
InputStream is = (InputStream) data;
298-
data = getBytes(is);
299-
is.close();
300-
} else {
301-
data = data.toString().getBytes();
290+
if (data == null || data instanceof byte[]) {
291+
return (byte[]) data;
292+
} else if (data instanceof File) {
293+
try (FileInputStream fis = new FileInputStream((File) data)) {
294+
return getBytes(fis);
302295
}
303-
} catch (IOException e) {
296+
catch (IOException e) {
297+
e.printStackTrace();
298+
return null;
299+
}
300+
} else if (data instanceof InputStream) {
301+
try (InputStream is = (InputStream) data) {
302+
return getBytes(is);
303+
}
304+
catch (IOException e) {
305+
e.printStackTrace();
306+
return null;
307+
}
308+
} else {
309+
return data.toString().getBytes();
304310
}
305-
return (byte[]) data;
306311
}
307312

308313
@Override

0 commit comments

Comments
 (0)