binary - Read from byte array to console (or file) Java -
so have 2 files, binary file have read byte array, , text file have read arraylist of strings. lets assume arraylist has values "char", "int", "double" , serves schema reading binary file.
that means first 2 bytes in byte array want interpret char, next 4 int, , next 8 double, , repeat until file over.
i've implemented reading in data, can't figure out way interpret binary data according schema file. there way this?
ie (psuedocode) using printstream out = new printstream (new outputstream(bytearray)) out.writechar() out.writeint() out.writedouble()
where stream works through bytearray me? (as opposed me saying out.writechar(bytearray[2])?
i have figurd out logic of mapping schema proper action, having trouble converting data. thoughts?
i want write information either console (or file).
think object-oriented! have create class each desired data type, capable of parsing data in input stream.
steps (without caring exceptions):
1) create interface these classes:
interface dataparser { void parse(datainputstream in, printstream out); }
2) create class each data type implementing interface. example:
class intparser implements dataparser { public void parse(datainputstream in, printstream out) { int value = in.readint(); out.print(value); } }
3) use hashmap registering known parsers string id:
map<string, dataparser> parsers = new hashmap<>(); parsers.put("int", new intparser());
4) can use this:
datainputstream in = ... ; printstream out = ... ; ... while(hasdata()) { string type = getnexttype(); dataparser parser = parsers.get(type); parser.parse(in, out); } // close streams , work on output here
the method hasdata
should determine, whether there still data in input stream. method getnexttype
should return next type string arraylist.
Comments
Post a Comment