Some guidelines for this week's studio:
We will first write a tool that reads a file and dumps (prints out) its contents just as it appears in that file. We will use this tool to study how various data types are represented persistently.
At this point, eclipse should complain about some unhandled exceptions. Click on the yellow light bulb to see the possible fixes:
Discuss amongst yourselves the tradeoffs in the above approaches, and settle on what your group wants to do. There is no right answer here, but you must be prepared to defend your choice and explain its consequences.
- You can catch the exception here using try...catch.
- You can propagate the exception back to the caller by adding the throws .... clause to the constructor's declaration.
If you decided on the try...catch approach for the previous statement, then you can extend the scope of the try block to include this statement as well.
What are the responsibilities of the three objects you instantiated above? Explain how the Decorator pattern is used to build increasingly sophisticated input streams.
- Receive the result of readByte() as a byte variable.
- You must again figure out what to do with the exceptions that might get thrown from this action. Remember that the goal is just to dump the file, and that end of input will eventually happen.
For now, you can just print out the byte, but you may eventually want to print out the location of the byte in terms of how many bytes have been printed before this one.
You may also want to see the byte written in a different format as well as the integer format. For example, writing the byte in hex makes it easier to see the bit pattern, since each 4-bit group corresponds to one hexadecimal character.
Here is a method that converts an int into a String showing its hex representation. A numerical character means what it says. The character A corresponds to 10, B to 11, and so on.
You should arrange for an instance variable to keep track of whether the file has useful information remaining. The exceptions thrown by the code in dumpNextByte() could set this instance variable to true.
It is important at this point to understand why you see what you see. The test.txt file contains ASCII characters, encoded as shown in this table.
The test.bin file contains a consecutive stream of bytes with values from 0 to 100.
Don't try to modify the test.bin file: it has binary data in it that cannot easily be edited in eclipse.
We next experiment by writing out some primitive data types to persistent storage, and then using your Dumpster program to examine the results.
Figure out what the class does, getting help if needed.
This will write the file exper.bin, which may not show up in your Package view until you refresh the project (function key F5).It will also run the Dumpster on the file to show you the output.
You should see the single byte with value 32.
Now have some fun to answer the following questions. In each case, simply modify the run() method in Experiment to cause things to be written to the exper.bin file.
To write out a primitive type, find the appropriate method (writeByte, writeInt, writeBoolean, etc.) in DataOutputStream.
Don't change your Dumpster code -- let it continue to read the file as a byte stream, so you can see what is inside.
Review the two's complement representation if needed.
Consider how Java I/O libraries use the decorator pattern.