Class AwkSink
- Direct Known Subclasses:
AppendableAwkSink,OutputStreamAwkSink
print and printf statements.
Implementations decide how to represent AWK output, whether as text written to a stream, appended characters, or structured values collected by the embedding application. Numeric rendering uses the sink's immutable construction-time locale.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidflush()Flushes any buffered output held by this sink.static StringformatOutputValue(Object value, String ofmt, Locale locale) Formats one already-normalized AWK output value.protected final StringformatPrintArgument(Object value, String ofmt) Formats one operand of a plain AWKprintstatement.protected final StringformatPrintfResult(String format, Object... values) Formats oneprintfresult string using this sink's locale.static AwkSinkfrom(OutputStream outputStream) Creates a sink backed by anOutputStream.static AwkSinkfrom(OutputStream outputStream, Locale locale) Creates a sink backed by anOutputStream.static AwkSinkfrom(PrintStream printStream) Creates a sink backed by aPrintStream.static AwkSinkfrom(PrintStream printStream, Locale locale) Creates a sink backed by aPrintStream.static AwkSinkfrom(Appendable appendable) Creates a sink backed by anAppendable.static AwkSinkfrom(Appendable appendable, Locale locale) Creates a sink backed by anAppendable.final LocaleReturns the locale used by this sink when it renders numeric values.Returns aPrintStreamview that receives raw process output written by spawned commands such assystem("...").protected final ObjectnormalizePrintArgument(Object value) Deprecated.abstract voidWrites one AWKprintoperation.abstract voidWrites one AWKprintfoperation.Formats a string in the same way as AWK'ssprintf()built-in.
-
Field Details
-
NOP_SINK
A shared no-op sink that silently discards all output.This singleton is safe to share across all JRT/AVM instances because its
print(String, String, String, Object...),printf(String, String, String, String, Object...), andflush()operations are all no-ops.
-
-
Constructor Details
-
AwkSink
protected AwkSink()Creates a sink using the defaultLocale.USformatting rules. -
AwkSink
Creates a sink using the supplied locale for numeric formatting.- Parameters:
localeParam- locale to use for numeric formatting
-
-
Method Details
-
getLocale
Returns the locale used by this sink when it renders numeric values.- Returns:
- sink locale
-
print
public abstract void print(String ofs, String ors, String ofmt, Object... values) throws IOException Writes one AWKprintoperation.- Parameters:
ofs- output field separatorors- output record separatorofmt- numeric output format used by plainprintvalues- values supplied toprint- Throws:
IOException- if the sink cannot write the output
-
printf
public abstract void printf(String ofs, String ors, String ofmt, String format, Object... values) throws IOException Writes one AWKprintfoperation.- Parameters:
ofs- output field separatorors- output record separatorofmt- numeric output format available to the sinkformat- format string passed toprintfvalues- arguments supplied after the format string- Throws:
IOException- if the sink cannot write the output
-
flush
Flushes any buffered output held by this sink.- Throws:
IOException- if the sink cannot be flushed
-
getPrintStream
Returns aPrintStreamview that receives raw process output written by spawned commands such assystem("...").The default implementation returns a stream that silently discards all output. Override this method in sinks that need to capture process output.
- Returns:
- print stream that should receive raw process output
-
from
Creates a sink backed by anOutputStream.- Parameters:
outputStream- stream that should receive AWK output- Returns:
- sink writing to
outputStream
-
from
Creates a sink backed by anOutputStream.- Parameters:
outputStream- stream that should receive AWK outputlocale- locale to use for numeric formatting- Returns:
- sink writing to
outputStream
-
from
Creates a sink backed by aPrintStream.- Parameters:
printStream- stream that should receive AWK output- Returns:
- sink writing to
printStream
-
from
Creates a sink backed by aPrintStream.- Parameters:
printStream- stream that should receive AWK outputlocale- locale to use for numeric formatting- Returns:
- sink writing to
printStream
-
from
Creates a sink backed by anAppendable.- Parameters:
appendable- appendable that should receive AWK output- Returns:
- sink writing to
appendable
-
from
Creates a sink backed by anAppendable.- Parameters:
appendable- appendable that should receive AWK outputlocale- locale to use for numeric formatting- Returns:
- sink writing to
appendable
-
formatPrintArgument
Formats one operand of a plain AWKprintstatement.Numeric values are rendered with
OFMT(or as integers when they hold an integral value). String values — including numeric strings that originate from input — are printed verbatim, as required by POSIX.- Parameters:
value- operand to formatofmt- numeric output format- Returns:
- the textual representation AWK would print for this operand
-
normalizePrintArgument
Deprecated.Plainprintdoes not numerically coerce string values; use the operand as-is, orformatPrintArgument(Object, String)to render it the wayprintwould.Converts aprintoperand that renders as a numeric string into a numeric form.Historical versions of Jawk applied this conversion in plain
print, which is not what POSIX AWK does:printoutputs string values verbatim, and only actual numbers are formatted withOFMT. The built-in sinks therefore no longer call this helper; it is preserved only for compatibility with custom sinks compiled against earlier releases.- Parameters:
value- operand to normalize- Returns:
- the normalized value, either unchanged or converted to a numeric form
-
sprintf
Formats a string in the same way as AWK'ssprintf()built-in.Subclasses may override this method to customize formatting. The default implementation delegates to
Printf4J.sprintf(Locale, String, Object...). Becauseprintf(String, String, String, String, Object...)uses this method internally, overriding it ensures that bothprintfandsprintfproduce consistent output.- Parameters:
format- format stringvalues- arguments supplied after the format string- Returns:
- formatted text
-
formatPrintfResult
Formats oneprintfresult string using this sink's locale.- Parameters:
format- format string passed toprintfvalues- arguments supplied after the format string- Returns:
- formatted text
-
formatOutputValue
Formats one already-normalized AWK output value.- Parameters:
value- value to formatofmt- numeric output formatlocale- locale used for numeric formatting- Returns:
- textual output for
value
-
printdoes not numerically coerce string values; use the operand as-is, orformatPrintArgument(Object, String)to render it the wayprintwould.