package test.scanner.read;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class TestScannerNoIOManyStreams {

	public static void main(String[] args) throws IOException {
		System.out.println("java.version=" + System.getProperty("java.version"));
		double d = 0.0;
		long totalElapsed = 0L;
		for (int f = 0; f < FILE_SIZES.length; ++f) {
			long byteCount = FILE_SIZES[f];
			long n = byteCount / 8;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			for (int i = 0; i < n; ++i) {
				String s = String.valueOf(1.0);
				bos.write(s.getBytes(StandardCharsets.UTF_8));
				bos.write('\n');
			}
			ByteArrayInputStream source = new ByteArrayInputStream(bos.toByteArray());
			long start = System.currentTimeMillis();
			try (Scanner scanner = new Scanner(source, StandardCharsets.UTF_8.name())) {
				while (scanner.hasNext()) {
					d += scanner.nextDouble();
				}
			}
			long end = System.currentTimeMillis();
			long elapsed = (end - start);
			totalElapsed += elapsed;
		}
		System.out.println("reading with Scanner took: " + totalElapsed + "ms");
		System.out.println("useless line to ensure everything is read " + d);
	}

	private static long[] FILE_SIZES = { 24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L,
			1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L,
			196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L,
			196608L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L,
			1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L,
			1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L,
			196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 24384L, 24217L,
			195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L,
			24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L,
			48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L,
			48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L,
			48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L,
			48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L,
			24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L,
			24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L,
			24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L,
			1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L,
			1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L,
			1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L,
			24384L, 24217L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 195060L, 193665L, 196608L, 196608L,
			1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L,
			196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 195060L, 193665L,
			196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L,
			195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L,
			195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L,
			195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L,
			195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L,
			195060L, 193665L, 196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L,
			196608L, 196608L, 24384L, 24217L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L,
			196608L, 196608L, 1499600L, 1499300L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 19829L, 19851L,
			19829L, 19851L, 19829L, 19851L, 19829L, 19851L, 19829L, 19851L, 24384L, 24217L, 195060L, 193665L, 196608L,
			196608L, 24384L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 24384L, 24217L, 196608L, 24384L, 24217L,
			195060L, 193665L, 196608L, 24384L, 24217L, 196608L, 24384L, 24217L, 195060L, 193665L, 196608L, 24384L,
			196608L, 24217L, 24384L, 24217L, 195060L, 193665L, 196608L, 24384L, 24217L, 196608L, 24384L, 24217L, 24384L,
			24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L,
			24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L,
			24217L, 24384L, 24217L, 48161L, 48157L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L,
			24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 24384L, 24217L, 1537381L, 1537381L, 1537381L, 196608L,
			196608L, 196608L, 196608L, 1537381L, 1537381L, 1537381L, 1540039L, 1540039L, 1540039L, 196608L, 196608L,
			196608L, 1540039L, 1540039L, 196608L, 1537381L, 1537381L, 1540039L, 1537381L, 1540039L, 1540039L, 1537381L,
			1537381L, 1537381L, 1540039L, 196608L, 196608L, 1540039L, 1540039L, 192515L, 192443L, 385441L, 1540039L,
			384906L, 192515L, 192443L, 385441L, 384906L, 192515L, 192443L, 196608L, 196608L, 385441L, 384906L, 192515L,
			192443L, 385441L, 384906L, 192515L, 192443L, 385441L, 384906L, 192515L, 192443L, 385441L, 384906L, 192515L,
			192443L, 196608L, 196608L, 196608L, 196608L, 196608L, 196608L, 196608L, 196608L, 196608L, 196608L, };
}
