Think object-oriented! You have to create a class for each desired data type, which is capable of parsing the data in an input stream.
Steps are (without caring about exceptions):
1) Create an interface for these classes:
interface DataParser {
void parse(DataInputStream in, PrintStream out);
}
2) Create a class for each data type implementing that interface. Example:
class IntParser implements DataParser {
public void parse(DataInputStream in, PrintStream out) {
int value = in.readInt();
out.print(value);
}
}
3) Use a HashMap for registering all known parsers with their String ID:
Map<String, DataParser> parsers = new HashMap<>();
parsers.put("int", new IntParser());
4) Now you can use it like this:
DataInputStream in = ... ;
PrintStream out = ... ;
...
while(hasData()) {
String type = getNextType();
DataParser parser = parsers.get(type);
parser.parse(in, out);
}
// close streams and work on the output here
The method hasData
should determine, whether there is still data in the input stream. The method getNextType
should return the next type string from your ArrayList.