I have this piece of code that takes the server response and writes it into a file.
The file contains json data. I write response into file in order to scan json sequentially and to avoid to load big json data in a List!
I Think exception is thrown in this method but I'm not sure!
public File getData(final String url) throws URISyntaxException, AuthenticationException, IOException, ClientProtocolException, HttpResponseException {
final HttpGet getRequest = new HttpGet(new URI(url));
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
getRequest.addHeader(new BasicScheme().authenticate(creds, getRequest));
getRequest.setHeader("Content-Type", "application/json");
final ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
final byte[] responseBody = mClient.execute(getRequest, responseHandler);
final File output = new File(FileConfig.TEMP_PATH + System.currentTimeMillis()+".json");
final FileOutputStream fos = new FileOutputStream(output.getPath());
fos.write(responseBody);
fos.close();
return output;
}
But I've noticed that recently (I don't know why) I get this exception:
01-22 07:45:51.809: E/System(9055): Uncaught exception thrown by finalizer
01-22 07:45:51.833: E/System(9055): java.io.IOException: close failed: EIO (I/O error)
01-22 07:45:51.833: E/System(9055): at libcore.io.IoUtils.close(IoUtils.java:41)
01-22 07:45:51.833: E/System(9055): at java.io.FileInputStream.close(FileInputStream.java:121)
01-22 07:45:51.833: E/System(9055): at java.io.FileInputStream.finalize(FileInputStream.java:142)
01-22 07:45:51.833: E/System(9055): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)
01-22 07:45:51.833: E/System(9055): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-22 07:45:51.833: E/System(9055): at java.lang.Thread.run(Thread.java:856)
01-22 07:45:51.833: E/System(9055): Caused by: libcore.io.ErrnoException: close failed: EIO (I/O error)
01-22 07:45:51.833: E/System(9055): at libcore.io.Posix.close(Native Method)
01-22 07:45:51.833: E/System(9055): at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
01-22 07:45:51.833: E/System(9055): at libcore.io.IoUtils.close(IoUtils.java:38)
01-22 07:45:51.833: E/System(9055): ... 5 more
01-22 07:45:51.833: E/System(9055): Uncaught exception thrown by finalizer
01-22 07:45:51.841: E/System(9055): java.io.IOException: close failed: EIO (I/O error)
01-22 07:45:51.841: E/System(9055): at libcore.io.IoUtils.close(IoUtils.java:41)
01-22 07:45:51.841: E/System(9055): at java.io.FileInputStream.close(FileInputStream.java:121)
01-22 07:45:51.841: E/System(9055): at java.io.FileInputStream.finalize(FileInputStream.java:142)
01-22 07:45:51.841: E/System(9055): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)
01-22 07:45:51.841: E/System(9055): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-22 07:45:51.841: E/System(9055): at java.lang.Thread.run(Thread.java:856)
01-22 07:45:51.841: E/System(9055): Caused by: libcore.io.ErrnoException: close failed: EIO (I/O error)
01-22 07:45:51.841: E/System(9055): at libcore.io.Posix.close(Native Method)
01-22 07:45:51.841: E/System(9055): at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
01-22 07:45:51.841: E/System(9055): at libcore.io.IoUtils.close(IoUtils.java:38)
01-22 07:45:51.841: E/System(9055): ... 5 more
Everything seems to work, but I'm perplexed about this exception.
The targetSdk ok my app is 13.
Thanks for any comment / response!
update your code some thing like this.
public File getData(final String url) throws URISyntaxException, AuthenticationException, IOException, ClientProtocolException, HttpResponseException {
FileOutputStream fos = null;
File output = null;
final HttpGet getRequest = new HttpGet(new URI(url));
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
username, password);
getRequest.addHeader(new BasicScheme().authenticate(creds, getRequest));
getRequest.setHeader("Content-Type", "application/json");
final ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
final byte[] responseBody = mClient
.execute(getRequest, responseHandler);
try {
output = new File(FileConfig.TEMP_PATH + System.currentTimeMillis()
+ ".json");
fos = new FileOutputStream(output.getPath());
fos.write(responseBody);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (Exception e) {
} finally {
if (fos != null) {
fos = null;
}
}
return output;
}
Because when your trying to fos.wrire() or fos.close() it may through IOException and when your calling new FileOutputStream(output.getPath()); it may through FileNotFoundException.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With