java - Writing a parser for a binary message format -


i need develop parser binary message exchange format i.e., message parser parses binary message java object representation. ask useful patterns used implement parser in flexible way. describe in nutshell or provide resources read?

since youre trying read binary data , transform java object, there many approaches, first thing first, must know structure/protocol of binary.

the pattern show bellow style (if you) use scenario.

make sure have input stream stream out binary data. if have byte array, make bytearrayinputstream.

in objects graph, each node/object should implement parsein(inputstream s) method.

public class parent extends arraylist<child> {     int age;     // ... more code here     public void parsein(inputstream is) throws ioexception {         // .. logic read stream instance.         datainputstream dis = new datainputstream(is);         this.age = dis.readint();          // .. if necessary         child c = new child();         c.parsein(inputstream is);         this.add(c);     }     // ... more code here }  public class child {     int height;     short weight;     date birthdate;     public void parsein(inputstream is) throws ioexception {         // .. logic read stream instance.         datainputstream dis = new datainputstream(is);         height = dis.readint();         weight = dis.readshort();         birthdate = new date(dis.readlong());     } } 

so, when obtain stream, simply

inputstream stream = this.getinputstream();  parent p = new parent(); parent.parsein(stream); 

and on , forth.

some times, need read underlying stream hint need read forward. example when reading string data in binary stream. either keep reading byte-by-byte until find terminator byte (as of c's style 0 termination character). or provide string length on first byte , read byte array of length.

i hope idea. , hope helps.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -