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

Discussion in 'Java' started by MattC, Dec 30, 2005.

  1. MattC

    MattC Guest

    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();
    MattC, Dec 30, 2005
    #1
    1. Advertising

  2. Re: Taking the Contents of a File object and converting it to a bytearray

    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/
    Thomas Hawtin, Dec 30, 2005
    #2
    1. Advertising

  3. MattC

    Roedy Green Guest

    On 30 Dec 2005 08:33:14 -0800, "MattC" <>
    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.
    Roedy Green, Dec 30, 2005
    #3
  4. MattC

    Roedy Green Guest

    On 30 Dec 2005 08:33:14 -0800, "MattC" <>
    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.
    Roedy Green, Dec 30, 2005
    #4
  5. Re: Taking the Contents of a File object and converting it to a bytearray

    Roedy Green wrote:
    > On 30 Dec 2005 08:33:14 -0800, "MattC" <>
    > 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/
    Thomas Hawtin, Dec 30, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kirby
    Replies:
    3
    Views:
    637
    Kirby
    Oct 8, 2004
  2. carmen
    Replies:
    4
    Views:
    30,702
    ersecchio
    Jan 12, 2010
  3. MattC
    Replies:
    2
    Views:
    9,120
    steve
    Oct 24, 2005
  4. cryptogirl
    Replies:
    5
    Views:
    24,833
    cryptogirl
    Feb 25, 2006
  5. Jim Cain
    Replies:
    1
    Views:
    205
    Yukihiro Matsumoto
    Jul 18, 2003
Loading...

Share This Page