I am reading in a file that contains both ascii and binary data into a
byte array using a BufferedInputStream. What I need to do is iterate
through that byte array and pull out information based on file
separators that exist in the read in file. Looking for something like
this...
byte [] fileasbytes;
int location=0;
while fileasbytes != seperator
datatypeneeded chunk = read in from location to location of
seperator
And will repeat this through the entire file. Most data will be int/
text with some binary data.
Any help on how to accomplish this would be greatly appreciated.
Your pseudocode above is right on the money. There are a few choices in
the way to do this: mainly, whether you want to read the whole file into
an array and then work over the array, or whether you want to pull it from
a stream bit by bit. Then there's a choice of whether you want to build
the chunk up byte by byte, or whether you want to make it all in one go.
Building it byte by byte fits well with reading from a stream; making it
in one go fits well with reading from an array. The array approach will be
faster with small files, the stream approach with big files. I think the
stream approach might actually be easier to code, since you don't have to
manage array indices yourself.
Here's an implementation that does it with an array:
import java.io.RandomAccessFile ;
import java.io.IOException ;
public class Steve {
private static final byte SEPARATOR = (byte)'\n' ; // change this
public static void main(String[] args) throws IOException {
String filename = args[0] ;
byte[] fileData = readFile(filename) ;
processFile(fileData, SEPARATOR) ;
}
private static byte[] readFile(String filename) throws IOException {
RandomAccessFile file = new RandomAccessFile(filename, "r") ;
long length = file.length() ;
if (length > Integer.MAX_VALUE) throw new IOException("file too big!") ;
byte[] fileData = new byte[(int)length] ;
file.readFully(fileData) ;
return fileData ;
}
private static void processFile(byte[] fileData, byte separator) {
int start = 0 ;
for (int end = 0; end < fileData.length; ++end) {
if (fileData[end] == separator) {
extractAndProcessChunk(fileData, start, end) ;
start = end + 1 ;
}
}
extractAndProcessChunk(fileData, start, fileData.length) ;
}
private static void extractAndProcessChunk(byte[] fileData, int start, int end) {
if (start == end) return ; // ignore empty chunks
int chunkLength = end - start ;
byte[] chunk = new byte[chunkLength] ;
System.arraycopy(fileData, start, chunk, 0, chunkLength) ;
processChunk(chunk) ;
}
private static void processChunk(byte[] chunk) {
System.err.println("CHUNK " + new String(chunk)) ; // change this
}
}
tom
--
But in natural sciences whose conclusions are true and necessary and
have nothing to do with human will, one must take care not to place
oneself in the defence of error; for here a thousand Demostheneses and
a thousand Aristotles would be left in the lurch by every mediocre wit
who happened to hit upon the truth for himself. -- Galileo