• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Binary file read/write Question

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the following code to read in a PDF file and writing the contents out to a different file. I am expecting that I should be able to
read the second file using the Acrobat reader. I can but the contents are blank! The original file is a proper PDF with text and images.Am I missing something obvious?
Everything is on the same system(same charset etc.)
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Converting the (binary) bytes to String data and back to bytes again assumes that the bytes represent text. If they don't, then changes may occur that damage the document (as in this case).

So don't use String or StringBuffer. Just read the bytes from one file and write them to the other file.

Personally I wouldn't use RandomAccessFiles for this task. I would use a BufferedInputStream to read into a buffer of something like 2048 bytes and a BufferedOutputStream to write that buffer into. Keep doing that (in a loop) until you have copied the entire file.
 
santosh kulkarni
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the prompt reply Paul. your comments make sense.
Allow me to elaborate though.
I simplified the code before posting.
What I want to do is Base64 encode the pdf, send it over a web service and
decode and be able to view/print it on the other side.
I was able to do this when I started from a already encoded pdf as a
(java.lang.)String and got the bytes and wrote a file and poof it came up.
I'll keep trying, may be store it in arraylist or something
S
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If you implement the steps you mentioned as they should, I feel that solution would work smooth. The important thing to keep in mind is that to read the input pdf and write the output pdf in binary format (you should use streams instead of Reader or Writers). I tried it out with sample pdf (around 61kb which had some images too) and the below code worked fine in creating a duplicate pdf.

 
santosh kulkarni
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Santhosh, I was trying to avoid pre-determined buffer size. I realized, in my case and most of the cases 512K(or some other simlar number) ought to be more than enough.
Santosh
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic