Advertisement
dburyak

java cat with buffered IO

Jan 6th, 2022
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package dburyak.sandbox;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.OutputStreamWriter;
  8. import java.time.Duration;
  9. import java.time.Instant;
  10. import java.util.Scanner;
  11.  
  12. public class CatBuffered {
  13.     public static void main(String[] args) {
  14.         var numPreHeatIterationsStr = System.getenv("X_PREHEAT_ITERATIONS");
  15.         if (numPreHeatIterationsStr == null) {
  16.             numPreHeatIterationsStr = "500";
  17.         }
  18.         var inputBufferSizeStr = System.getenv("X_INPUT_BUFFER_SIZE");
  19.         if (inputBufferSizeStr == null) {
  20.             inputBufferSizeStr = "8192";
  21.         }
  22.         var outputBufferSizeStr = System.getenv("X_OUTPUT_BUFFER_SIZE");
  23.         if (outputBufferSizeStr == null) {
  24.             outputBufferSizeStr = "8192";
  25.         }
  26.  
  27.         var numPreheatIterations = Integer.parseInt(numPreHeatIterationsStr);
  28.         var inputBufferSize = Integer.parseInt(inputBufferSizeStr);
  29.         var outputBufferSize = Integer.parseInt(outputBufferSizeStr);
  30.         var filePath = args[0];
  31.  
  32.         try (var out = new BufferedWriter(new OutputStreamWriter(System.out), outputBufferSize)) {
  33.             out.append("preheating .....").flush();
  34.             var preheatingStartedAt = Instant.now();
  35.             for (int i = 0; i < numPreheatIterations; i++) {
  36.                 printFileWithReadline(filePath, inputBufferSize, out);
  37.             }
  38.             var preheatingTime = Duration.between(preheatingStartedAt, Instant.now());
  39.             out.flush();
  40.  
  41.             var startedAt = Instant.now();
  42.             printFileWithReadline(filePath, inputBufferSize, out);
  43.             out.flush();
  44.             var catTime = Duration.between(startedAt, Instant.now());
  45.  
  46.             out.append("num preheat operations : ").append(numPreHeatIterationsStr);
  47.             out.newLine();
  48.             out.append("preheat time : ").append(preheatingTime.toString());
  49.             out.newLine();
  50.             out.append("input buffer size : ").append(inputBufferSizeStr);
  51.             out.newLine();
  52.             out.append("output buffer size : ").append(outputBufferSizeStr);
  53.             out.newLine();
  54.             out.append("cat time : ").append(catTime.toString());
  55.             out.newLine();
  56.         } catch (IOException e) {
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.  
  61.     private static void printFileWithReadline(String filePath, int inputBufferSize, BufferedWriter out) {
  62.         try (var input = new BufferedReader(new FileReader(filePath), inputBufferSize)) {
  63.             String line = null;
  64.             while ((line = input.readLine()) != null) {
  65.                 out.append(line);
  66.                 out.newLine();
  67.             }
  68.         } catch (IOException e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.  
  73.     private static void printFileWithScanner(String filePath, int inputBufferSize, BufferedWriter out) {
  74.         try (var input = new BufferedReader(new FileReader(filePath), inputBufferSize)) {
  75.             var scanner = new Scanner(input);
  76.             while (scanner.hasNextLine()) {
  77.                 var line = scanner.nextLine();
  78.                 out.append(line);
  79.                 out.newLine();
  80.             }
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement