View Javadoc
1   package io.jawk.jrt;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * Text {@link AwkSink} backed by a {@link PrintStream}.
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  	 * Creates a sink backed by an {@link OutputStream}.
42  	 *
43  	 * @param outputStream stream that should receive AWK output
44  	 */
45  	public OutputStreamAwkSink(OutputStream outputStream) {
46  		this(outputStream, Locale.US);
47  	}
48  
49  	/**
50  	 * Creates a sink backed by an {@link OutputStream}.
51  	 *
52  	 * @param outputStream stream that should receive AWK output
53  	 * @param locale locale used for numeric formatting
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  	 * Creates a sink backed directly by a {@link PrintStream}.
72  	 *
73  	 * @param printStream stream that should receive AWK output
74  	 */
75  	public OutputStreamAwkSink(PrintStream printStream) {
76  		this(printStream, Locale.US);
77  	}
78  
79  	/**
80  	 * Creates a sink backed directly by a {@link PrintStream}.
81  	 *
82  	 * @param printStream stream that should receive AWK output
83  	 * @param locale locale used for numeric formatting
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 }