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  public final class OutputStreamAwkSink extends AwkSink {
36  
37  	private final PrintStream printStream;
38  
39  	/**
40  	 * Creates a sink backed by an {@link OutputStream}.
41  	 *
42  	 * @param outputStream stream that should receive AWK output
43  	 */
44  	public OutputStreamAwkSink(OutputStream outputStream) {
45  		this(outputStream, Locale.US);
46  	}
47  
48  	/**
49  	 * Creates a sink backed by an {@link OutputStream}.
50  	 *
51  	 * @param outputStream stream that should receive AWK output
52  	 * @param locale locale used for numeric formatting
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  	 * Creates a sink backed directly by a {@link PrintStream}.
71  	 *
72  	 * @param printStream stream that should receive AWK output
73  	 */
74  	public OutputStreamAwkSink(PrintStream printStream) {
75  		this(printStream, Locale.US);
76  	}
77  
78  	/**
79  	 * Creates a sink backed directly by a {@link PrintStream}.
80  	 *
81  	 * @param printStream stream that should receive AWK output
82  	 * @param locale locale used for numeric formatting
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 }