Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Taking the Contents of a File object and converting it to a byte array

Reply
Thread Tools

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

 
 
MattC
Guest
Posts: n/a
 
      12-30-2005
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();

 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      12-30-2005
MattC wrote:
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-30-2005
On 30 Dec 2005 08:33:14 -0800, "MattC" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

> int length = new Long(templateFile.length()).intValue();


this is a bit roundabout. Try
int length = (int) templateFile.length();
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-30-2005
On 30 Dec 2005 08:33:14 -0800, "MattC" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

> /* 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();


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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      12-30-2005
Roedy Green wrote:
> On 30 Dec 2005 08:33:14 -0800, "MattC" <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>
>> /* 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting int and short to byte array and vice versa carmen Java 4 01-12-2010 05:00 PM
How to extract x amount of bytes from a byte object and store into another byte obj. DaBeef Java 1 07-21-2006 05:20 PM
converting a int array to a byte array cryptogirl Java 5 02-25-2006 03:38 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments