{"id":725,"date":"2011-04-26T17:20:42","date_gmt":"2011-04-26T09:20:42","guid":{"rendered":"http:\/\/www.strongd.net\/?p=725"},"modified":"2011-04-26T17:20:42","modified_gmt":"2011-04-26T09:20:42","slug":"java-reading-a-pdf-file-from-url-into-byte-arraybytebuffer-in-an-applet","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=725","title":{"rendered":"Java: Reading a pdf file from URL into Byte array\/ByteBuffer in an applet."},"content":{"rendered":"<p>I&#8217;m trying to figure out why this particular snippet of code isn&#8217;t working for me. I&#8217;ve got an applet which is supposed to read a .pdf and display it with a pdf-renderer library, but for some reason when I read in the .pdf files which sit on my server, they end up as being corrupt. I&#8217;ve tested it by writing the files back out again.<\/p>\n<p>I&#8217;ve tried viewing the applet in both IE and Firefox and the corrupt files occur. Funny thing is, when I trying viewing the applet in Safari (for Windows), the file is actually fine! I understand the JVM might be different, but I am still lost. I&#8217;ve compiled in Java 1.5. JVMs are 1.6. The snippet which reads the file is below.<\/p>\n<pre><code>public static ByteBuffer getAsByteArray(URL url) throws IOException {\r\n\u00a0 \u00a0 \u00a0 \u00a0 ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 URLConnection connection = url.openConnection();\r\n\u00a0 \u00a0 \u00a0 \u00a0 int contentLength = connection.getContentLength();\r\n\u00a0 \u00a0 \u00a0 \u00a0 InputStream in = url.openStream();\r\n\u00a0 \u00a0 \u00a0 \u00a0 byte[] buf = new byte[512];\r\n\u00a0 \u00a0 \u00a0 \u00a0 int len;\r\n\u00a0 \u00a0 \u00a0 \u00a0 while (true) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 len = in.read(buf);\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if (len == -1) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 break;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 tmpOut.write(buf, 0, len);\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 tmpOut.close();\r\n\u00a0 \u00a0 \u00a0 \u00a0 ByteBuffer bb = ByteBuffer.wrap(tmpOut.toByteArray(), 0,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 tmpOut.size());\r\n\u00a0 \u00a0 \u00a0 \u00a0 \/\/Lines below used to test if file is corrupt\r\n\u00a0 \u00a0 \u00a0 \u00a0 \/\/FileOutputStream fos = new FileOutputStream(\"C:\\\\abc.pdf\");\r\n\u00a0 \u00a0 \u00a0 \u00a0 \/\/fos.write(tmpOut.toByteArray());\r\n\u00a0 \u00a0 \u00a0 \u00a0 return bb;\r\n}\r\n<\/code><\/pre>\n<p>I must be missing something, and I&#8217;ve been banging my head trying to figure it out. Any help is greatly appreciated. Thanks.<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><strong>Edit:<\/strong> To further clarify my situation, the difference in the file before I read then with the snippet and after, is that the ones I output after reading are significantly smaller than they originally are. When opening them, they are not recognized as .pdf files. There are no exceptions being thrown that I ignore, and I have tried flushing to no avail.<\/p>\n<p>This snippet works in Safari, meaning the files are read in it&#8217;s entirety, with no difference in size, and can be opened with any .pdf reader. In IE and Firefox, the files always end up being corrupted, consistently the same smaller size.<\/p>\n<p>I monitored the len variable (when reading a 59kb file), hoping to see how many bytes get read in at each loop. In IE and Firefox, at 18kb, the in.read(buf) returns a -1 as if the file has ended. Safari does not do this.<\/p>\n<p>I&#8217;ll keep at it, and I appreciate all the suggestions so far.<\/p>\n<p>&nbsp;<\/p>\n<p>come from :\u00a0<a href=\"http:\/\/stackoverflow.com\/questions\/637100\/java-reading-a-pdf-file-from-url-into-byte-array-bytebuffer-in-an-applet\">http:\/\/stackoverflow.com\/questions\/637100\/java-reading-a-pdf-file-from-url-into-byte-array-bytebuffer-in-an-applet<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to figure out why this particular snippet of code isn&#8217;t working for me. I&#8217;ve got an applet which is supposed to read a .pdf and display it with a pdf-renderer library, but for some reason when I read in the .pdf files which sit on my server, they end up as being corrupt. &hellip; <a href=\"https:\/\/www.strongd.net\/?p=725\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java: Reading a pdf file from URL into Byte array\/ByteBuffer in an applet.<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-725","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=725"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/725\/revisions"}],"predecessor-version":[{"id":726,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/725\/revisions\/726"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}