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 convfmt, 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
      convfmt - number-to-string conversion format (CONVFMT), used by %s to convert numeric values the way AWK does
      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
    • sprintf

      public String sprintf(String convfmt, String format, Object... values)
      Formats a string in the same way as AWK's sprintf() built-in, converting numeric %s operands with the supplied CONVFMT value.

      Subclasses may override this method to customize formatting. The default implementation delegates to AwkPrintf.sprintf(Locale, String, String, Object...). The built-in sinks render printf output through this method, so overriding it keeps printf and sprintf consistent.

      Parameters:
      convfmt - number-to-string conversion format (CONVFMT)
      format - format string
      values - arguments supplied after the format string
      Returns:
      formatted text