Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import sun.reflect.generics.reflectiveObjects.NotImplementedException;
  2.  
  3. import java.io.*;
  4. import java.nio.CharBuffer;
  5. import java.text.ParseException;
  6. import java.util.InputMismatchException;
  7. import java.util.Objects;
  8.  
  9. public final class Scanner {
  10.  
  11. // Internal buffer used to hold input
  12. private CharBuffer buf1;
  13. private CharBuffer buf2;
  14.  
  15. // Size of internal character buffer
  16. // 50M chars, or ~100MB of memory allocated by CharBuffer buf
  17. private static final int BUFFER_SIZE = 50 * 1024 * 1024;
  18.  
  19. // The index into the buffer currently held by the Scanner
  20. private int position;
  21.  
  22. // The input source
  23. private Reader source;
  24. private String lineSeparator;
  25.  
  26. private Boolean isAtEndOfReader = false;
  27. private Boolean firstMethodCall = false;
  28.  
  29. private Scanner(Reader source) {
  30. assert source != null : "source should not be null";
  31. this.source = source;
  32. buf1 = CharBuffer.allocate(BUFFER_SIZE);
  33. buf2 = CharBuffer.allocate(BUFFER_SIZE);
  34. firstMethodCall = true;
  35. isAtEndOfReader = false;
  36. lineSeparator = System.lineSeparator();
  37. }
  38.  
  39. public Scanner(InputStream source) {
  40. this(new InputStreamReader(source));
  41. }
  42. public Scanner(String source) {
  43. this(new StringReader(source));
  44. }
  45.  
  46. public boolean hasNextLine() throws IOException {
  47. if (firstMethodCall)
  48. updateBuffers();
  49. if (isAtEndOfReader)
  50. return false;
  51. throw new NotImplementedException();
  52. }
  53.  
  54. public String nextLine() throws IOException {
  55. if (firstMethodCall)
  56. updateBuffers();
  57. if (isAtEndOfReader)
  58. throw new InputMismatchException("Unexpected end of input");
  59. throw new NotImplementedException();
  60. }
  61.  
  62. public boolean hasNextInt() throws IOException {
  63. if (firstMethodCall)
  64. updateBuffers();
  65. if (isAtEndOfReader)
  66. return false;
  67. char nextChar = buf.charAt(position);
  68. if (nextChar >= '0' && nextChar <= '9') {
  69. return true;
  70. }
  71. return false;
  72. }
  73.  
  74. public int nextInt() throws ParseException, IOException {
  75. if (firstMethodCall)
  76. updateBuffers();
  77. if (isAtEndOfReader)
  78. throw new InputMismatchException("Unexpected end of input");
  79. StringBuilder intStringBuilder = new StringBuilder();
  80. while (true) {
  81. char nextChar = buf.charAt(position);
  82.  
  83. if (isAtEndOfReader)
  84. {
  85. String intString = intStringBuilder.toString();
  86. if (Objects.equals(intString, ""))
  87. throw new InputMismatchException("Unexpected end of input");
  88. return Integer.parseInt(intString);
  89. }
  90. if (nextChar == ' ') {
  91. String intString = intStringBuilder.toString();
  92. return Integer.parseInt(intString);
  93. }
  94.  
  95. if (nextChar < '0' || nextChar > '9') {
  96. throw new InputMismatchException("Unsupported char");
  97. }
  98.  
  99. intStringBuilder.append(nextChar);
  100.  
  101. position++;
  102.  
  103. if (position == BUFFER_SIZE) {
  104. updateBuffers();
  105. }
  106. }
  107. }
  108.  
  109. private void updateBuffers() throws IOException {
  110. int charsRead = source.read(buf1);
  111. if (charsRead == 0) {
  112. isAtEndOfReader = true;
  113. }
  114.  
  115. position = 0;
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement