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

  • 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
    • 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
    • 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