{"id":5451,"date":"2014-03-30T22:12:04","date_gmt":"2014-03-30T22:12:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/inputstream-dealing-with-network-changes-collection-of-common-programming-errors\/"},"modified":"2014-03-30T22:12:04","modified_gmt":"2014-03-30T22:12:04","slug":"inputstream-dealing-with-network-changes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/inputstream-dealing-with-network-changes-collection-of-common-programming-errors\/","title":{"rendered":"InputStream &#8211; Dealing with network changes-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m downloading an attachment using Java mail API and whenever there is a small change in network state, my app gets stuck and I have to restart it, it&#8217;s not even crashing. This is the code snippet:<\/p>\n<pre><code>InputStream is = bodyPart.getInputStream();\n\nString fileName = MimeUtility.decodeText(bodyPart.getFileName());\n\n\/\/ Downloading the file\nFile f = new File(Constants.getPath() + fileName);\ntry {\n    FileOutputStream fos;\n    fos = new FileOutputStream(f);\n\n    byte[] buf = new byte[8*1024];\n    int bytesRead;\n\n    while ((bytesRead = is.read(buf)) != -1) {\n    fos.write(buf, 0, bytesRead);\n    }\n    fos.close();\n}\n<\/code><\/pre>\n<p>What is the best way to deal with this issue? Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m downloading an attachment using Java mail API and whenever there is a small change in network state, my app gets stuck and I have to restart it, it&#8217;s not even crashing. This is the code snippet: InputStream is = bodyPart.getInputStream(); String fileName = MimeUtility.decodeText(bodyPart.getFileName()); \/\/ Downloading the file File f = new File(Constants.getPath() + [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5451","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5451"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5451\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}