Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package chessReader;
  2.  
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import java.util.Arrays;
  6.  
  7. public class ChunkIO {
  8.  
  9. public Game loadGame(Reader input) throws CorruptedChessFileException {
  10.  
  11. /**
  12. * This is the game object this method will fill with data. The object
  13. * is returned when the END chunk is reached.
  14. */
  15.  
  16. Game game = new Game();
  17.  
  18. char[] header = new char[8];
  19. char[] date = new char[8];
  20. char[] chunkHeader = new char[5];
  21. try {
  22.  
  23. // Read the file header and the save date
  24. input.read(header);
  25. input.read(date);
  26. input.read(chunkHeader);
  27. input.read(chunkHeader);
  28. readFully(header, input);
  29. readFully(date, input);
  30. int chunkSize = ChunkIO.extractChunkSize(chunkHeader);
  31. char[] data = new char[chunkSize];
  32. input.read(data, 21, chunkSize + 21);
  33. String playerName;
  34. for (int i = 2; i <= (int) data[1]; i++) {
  35. char current = data[i];
  36. playerName = playerName + current;
  37. }
  38.  
  39. if (chunkHeader[0] == 'P') {
  40. if (data[0] == 'M') {
  41. Player player = new Player(playerName, BLACK);
  42. }
  43. }
  44.  
  45. // Process the data we just read.
  46. // NOTE: To test the line below you must test the class once with a
  47. // broken header
  48.  
  49. if (!new String(header).startsWith("SHAKKI")) {
  50. throw new CorruptedChessFileException("Unknown file type");
  51. }
  52.  
  53. // The version information and the date are not used in this
  54. // exercise
  55.  
  56. // *************************************************************
  57. //
  58. // EXERCISE
  59. //
  60. // ADD CODE HERE FOR READING THE
  61. // DATA FOLLOWING THE MAIN HEADERS
  62. //
  63. //
  64. // *************************************************************
  65.  
  66. // If we reach this point the Game-object should now have the proper
  67. // players and
  68. // a fully set up chess board. Therefore we might as well return it.
  69.  
  70. return game;
  71.  
  72. } catch (IOException e) {
  73.  
  74. // To test this part the stream would have to cause an
  75. // IOException. That's a bit complicated to test. Therefore we have
  76. // given you a "secret tool", class BrokenReader, which will throw
  77. // an IOException at a requested position in the stream.
  78. // Throw the exception inside any chunk, but not in the chunk
  79. // header.
  80.  
  81. CorruptedChessFileException chessExc = new CorruptedChessFileException(
  82. "Reading the chess data failed.");
  83.  
  84. // Append the information about the initial cause for use in
  85. // debugging. Otherwise the programmer cannot know the method or
  86. // line number causing the problem.
  87.  
  88. chessExc.initCause(e);
  89.  
  90. throw chessExc;
  91. }
  92. }
  93.  
  94. // HELPER METHODS -------------------------------------------------------
  95.  
  96. /**
  97. * Given a chunk header (an array of 5 chars) will return the size of this
  98. * chunks data.
  99. *
  100. * @param chunkHeader
  101. * a chunk header to process
  102. * @return the size of this chunks data
  103. */
  104.  
  105. private static int extractChunkSize(char[] chunkHeader) {
  106.  
  107. // subtracting the ascii value of the character 0 from
  108. // a character containing a number will return the
  109. // number itself
  110.  
  111. return 10 * (chunkHeader[3] - '0') + (chunkHeader[4] - '0');
  112.  
  113. }
  114.  
  115. /**
  116. * Given a chunk header (an array of 5 chars) will return the name of this
  117. * chunk as a 3-letter String.
  118. *
  119. * @param chunkHeader
  120. * a chunk header to process
  121. * @return the name of this chunk
  122. */
  123. private static String extractChunkName(char[] chunkHeader) {
  124. return new StringBuilder().append(chunkHeader[0])
  125. .append(chunkHeader[1]).append(chunkHeader[2]).toString();
  126. }
  127.  
  128. /**
  129. * The read-method of the Reader class will occasionally read only part of
  130. * the characters that were requested. This method will repeatedly call read
  131. * to completely fill the given buffer. The size of the buffer tells the
  132. * algorithm how many bytes should be read.
  133. *
  134. * @param result
  135. * The result of the reading will be stored in this array.
  136. * @param input
  137. * The character stream to read from
  138. * @throws IOException
  139. * @throws CorruptedChessFileException
  140. */
  141. private static void readFully(char[] result, Reader input)
  142. throws IOException, CorruptedChessFileException {
  143. int cursor = 0;
  144.  
  145. while (cursor != result.length) {
  146. int numCharactersRead = input.read(result, cursor, result.length
  147. - cursor);
  148.  
  149. // If the file end is reached before the buffer is filled
  150. // an exception is thrown.
  151.  
  152. if (numCharactersRead == -1) {
  153. throw new CorruptedChessFileException("Unexpected end of file.");
  154. }
  155.  
  156. cursor += numCharactersRead;
  157. }
  158.  
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement