Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dburyak.sandbox;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.time.Duration;
- import java.time.Instant;
- import java.util.Scanner;
- public class CatBuffered {
- public static void main(String[] args) {
- var numPreHeatIterationsStr = System.getenv("X_PREHEAT_ITERATIONS");
- if (numPreHeatIterationsStr == null) {
- numPreHeatIterationsStr = "500";
- }
- var inputBufferSizeStr = System.getenv("X_INPUT_BUFFER_SIZE");
- if (inputBufferSizeStr == null) {
- inputBufferSizeStr = "8192";
- }
- var outputBufferSizeStr = System.getenv("X_OUTPUT_BUFFER_SIZE");
- if (outputBufferSizeStr == null) {
- outputBufferSizeStr = "8192";
- }
- var numPreheatIterations = Integer.parseInt(numPreHeatIterationsStr);
- var inputBufferSize = Integer.parseInt(inputBufferSizeStr);
- var outputBufferSize = Integer.parseInt(outputBufferSizeStr);
- var filePath = args[0];
- try (var out = new BufferedWriter(new OutputStreamWriter(System.out), outputBufferSize)) {
- out.append("preheating .....").flush();
- var preheatingStartedAt = Instant.now();
- for (int i = 0; i < numPreheatIterations; i++) {
- printFileWithReadline(filePath, inputBufferSize, out);
- }
- var preheatingTime = Duration.between(preheatingStartedAt, Instant.now());
- out.flush();
- var startedAt = Instant.now();
- printFileWithReadline(filePath, inputBufferSize, out);
- out.flush();
- var catTime = Duration.between(startedAt, Instant.now());
- out.append("num preheat operations : ").append(numPreHeatIterationsStr);
- out.newLine();
- out.append("preheat time : ").append(preheatingTime.toString());
- out.newLine();
- out.append("input buffer size : ").append(inputBufferSizeStr);
- out.newLine();
- out.append("output buffer size : ").append(outputBufferSizeStr);
- out.newLine();
- out.append("cat time : ").append(catTime.toString());
- out.newLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void printFileWithReadline(String filePath, int inputBufferSize, BufferedWriter out) {
- try (var input = new BufferedReader(new FileReader(filePath), inputBufferSize)) {
- String line = null;
- while ((line = input.readLine()) != null) {
- out.append(line);
- out.newLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void printFileWithScanner(String filePath, int inputBufferSize, BufferedWriter out) {
- try (var input = new BufferedReader(new FileReader(filePath), inputBufferSize)) {
- var scanner = new Scanner(input);
- while (scanner.hasNextLine()) {
- var line = scanner.nextLine();
- out.append(line);
- out.newLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement