views:

525

answers:

6

I have a bin file that I need to convert to a byte array. Can anyone tell me how to do this?

Here is what I have so far:

File f = new File("notification.bin");
is = new FileInputStream(f);

long length = f.length();

/*if (length > Integer.MAX_VALUE) {
    // File is too large
}*/

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
    offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
    throw new IOException("Could not completely read file "+f.getName());
}

But it's not working...

Kaddy

+1  A: 

You're probably better off using a memory mapped file. See this question

Jherico
+1  A: 

try using this

public byte[] readFromStream(InputStream inputStream) throws Exception
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    byte[] data = new byte[4096];
    int count = inputStream.read(data);
    while(count != -1)
    {
        dos.write(data, 0, count);
        count = inputStream.read(data);
    }

    return baos.toByteArray();
}

Btw, do you want a Java code or C++ code. Seeing the code in your question, I assumed it to be a java code and hence gave a java answer to it

Ram
What's that `DataOutputStream` about?
Tom Hawtin - tackline
+1 That's a nice catch Tom, ByteArrayOutputStream would suffice in this case, where we just read bytes and write bytes. If we were to write primitive types in addition DataOutputStream might be needed
Ram
A: 

Unless you really need to do it just that way, maybe simplify what you're doing.

Doing everything in the for loop may seem like a very slick way of doing it, but it's shooting yourself in the foot when you need to debug and don't immediately see the solution.

Dean J
A: 

In Java, a simple solution is:

InputStream is = ...
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] data = new byte[4096];  // A larger buffer size would probably help
int count; 
while ((count = is.read(data)) != -1) {
    os.write(data, 0, count);
}
byte[] result = os.toByteArray();

If the input is a file, we can preallocate a byte array of the right size:

File f = ...
long fileSize = f.length();
if (fileSize > Integer.MAX_VALUE) {
    // file too big
}
InputStream is = new FileInputStream(f);
byte[] data = new byte[fileSize];
if (is.read(data)) != data.length) {
    // file truncated while we were reading it???
}

However, there is probably a more efficient way to do this task using NIO.

Stephen C
A: 

In this answer I read from an URL

You could modify it so the InputStream is from a File instead of a URLConnection.

Something like:

    FileInputStream inputStream = new FileInputStream("your.binary.file");

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte [] buffer               = new byte[ 1024 ];

    int n = 0;
    while (-1 != (n = inputStream.read(buffer))) {
       output.write(buffer, 0, n);
    }
    inputStream.close();

etc

OscarRyz
A: 

Try open source library apache commons-io IOUtils.toByteArray(inputStream) You are not the first and not the last developer who needs to read a file, no need to reinvent it each time.

Pavel Feldman