1

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

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
Kaddy
  • 1,166
  • 7
  • 19
  • 34

6 Answers6

2

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

Prabhu R
  • 13,218
  • 21
  • 76
  • 112
1

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

Community
  • 1
  • 1
Jherico
  • 27,377
  • 8
  • 60
  • 87
1

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
  • 669,072
  • 92
  • 771
  • 1,162
0

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
  • 37,552
  • 16
  • 64
  • 93
0

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

Community
  • 1
  • 1
OscarRyz
  • 190,799
  • 110
  • 376
  • 555
0

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
  • 4,471
  • 6
  • 29
  • 31