Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Taking the Contents of a File object and converting it to a byte array

Thread view: 
MattC - 30 Dec 2005 16:33 GMT
I have a file that I need to read into my program and convert to a byte
array (byte [] ). The code below works but it seems sort of "smelly".
Can someone suggest a cleaner, more eloquent,  way of accomplishing
this?

Thanks!

****************

       /* Move the data in the File object to a byte array */
       InputStream in = new FileInputStream(templateFile);
       int length = new Long(templateFile.length()).intValue();
       ByteArrayOutputStream out = new ByteArrayOutputStream(length);

       byte[] buf = new byte[1024];
       int len;

       while ((len = in.read(buf)) > 0) {
           out.write(buf, 0, len);
       }

       byte[] certBytes = new byte[out.size()];
       certBytes = out.toByteArray();
Thomas Hawtin - 30 Dec 2005 17:16 GMT
> I have a file that I need to read into my program and convert to a byte
> array (byte [] ). The code below works but it seems sort of "smelly".
> Can someone suggest a cleaner, more eloquent,  way of accomplishing
> this?

What smells about it to you?

>         /* Move the data in the File object to a byte array */

You should ensure that you always close the stream.

>         InputStream in = new FileInputStream(templateFile);
          try {
>         int length = new Long(templateFile.length()).intValue();
>         ByteArrayOutputStream out = new ByteArrayOutputStream(length);
>
>         byte[] buf = new byte[1024];

A bit small in this day an age, I think. I go for 8192 out of habit.
Unlikely to make much difference.

>         int len;
>
>         while ((len = in.read(buf)) > 0) {

I prefer to avoid side effects in conditional expressions, and also
testing explicitly against -1. A value of 0 probably should not break
the loop.

>             out.write(buf, 0, len);
>         }
          } finally {
              in.close();
          }

The allocation is entirely pointless as the next line overwrites the array.

>         byte[] certBytes = new byte[out.size()];
>         certBytes = out.toByteArray();

You could read the length of the file first and allocate an exact sized
buffer, but the code above is safer and less tied to specifics.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 30 Dec 2005 18:02 GMT
>        int length = new Long(templateFile.length()).intValue();

this is a bit roundabout. Try
  int length = (int) templateFile.length();
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 30 Dec 2005 18:05 GMT
>     /* Move the data in the File object to a byte array */
>        InputStream in = new FileInputStream(templateFile);
[quoted text clipped - 10 lines]
>        byte[] certBytes = new byte[out.size()];
>        certBytes = out.toByteArray();

You can read the entire file of raw bytes in one i/o into your
certBytes. See http://mindprod.com/applets/fileio.html

You don't need a ByteArrayOutputStream or a byte[1024] buffer.

Even if you did need a buffer, use a BufferedInputStream rather than
rolling your own.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 30 Dec 2005 20:12 GMT
>>    /* Move the data in the File object to a byte array */
>>       InputStream in = new FileInputStream(templateFile);
>>       int length = new Long(templateFile.length()).intValue();

(I didn't see that...)

> You don't need a ByteArrayOutputStream or a byte[1024] buffer.
>
> Even if you did need a buffer, use a BufferedInputStream rather than
> rolling your own.

You don't mean reading a byte at a time, do you? If nothing else, the
synchronisation would kill performance, unless the JVM was something
special.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.