patternjavaMinor
Simple I/O class for reading input data fast
Viewed 0 times
fastreadingsimpledatainputforclass
Problem
I wrote a
How could I made this better? I am mainly looking for good code suggestions rather than the specific details of the logic. Though, any comments/recommendations are appreciated.
```
package com.muratdozen.playground.util.io;
import com.muratdozen.playground.util.threading.NonThreadSafe;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.StringTokenizer;
/**
* FastReader class helps to read input in the form of words
* from an {@link InputStream}. Good to use as a parser.
*
* Usage:
*
* Assuming an input stream with the following lines:
* asd xxx
* 123
* {@code
* final FastReader fastReader = FastReader.from(System.in);
* final String s1 = fastReader.next();
* final String s2 = fastReader.next();
* final int n = fastReader.nextInt();
* ...
* }
*
*
* @author Murat Derya Ozen
* @since: 9/28/13 1:50 PM
*/
@NonThreadSafe
public final class FastReader {
private final BufferedReader bufferedReader;
/ legacy class preferred over String#split and Scanner for performance /
private StringTokenizer tokenizer;
private FastReader(final BufferedReader bufferedReader) {
this.bufferedReader = bufferedReader;
this.tokenizer = null;
}
/**
* Returns a {@link FastReader} instance that reads input from {@code inputStream}.
*
* @param inputStream
* @return Returns a {@link FastReader} instance that reads input from {@code inputStream}.
*/
public static final FastReader from(final InputStream inputStream) {
return new FastReader(new BufferedReader(new InputStreamReader(inputStream)));
}
/**
* Returns the next word acquired by {@link StringTokenizer}.
FastReader utility class that is supposed to read input fast. It's mostly aimed at parsing input files in competitive programming.How could I made this better? I am mainly looking for good code suggestions rather than the specific details of the logic. Though, any comments/recommendations are appreciated.
```
package com.muratdozen.playground.util.io;
import com.muratdozen.playground.util.threading.NonThreadSafe;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.StringTokenizer;
/**
* FastReader class helps to read input in the form of words
* from an {@link InputStream}. Good to use as a parser.
*
* Usage:
*
* Assuming an input stream with the following lines:
* asd xxx
* 123
* {@code
* final FastReader fastReader = FastReader.from(System.in);
* final String s1 = fastReader.next();
* final String s2 = fastReader.next();
* final int n = fastReader.nextInt();
* ...
* }
*
*
* @author Murat Derya Ozen
* @since: 9/28/13 1:50 PM
*/
@NonThreadSafe
public final class FastReader {
private final BufferedReader bufferedReader;
/ legacy class preferred over String#split and Scanner for performance /
private StringTokenizer tokenizer;
private FastReader(final BufferedReader bufferedReader) {
this.bufferedReader = bufferedReader;
this.tokenizer = null;
}
/**
* Returns a {@link FastReader} instance that reads input from {@code inputStream}.
*
* @param inputStream
* @return Returns a {@link FastReader} instance that reads input from {@code inputStream}.
*/
public static final FastReader from(final InputStream inputStream) {
return new FastReader(new BufferedReader(new InputStreamReader(inputStream)));
}
/**
* Returns the next word acquired by {@link StringTokenizer}.
Solution
-
I'm always wary of naming my classes according to performance criteria. What if you (or someone else) finds a faster method of reading tokens from an input stream. Would you then write an
-
Consider letting the user pass the delimiters as well (optionally). This will make it more useful by allowing to parse input which is delimited by other things than white spaces.
I'm always wary of naming my classes according to performance criteria. What if you (or someone else) finds a faster method of reading tokens from an input stream. Would you then write an
EvenFasterReader? How about something like TokenReader?-
Consider letting the user pass the delimiters as well (optionally). This will make it more useful by allowing to parse input which is delimited by other things than white spaces.
Context
StackExchange Code Review Q#32302, answer score: 3
Revisions (0)
No revisions yet.