Class DatevStreamWriter

java.lang.Object
io.github.mrtyldr.datev.plain.DatevStreamWriter
All Implemented Interfaces:
AutoCloseable

public final class DatevStreamWriter extends Object implements AutoCloseable
Writes a fixed DATEV column-heading record and booking rows without retaining completed rows.

Attach a DatevMetadata through builder() to emit the mandatory EXTF management record before the heading and produce a complete, importable Buchungsstapel file. Both records are written when the writer is constructed.

Each append is fully aligned, formatted, structurally checked, optionally validated and serialized in row-local memory before output begins. Every completed record is then handed to the destination immediately; this writer does not batch records or retain serialization buffers between appends. Successfully written rows are discarded, so library-managed working memory is proportional to one row instead of the total row count. Byte-stream output uses one temporary encoded byte array per record. Callers decide whether the destination itself should buffer those writes by supplying, for example, a BufferedOutputStream. The supplied output remains caller-owned: during normal completion close() flushes it but does not close it. After a destination failure, close does not retry a flush. Byte-stream factories emit Windows-1252 directly; callers supplying a character Writer remain responsible for its eventual byte encoding.

Validation and formatting failures leave the output unchanged and the writer reusable. An I/O failure can occur after a destination has accepted part of a record, so physical rollback cannot be guaranteed; the writer becomes terminal after any destination failure. Instances are mutable, forward-only and not thread-safe.

  • Method Details

    • builder

      public static DatevStreamWriter.Builder builder()
      Creates a builder for the current v13 schema.
      Returns:
      a new builder
    • builder

      public static DatevStreamWriter.Builder builder(io.github.mrtyldr.datev.core.DatevSchema schema)
      Creates a builder for an explicit fixed schema.
      Parameters:
      schema - the fixed schema to use
      Returns:
      a new builder
    • withDefaults

      public static DatevStreamWriter withDefaults(OutputStream output)
      Starts an unvalidated current-v13 writer on a caller-owned byte stream.
      Parameters:
      output - destination receiving strict Windows-1252 bytes
      Returns:
      a new forward-only writer whose column heading has been written
    • withDefaults

      public static DatevStreamWriter withDefaults(OutputStream output, BiConsumer<Integer,List<String>> validator)
      Starts a validated current-v13 writer on a caller-owned byte stream.
      Parameters:
      output - destination receiving strict Windows-1252 bytes
      validator - validator invoked before each row write
      Returns:
      a new forward-only writer whose column heading has been written
    • withDefaults

      public static DatevStreamWriter withDefaults(Writer output)
      Starts an unvalidated current-v13 writer on a caller-owned character writer.
      Parameters:
      output - destination character writer
      Returns:
      a new forward-only writer whose column heading has been written
    • withDefaults

      public static DatevStreamWriter withDefaults(Writer output, BiConsumer<Integer,List<String>> validator)
      Starts a validated current-v13 writer on a caller-owned character writer.
      Parameters:
      output - destination character writer
      validator - validator invoked before each row write
      Returns:
      a new forward-only writer whose column heading has been written
    • legacyV12

      public static DatevStreamWriter legacyV12(OutputStream output)
      Starts an unvalidated legacy-v12 writer on a caller-owned byte stream.
      Parameters:
      output - destination receiving strict Windows-1252 bytes
      Returns:
      a new forward-only writer whose column heading has been written
    • legacyV12

      public static DatevStreamWriter legacyV12(OutputStream output, BiConsumer<Integer,List<String>> validator)
      Starts a validated legacy-v12 writer on a caller-owned byte stream.
      Parameters:
      output - destination receiving strict Windows-1252 bytes
      validator - validator invoked before each row write
      Returns:
      a new forward-only writer whose column heading has been written
    • legacyV12

      public static DatevStreamWriter legacyV12(Writer output)
      Starts an unvalidated legacy-v12 writer on a caller-owned character writer.
      Parameters:
      output - destination character writer
      Returns:
      a new forward-only writer whose column heading has been written
    • legacyV12

      public static DatevStreamWriter legacyV12(Writer output, BiConsumer<Integer,List<String>> validator)
      Starts a validated legacy-v12 writer on a caller-owned character writer.
      Parameters:
      output - destination character writer
      validator - validator invoked before each row write
      Returns:
      a new forward-only writer whose column heading has been written
    • forSchema

      public static DatevStreamWriter forSchema(io.github.mrtyldr.datev.core.DatevSchema schema, OutputStream output)
      Starts an unvalidated writer for one fixed schema on a caller-owned byte stream.
      Parameters:
      schema - fixed DATEV schema
      output - destination receiving strict Windows-1252 bytes
      Returns:
      a new forward-only writer whose column heading has been written
    • forSchema

      public static DatevStreamWriter forSchema(io.github.mrtyldr.datev.core.DatevSchema schema, OutputStream output, BiConsumer<Integer,List<String>> validator)
      Starts a validated writer for one fixed schema on a caller-owned byte stream.
      Parameters:
      schema - fixed DATEV schema
      output - destination receiving strict Windows-1252 bytes
      validator - validator invoked before each row write
      Returns:
      a new forward-only writer whose column heading has been written
    • forSchema

      public static DatevStreamWriter forSchema(io.github.mrtyldr.datev.core.DatevSchema schema, Writer output)
      Starts an unvalidated writer for one fixed schema on a caller-owned character writer.
      Parameters:
      schema - fixed DATEV schema
      output - destination character writer
      Returns:
      a new forward-only writer whose column heading has been written
    • forSchema

      public static DatevStreamWriter forSchema(io.github.mrtyldr.datev.core.DatevSchema schema, Writer output, BiConsumer<Integer,List<String>> validator)
      Starts a validated writer for one fixed schema on a caller-owned character writer.
      Parameters:
      schema - fixed DATEV schema
      output - destination character writer
      validator - validator invoked before each row write
      Returns:
      a new forward-only writer whose column heading has been written
    • schema

      public io.github.mrtyldr.datev.core.DatevSchema schema()
      Returns the fixed schema selected for this output.
      Returns:
      selected v12 or v13 schema
    • headers

      public List<String> headers()
      Returns the immutable official headings in output order.
      Returns:
      fixed ordered headings
    • charset

      public Charset charset()
      Returns the byte-output charset.
      Returns:
      Windows-1252
    • validator

      public Optional<BiConsumer<Integer,List<String>>> validator()
      Returns the configured optional validator.
      Returns:
      configured format-version/row consumer, if any
    • metadata

      public Optional<io.github.mrtyldr.datev.core.DatevMetadata> metadata()
      Returns the EXTF management record written before the heading, if any.
      Returns:
      the configured metadata
    • isCompleteExtf

      public boolean isCompleteExtf()
      Returns whether the output contains the mandatory EXTF management record.
      Returns:
      true if metadata was configured
    • rowCount

      public int rowCount()
      Returns the number of booking rows successfully handed to the destination.
      Returns:
      committed row count, excluding the heading
    • isEmpty

      public boolean isEmpty()
      Returns whether no booking row has been successfully written.
      Returns:
      true until the first successful row write
    • append

      public void append(String semicolonSeparatedRow)
      Parses and writes exactly one strict semicolon-delimited CSV record.
      Parameters:
      semicolonSeparatedRow - exactly one complete CSV row
    • append

      public void append(String[] orderedValues)
      Writes a complete positional row from a defensively copied array.
      Parameters:
      orderedValues - values in fixed heading order
    • append

      public void append(Collection<String> orderedValues)
      Writes a complete positional row from a defensively copied collection.
      Parameters:
      orderedValues - values in fixed heading order
    • appendValues

      public void appendValues(Object... orderedValues)
      Writes a complete positional row after converting non-null values with String.valueOf(Object).
      Parameters:
      orderedValues - values in fixed heading order
    • append

      public void append(Map<String,?> valuesByHeader)
      Writes a sparse row addressed by exact official heading names.
      Parameters:
      valuesByHeader - values addressed by exact official headings
    • append

      public void append(io.github.mrtyldr.datev.core.DatevColumn<?>... columns)
      Writes a sparse formatted row addressed by exact official heading names.
      Parameters:
      columns - columns to align by heading
    • append

      public void append(Iterable<? extends io.github.mrtyldr.datev.core.DatevColumn<?>> columns)
      Writes formatted columns from an iterable.
      Parameters:
      columns - columns to align by heading
    • appendColumns

      public void appendColumns(Collection<? extends io.github.mrtyldr.datev.core.DatevColumn<?>> columns)
      Writes a sparse formatted row from a collection.
      Parameters:
      columns - columns to align by heading
    • flush

      public void flush()
      Flushes completed records without closing the caller-owned destination.
      Throws:
      UncheckedIOException - if the destination rejects the flush
    • close

      public void close()
      Flushes completed records and makes this writer terminal without closing the caller-owned destination.

      Repeated calls have no effect. If a previous destination operation failed, this method does not retry flushing potentially partial output, and it does not close the destination either.

      Specified by:
      close in interface AutoCloseable
      Throws:
      UncheckedIOException - if the destination rejects the flush