Package io.jawk.jrt

Class StreamInputSource

java.lang.Object
io.jawk.jrt.StreamInputSource
All Implemented Interfaces:
InputSource, Closeable, AutoCloseable

public class StreamInputSource extends Object implements InputSource, Closeable
An InputSource that reads records from an InputStream, traversing the ARGV array to open filenames and apply name=value variable assignments exactly like the classic AWK command-line flow.

When no filename arguments are present in ARGV, records are read from the supplied default InputStream (usually System.in). This class is the default InputSource used internally by the runtime when no custom source has been configured via AwkSettings#setInputSource(...).

API note: this type is public to allow runtime wiring between packages, but it is considered an internal implementation detail. Embedding applications should implement InputSource directly rather than depend on this class, whose behavior may change in future releases.

See Also:
  • Constructor Details

    • StreamInputSource

      public StreamInputSource(InputStream defaultInput, VariableManager vm, JRT jrt)
      Creates a stream-backed input source.
      Parameters:
      defaultInput - the fallback input stream used when ARGV contains no filename arguments (typically System.in)
      vm - the variable manager providing access to ARGV and ARGC
      jrt - the JRT instance used for string conversion and special variable updates
  • Method Details

    • nextRecord

      public boolean nextRecord() throws IOException
      Advances to the next input record.

      Implementations should keep the current record accessible through InputSource.getRecordText() and InputSource.getFields() until a subsequent call successfully advances to a new record. This allows END blocks to continue observing the last consumed record after the final EOF probe.

      Specified by:
      nextRecord in interface InputSource
      Returns:
      true when a record is available, false when input is exhausted
      Throws:
      IOException - if an I/O error occurs
    • getRecordText

      public String getRecordText()
      Returns the current record text ($0), or null when the source only exposes pre-split fields for the current record.

      When both InputSource.getRecordText() and InputSource.getFields() return non-null values, the field list is authoritative for field/NF access while the record text is authoritative for the initial $0 value.

      Specified by:
      getRecordText in interface InputSource
      Returns:
      current record text, or null when unavailable
    • getFields

      public List<String> getFields()
      Always returns null so that the runtime splits $0 using the current field separator (FS).
      Specified by:
      getFields in interface InputSource
      Returns:
      null
    • isFromFilenameList

      public boolean isFromFilenameList()
      Indicates whether the current record originates from a named file in the argument list.

      For custom implementations, this flag also controls whether consuming a record advances the per-file record counter FNR. Records read by the built-in stream input always advance FNR — including standard input — per POSIX.

      Specified by:
      isFromFilenameList in interface InputSource
      Returns:
      true when sourced from a filename argument
    • setRecordSeparator

      public void setRecordSeparator(String rs)
      Propagates a record-separator change to the active PartitioningReader.
      Parameters:
      rs - the new record separator value
    • advanceToNextFile

      public boolean advanceToNextFile() throws IOException
      Advance to the next input file for the per-file main input loop used when BEGINFILE/ENDFILE rules or nextfile are present. Variable assignment arguments are applied along the way, exactly like nextRecord() does when it crosses a file boundary.

      On success, FILENAME, FNR, ARGIND, and ERRNO are updated and $0 is cleared, so the BEGINFILE rules observe the new file before any record is read. A file that cannot be opened is still reported as available: ERRNO carries the error description and getCurrentFileOpenError() returns it until the next advance, enabling gawk's non-fatal BEGINFILE error handling.

      Returns:
      true when a new input file (or the initial stdin stream) is current; false when input is exhausted
      Throws:
      IOException - if an I/O error occurs while traversing ARGV
    • nextRecordInCurrentFile

      public boolean nextRecordInCurrentFile() throws IOException
      Reads the next record of the current input file only, never advancing to the next input file. Used by the per-file main input loop so that ENDFILE rules can run at each file boundary.
      Returns:
      true when a record is available; false at the end of the current input file
      Throws:
      IOException - if an I/O error occurs
    • getCurrentFileOpenError

      public String getCurrentFileOpenError()
      Returns the error description recorded when the current input file could not be opened by advanceToNextFile(), or null when the current input is readable.
      Returns:
      the pending open error, or null
    • close

      public void close() throws IOException
      Releases any open file-backed reader held by this source.

      This method is idempotent and safe to call multiple times. It does not close the default input stream (System.in).

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - never thrown; signature required by Closeable