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.38 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  3. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. *
  20. *
  21. *
  22. *
  23. *
  24. */
  25.  
  26. import sun.reflect.generics.reflectiveObjects.NotImplementedException;
  27.  
  28. import java.io.*;
  29. import java.nio.CharBuffer;
  30. import java.text.ParseException;
  31. import java.util.InputMismatchException;
  32. import java.util.Objects;
  33.  
  34. public final class Scanner {
  35.  
  36. // Internal buffer used to hold input
  37. private CharBuffer buf;
  38.  
  39. // Size of internal character buffer
  40. // 100M chars, or ~200MB of memory allocated by CharBuffer buf
  41. private static final int BUFFER_SIZE = 100 * 1024 * 1024;
  42.  
  43. // The index into the buffer currently held by the Scanner
  44. private int position;
  45.  
  46. // The input source
  47. private Reader source;
  48. private String lineSeparator;
  49.  
  50. private Boolean isAtEndOfReader = false;
  51. private Boolean firstMethodCall = false;
  52.  
  53. private Scanner(Reader source) {
  54. assert source != null : "source should not be null";
  55. this.source = source;
  56. position = BUFFER_SIZE; // To read buffer at first method call
  57. isAtEndOfReader = false;
  58. lineSeparator = System.lineSeparator();
  59. }
  60.  
  61. public Scanner(InputStream source) {
  62. this(new InputStreamReader(source));
  63. }
  64. public Scanner(String source) {
  65. this(new StringReader(source));
  66. }
  67.  
  68. public boolean hasNextLine() throws IOException {
  69. if (firstMethodCall)
  70. updateBuffer();
  71. throw new NotImplementedException();
  72. }
  73.  
  74. public String nextLine() throws IOException {
  75. if (firstMethodCall)
  76. updateBuffer();
  77. throw new NotImplementedException();
  78. }
  79.  
  80. public boolean hasNextInt() throws IOException {
  81. if (firstMethodCall)
  82. updateBuffer();
  83. if (isAtEndOfReader)
  84. return false;
  85. char nextChar = buf.charAt(position);
  86. if (nextChar >= '0' && nextChar <= '9') {
  87. return true;
  88. }
  89. return false;
  90. }
  91.  
  92. public int nextInt() throws ParseException, IOException {
  93. if (firstMethodCall)
  94. updateBuffer();
  95. StringBuilder intStringBuilder = new StringBuilder();
  96. while (true) {
  97. char nextChar = buf.charAt(position);
  98.  
  99. if (isAtEndOfReader)
  100. {
  101. String intString = intStringBuilder.toString();
  102. if (Objects.equals(intString, ""))
  103. throw new InputMismatchException("Unexpected end of input");
  104. return Integer.parseInt(intString);
  105. }
  106. if (nextChar == ' ') {
  107. String intString = intStringBuilder.toString();
  108. return Integer.parseInt(intString);
  109. }
  110.  
  111. if (nextChar < '0' || nextChar > '9') {
  112. throw new InputMismatchException("Unsupported char");
  113. }
  114.  
  115. intStringBuilder.append(nextChar);
  116.  
  117. position++;
  118.  
  119. if (position == BUFFER_SIZE) {
  120. updateBuffer();
  121. }
  122. }
  123. }
  124.  
  125. private void updateBuffer() throws IOException {
  126. int charsRead = source.read(buf);
  127. if (charsRead == 0) {
  128. isAtEndOfReader = true;
  129. }
  130. position = 0;
  131. }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement