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 @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "No security-sensitive state; finalizer attacks are not a concern here.")
36 public class OutputStreamAwkSink extends AwkSink {
37
38 private final PrintStream printStream;
39
40
41
42
43
44
45 public OutputStreamAwkSink(OutputStream outputStream) {
46 this(outputStream, Locale.US);
47 }
48
49
50
51
52
53
54
55 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "The provided stream is the sink's output target by design.")
56 public OutputStreamAwkSink(OutputStream outputStream, Locale locale) {
57 super(locale);
58 Objects.requireNonNull(outputStream, "outputStream");
59 if (outputStream instanceof PrintStream) {
60 this.printStream = (PrintStream) outputStream;
61 } else {
62 try {
63 this.printStream = new PrintStream(outputStream, false, StandardCharsets.UTF_8.name());
64 } catch (java.io.UnsupportedEncodingException e) {
65 throw new IllegalStateException(e);
66 }
67 }
68 }
69
70
71
72
73
74
75 public OutputStreamAwkSink(PrintStream printStream) {
76 this(printStream, Locale.US);
77 }
78
79
80
81
82
83
84
85 public OutputStreamAwkSink(PrintStream printStream, Locale locale) {
86 super(locale);
87 this.printStream = Objects.requireNonNull(printStream, "printStream");
88 }
89
90 @Override
91 public void print(String ofs, String ors, String ofmt, Object... values) {
92 for (int i = 0; i < values.length; i++) {
93 printStream.print(formatPrintArgument(values[i], ofmt));
94 if (i < values.length - 1) {
95 printStream.print(ofs);
96 }
97 }
98 printStream.print(ors);
99 }
100
101 @Override
102 public void printf(String ofs, String ors, String ofmt, String convfmt, String format, Object... values) {
103 printStream.print(sprintf(convfmt, format, values));
104 }
105
106 @Override
107 public void flush() {
108 printStream.flush();
109 }
110
111 @Override
112 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Callers need the live PrintStream used for process output pumping.")
113 public PrintStream getPrintStream() {
114 return printStream;
115 }
116 }