1

I'm looking for a way that I can read the binary data of a file into a string. I've found one that reads the bytes directly and converts the bytes to binary, the only problem is that it takes up a significant amount of RAM.

Here's the code I'm currently using

try {
    byte[] fileData = new byte[(int) sellect.length()];
    FileInputStream in = new FileInputStream(sellect);
    in.read(fileData);
    in.close();
    getBinary(fileData[0]);
    getBinary(fileData[1]);
    getBinary(fileData[2]);
} catch (IOException e) {
    e.printStackTrace();
}

And the getBinary() method

public String getBinary(byte bite) {
    String output = String.format("%8s", Integer.toBinaryString(bite & 0xFF)).replace(' ', '0');
    System.out.println(output); // 10000001
    return output;
}
Butterscotch
  • 100
  • 11
  • If the whole "binified" file won't fit in memory, then perhaps the solution is only to work on a portion of the file at a time? – John Hascall Dec 31 '15 at 22:59
  • So are you just converting the 1st 3 bytes of the file? Or is the above just an example? How big do you expect the files to be? As John pointed out, you can read the file in chunks, and increment through a large file that way. – philbrooksjazz Dec 31 '15 at 23:01
  • @John Hascall Makes sense, though I have no idea how you could grab out byte by byte through a file, as maybe it would be great to have some sort of "for" loop running taking in maybe 10 bytes at a time, processing it, then outputting it to a safe place, clearing the memory for that, then repeating as many times as it needs. – Butterscotch Dec 31 '15 at 23:03
  • @philbrooksjazz Sorry for not being clear, that is merely a test to see if it would work, yes, I am planning on doing it to the entire file. – Butterscotch Dec 31 '15 at 23:05
  • @Dankrushen, the `read()` method only reads as many bytes as your byte array is big. So you could do: `byte[] fileData = new byte[1024];` and each `read()` will just read the next 1024 bytes. (You can use whatever size seems appropriate). – John Hascall Dec 31 '15 at 23:17
  • @JohnHascall Sounds like what I need, although, how would I go about reading again, but at 1024 bytes in, or 2048, e.t.c bytes in? – Butterscotch Dec 31 '15 at 23:20
  • You would use a loop like in @Tony Ruth's answer. Java will keep track of the position in the file for you -- each read will pick up where the last one left off. – John Hascall Dec 31 '15 at 23:23
  • @JohnHascall Alright, I understand it entirely now, the "read" method has three possible arguments: Where to put the data, where to start reading the data, and how many bytes to read. Using method will get the bytes convert them, and add how many lines read to the offset. Thanks for your help! – Butterscotch Dec 31 '15 at 23:30

2 Answers2

3

Can you do something like this:

int buffersize = 1000;
int offset = 0;
byte[] fileData = new byte[buffersize];

int numBytesRead;
String string;
while((numBytesRead = in.read(fileData,offset,buffersize)) != -1)
{
 string = getBinary(fileData);//Adjust this so it can work with a whole array of bytes at once
 out.write(string); 
 offset += numBytesRead;
 }

This way, you never store more information in the ram than the byte and string structures. The file is read 1000 bytes at a time, translated to a string 1 byte at a time, and then put into a new file as a string. Using read() returns the value of how many bytes it reads.

Tony Ruth
  • 1,358
  • 8
  • 19
0

This link can help you :

File to byte[] in Java

public static byte[] toByteArray(InputStream input) throws IOException

Gets the contents of an InputStream as a byte[]. This method buffers the input internally, so there is no need to use a BufferedInputStream.

Parameters: input - the InputStream to read from Returns: the requested byte array Throws: NullPointerException - if the input is null IOException - if an I/O error occurs

Community
  • 1
  • 1
Fred
  • 3,365
  • 4
  • 36
  • 57