Fastest input in Java

Hi guys I’m trying to solve Java Practice problems here. I’ve noticed that instead of using the Scanner object and taking inputs if I create a suppose readInt() func and take input the Runtime is way faster.

For instance here is my readInt() which I use to take integer inputs just like a Scanner does.

  static int readInt() throws IOException{
	int number = 0;
	int neg = 1;
	if(offset==bufferSize){
		offset = 0;
		bufferSize = in.read(buffer);
	}
	for(;buffer[offset]<0x30 || buffer[offset]=='-'; ++offset)
	{
		if (buffer[offset]=='-')
			neg = -1;
		if(offset==bufferSize-1){
			offset=-1;
			bufferSize = in.read(buffer);
		}
	}
	for(;offset<bufferSize && buffer[offset]>0x2f;++offset){
		number = number*0x0a+buffer[offset]-0x30;
		if(offset==bufferSize-1){
			offset = -1;
			bufferSize = in.read(buffer);
		}
	}
	++offset;
	return number*neg;
} 

Is there any similar mehtod for Double and String variables. Scanner seems to take a lot of execution time. Also why does the scanner take so much time??

Scanner takes a lot of time because when it takes the input, it breaks the input into tokens and performs a lot of tokenisation and transformations so as to provide a lot of inbuilt functionalities to the user.

this is my template for Input reading:

static class Scanner {
    StringTokenizer st;
    BufferedReader br;

    public Scanner(InputStream s) {
        br = new BufferedReader(new InputStreamReader(s));
    }

    public String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public String nextLine() throws IOException {
        return br.readLine();
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public boolean ready() throws IOException {
        return br.ready();
    }
}

Here is a submission with that template: CodeChef: Practical coding for everyone

I pretty much use it as the normal Scanner class. Just that it runs faster in the back and will not TLE for big reads.