Package io.jawk.jrt

Class AwkSink

java.lang.Object
io.jawk.jrt.AwkSink
Direct Known Subclasses:
AppendableAwkSink, OutputStreamAwkSink

public abstract class AwkSink extends Object
Output target used by AWK 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 Details

  • Constructor Details

    • AwkSink

      protected AwkSink()
      Creates a sink using the default Locale.US formatting rules.
    • AwkSink

      protected AwkSink(Locale localeParam)
      Creates a sink using the supplied locale for numeric formatting.
      Parameters:
      localeParam - locale to use for numeric formatting
  • Method Details

    • getLocale

      public final Locale 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 AWK print operation.
      Parameters:
      ofs - output field separator
      ors - output record separator
      ofmt - numeric output format used by plain print
      values - values supplied to print
      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 AWK printf operation.
      Parameters:
      ofs - output field separator
      ors - output record separator
      ofmt - numeric output format available to the sink
      format - format string passed to printf
      values - arguments supplied after the format string
      Throws:
      IOException - if the sink cannot write the output
    • flush

      public void flush() throws IOException
      Flushes any buffered output held by this sink.
      Throws:
      IOException - if the sink cannot be flushed
    • getPrintStream

      public PrintStream getPrintStream()
      Returns a PrintStream view that receives raw process output written by spawned commands such as system("...").

      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

      public static AwkSink from(OutputStream outputStream)
      Creates a sink backed by an OutputStream.
      Parameters:
      outputStream - stream that should receive AWK output
      Returns:
      sink writing to outputStream
    • from

      public static AwkSink from(OutputStream outputStream, Locale locale)
      Creates a sink backed by an OutputStream.
      Parameters:
      outputStream - stream that should receive AWK output
      locale - locale to use for numeric formatting
      Returns:
      sink writing to outputStream
    • from

      public static AwkSink from(PrintStream printStream)
      Creates a sink backed by a PrintStream.
      Parameters:
      printStream - stream that should receive AWK output
      Returns:
      sink writing to printStream
    • from

      public static AwkSink from(PrintStream printStream, Locale locale)
      Creates a sink backed by a PrintStream.
      Parameters:
      printStream - stream that should receive AWK output
      locale - locale to use for numeric formatting
      Returns:
      sink writing to printStream
    • from

      public static AwkSink from(Appendable appendable)
      Creates a sink backed by an Appendable.
      Parameters:
      appendable - appendable that should receive AWK output
      Returns:
      sink writing to appendable
    • from

      public static AwkSink from(Appendable appendable, Locale locale)
      Creates a sink backed by an Appendable.
      Parameters:
      appendable - appendable that should receive AWK output
      locale - locale to use for numeric formatting
      Returns:
      sink writing to appendable
    • formatPrintArgument

      protected final String formatPrintArgument(Object value, String ofmt)
      Formats one operand of a plain AWK print statement.

      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 format
      ofmt - numeric output format
      Returns:
      the textual representation AWK would print for this operand
    • normalizePrintArgument

      @Deprecated protected final Object normalizePrintArgument(Object value)
      Deprecated.
      Plain print does not numerically coerce string values; use the operand as-is, or formatPrintArgument(Object, String) to render it the way print would.
      Converts a print operand 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: print outputs string values verbatim, and only actual numbers are formatted with OFMT. 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

      public String sprintf(String format, Object... values)
      Formats a string in the same way as AWK's sprintf() built-in.

      Subclasses may override this method to customize formatting. The default implementation delegates to Printf4J.sprintf(Locale, String, Object...). Because printf(String, String, String, String, Object...) uses this method internally, overriding it ensures that both printf and sprintf produce consistent output.

      Parameters:
      format - format string
      values - arguments supplied after the format string
      Returns:
      formatted text
    • formatPrintfResult

      protected final String formatPrintfResult(String format, Object... values)
      Formats one printf result string using this sink's locale.
      Parameters:
      format - format string passed to printf
      values - arguments supplied after the format string
      Returns:
      formatted text
    • formatOutputValue

      public static String formatOutputValue(Object value, String ofmt, Locale locale)
      Formats one already-normalized AWK output value.
      Parameters:
      value - value to format
      ofmt - numeric output format
      locale - locale used for numeric formatting
      Returns:
      textual output for value