Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chessReader;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.Arrays;
- public class ChunkIO {
- public Game loadGame(Reader input) throws CorruptedChessFileException {
- /**
- * This is the game object this method will fill with data. The object
- * is returned when the END chunk is reached.
- */
- Game game = new Game();
- char[] header = new char[8];
- char[] date = new char[8];
- char[] chunkHeader = new char[5];
- try {
- // Read the file header and the save date
- input.read(header);
- input.read(date);
- input.read(chunkHeader);
- input.read(chunkHeader);
- readFully(header, input);
- readFully(date, input);
- int chunkSize = ChunkIO.extractChunkSize(chunkHeader);
- char[] data = new char[chunkSize];
- input.read(data, 21, chunkSize + 21);
- String playerName;
- for (int i = 2; i <= (int) data[1]; i++) {
- char current = data[i];
- playerName = playerName + current;
- }
- if (chunkHeader[0] == 'P') {
- if (data[0] == 'M') {
- Player player = new Player(playerName, BLACK);
- }
- }
- // Process the data we just read.
- // NOTE: To test the line below you must test the class once with a
- // broken header
- if (!new String(header).startsWith("SHAKKI")) {
- throw new CorruptedChessFileException("Unknown file type");
- }
- // The version information and the date are not used in this
- // exercise
- // *************************************************************
- //
- // EXERCISE
- //
- // ADD CODE HERE FOR READING THE
- // DATA FOLLOWING THE MAIN HEADERS
- //
- //
- // *************************************************************
- // If we reach this point the Game-object should now have the proper
- // players and
- // a fully set up chess board. Therefore we might as well return it.
- return game;
- } catch (IOException e) {
- // To test this part the stream would have to cause an
- // IOException. That's a bit complicated to test. Therefore we have
- // given you a "secret tool", class BrokenReader, which will throw
- // an IOException at a requested position in the stream.
- // Throw the exception inside any chunk, but not in the chunk
- // header.
- CorruptedChessFileException chessExc = new CorruptedChessFileException(
- "Reading the chess data failed.");
- // Append the information about the initial cause for use in
- // debugging. Otherwise the programmer cannot know the method or
- // line number causing the problem.
- chessExc.initCause(e);
- throw chessExc;
- }
- }
- // HELPER METHODS -------------------------------------------------------
- /**
- * Given a chunk header (an array of 5 chars) will return the size of this
- * chunks data.
- *
- * @param chunkHeader
- * a chunk header to process
- * @return the size of this chunks data
- */
- private static int extractChunkSize(char[] chunkHeader) {
- // subtracting the ascii value of the character 0 from
- // a character containing a number will return the
- // number itself
- return 10 * (chunkHeader[3] - '0') + (chunkHeader[4] - '0');
- }
- /**
- * Given a chunk header (an array of 5 chars) will return the name of this
- * chunk as a 3-letter String.
- *
- * @param chunkHeader
- * a chunk header to process
- * @return the name of this chunk
- */
- private static String extractChunkName(char[] chunkHeader) {
- return new StringBuilder().append(chunkHeader[0])
- .append(chunkHeader[1]).append(chunkHeader[2]).toString();
- }
- /**
- * The read-method of the Reader class will occasionally read only part of
- * the characters that were requested. This method will repeatedly call read
- * to completely fill the given buffer. The size of the buffer tells the
- * algorithm how many bytes should be read.
- *
- * @param result
- * The result of the reading will be stored in this array.
- * @param input
- * The character stream to read from
- * @throws IOException
- * @throws CorruptedChessFileException
- */
- private static void readFully(char[] result, Reader input)
- throws IOException, CorruptedChessFileException {
- int cursor = 0;
- while (cursor != result.length) {
- int numCharactersRead = input.read(result, cursor, result.length
- - cursor);
- // If the file end is reached before the buffer is filled
- // an exception is thrown.
- if (numCharactersRead == -1) {
- throw new CorruptedChessFileException("Unexpected end of file.");
- }
- cursor += numCharactersRead;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement