1 package io.jawk.jrt;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.OutputStream;
26 import java.io.PrintStream;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Locale;
29 import java.util.Objects;
30 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
31
32
33
34
35 public final class OutputStreamAwkSink extends AwkSink {
36
37 private final PrintStream printStream;
38
39
40
41
42
43
44 public OutputStreamAwkSink(OutputStream outputStream) {
45 this(outputStream, Locale.US);
46 }
47
48
49
50
51
52
53
54 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "The provided stream is the sink's output target by design.")
55 public OutputStreamAwkSink(OutputStream outputStream, Locale locale) {
56 super(locale);
57 Objects.requireNonNull(outputStream, "outputStream");
58 if (outputStream instanceof PrintStream) {
59 this.printStream = (PrintStream) outputStream;
60 } else {
61 try {
62 this.printStream = new PrintStream(outputStream, false, StandardCharsets.UTF_8.name());
63 } catch (java.io.UnsupportedEncodingException e) {
64 throw new IllegalStateException(e);
65 }
66 }
67 }
68
69
70
71
72
73
74 public OutputStreamAwkSink(PrintStream printStream) {
75 this(printStream, Locale.US);
76 }
77
78
79
80
81
82
83
84 public OutputStreamAwkSink(PrintStream printStream, Locale locale) {
85 super(locale);
86 this.printStream = Objects.requireNonNull(printStream, "printStream");
87 }
88
89 @Override
90 public void print(String ofs, String ors, String ofmt, Object... values) {
91 for (int i = 0; i < values.length; i++) {
92 printStream.print(formatPrintArgument(values[i], ofmt));
93 if (i < values.length - 1) {
94 printStream.print(ofs);
95 }
96 }
97 printStream.print(ors);
98 }
99
100 @Override
101 public void printf(String ofs, String ors, String ofmt, String format, Object... values) {
102 printStream.print(formatPrintfResult(format, values));
103 }
104
105 @Override
106 public void flush() {
107 printStream.flush();
108 }
109
110 @Override
111 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Callers need the live PrintStream used for process output pumping.")
112 public PrintStream getPrintStream() {
113 return printStream;
114 }
115 }