Some guidelines for this week's studio:
We will first write a tool that reads a file and dumps (prints out) its contents as it is actually represented 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.
- What is the numeric value of a lower-case a?
- What is the numeric value of the character(s) that separate lines?
- What is the numeric value of the character(s) in front of the word fun? What does that value mean?
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 was included in your repository.It will also run your Dumpster on the file to show you the output, which is what the persistent file really contains.
You should see the single byte with value 32.
Next let's change some things and explore.
Review the two's complement representation if needed.
Consider how Java I/O libraries use the decorator pattern.
- Show the TA answers to the quetions posed above.
- Be sure you understand what you have seen.
- Ask the TA to explain anything you do not understand.
When you done with this studio, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so