Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
- * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- */
- import sun.reflect.generics.reflectiveObjects.NotImplementedException;
- import java.io.*;
- import java.nio.CharBuffer;
- import java.text.ParseException;
- import java.util.InputMismatchException;
- import java.util.Objects;
- public final class Scanner {
- // Internal buffer used to hold input
- private CharBuffer buf;
- // Size of internal character buffer
- // 100M chars, or ~200MB of memory allocated by CharBuffer buf
- private static final int BUFFER_SIZE = 100 * 1024 * 1024;
- // The index into the buffer currently held by the Scanner
- private int position;
- // The input source
- private Reader source;
- private String lineSeparator;
- private Boolean isAtEndOfReader = false;
- private Boolean firstMethodCall = false;
- private Scanner(Reader source) {
- assert source != null : "source should not be null";
- this.source = source;
- position = BUFFER_SIZE; // To read buffer at first method call
- isAtEndOfReader = false;
- lineSeparator = System.lineSeparator();
- }
- public Scanner(InputStream source) {
- this(new InputStreamReader(source));
- }
- public Scanner(String source) {
- this(new StringReader(source));
- }
- public boolean hasNextLine() throws IOException {
- if (firstMethodCall)
- updateBuffer();
- throw new NotImplementedException();
- }
- public String nextLine() throws IOException {
- if (firstMethodCall)
- updateBuffer();
- throw new NotImplementedException();
- }
- public boolean hasNextInt() throws IOException {
- if (firstMethodCall)
- updateBuffer();
- if (isAtEndOfReader)
- return false;
- char nextChar = buf.charAt(position);
- if (nextChar >= '0' && nextChar <= '9') {
- return true;
- }
- return false;
- }
- public int nextInt() throws ParseException, IOException {
- if (firstMethodCall)
- updateBuffer();
- StringBuilder intStringBuilder = new StringBuilder();
- while (true) {
- char nextChar = buf.charAt(position);
- if (isAtEndOfReader)
- {
- String intString = intStringBuilder.toString();
- if (Objects.equals(intString, ""))
- throw new InputMismatchException("Unexpected end of input");
- return Integer.parseInt(intString);
- }
- if (nextChar == ' ') {
- String intString = intStringBuilder.toString();
- return Integer.parseInt(intString);
- }
- if (nextChar < '0' || nextChar > '9') {
- throw new InputMismatchException("Unsupported char");
- }
- intStringBuilder.append(nextChar);
- position++;
- if (position == BUFFER_SIZE) {
- updateBuffer();
- }
- }
- }
- private void updateBuffer() throws IOException {
- int charsRead = source.read(buf);
- if (charsRead == 0) {
- isAtEndOfReader = true;
- }
- position = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement