Class ConsumerCommand

java.lang.Object
com.streamconverter.command.AbstractStreamCommand
com.streamconverter.command.ConsumerCommand
All Implemented Interfaces:
IStreamCommand
Direct Known Subclasses:
CsvValidateCommand, ValidateCommand

public abstract class ConsumerCommand extends AbstractStreamCommand
Abstract class for commands that consume an input stream and produce an output stream.

This class provides a template method pattern for executing the command, ensuring that the input and output streams are not null. It also provides a method to consume the input stream, which must be implemented by subclasses.

  • Constructor Details

    • ConsumerCommand

      public ConsumerCommand()
      Default constructor.

      Initializes the command with default settings.

  • Method Details

    • execute

      public void execute(InputStream inputStream, OutputStream outputStream) throws IOException
      Executes the command on the provided input stream and writes the result to the output stream.

      Note on data integrity: This method uses TeeInputStream to copy input bytes to outputStream as they are read by consume(InputStream). If consume() throws an exception, partial data may already have been written to outputStream. To prevent this, place a FileBufferCommand immediately before this command in the pipeline:

      
       StreamConverter.create(
           new MyValidateCommand(),
           FileBufferCommand.create(),
           new MyConsumerCommand()  // ConsumerCommand subclass
       );
       
      Specified by:
      execute in interface IStreamCommand
      Specified by:
      execute in class AbstractStreamCommand
      Parameters:
      inputStream - The input stream to read data from.
      outputStream - The output stream to write data to.
      Throws:
      IOException - If an I/O error occurs during the execution of the command.
    • consume

      public abstract void consume(InputStream inputStream) throws IOException
      Abstract method to be implemented by subclasses for consuming the input stream.
      Parameters:
      inputStream - The input stream to read data from.
      Throws:
      IOException - If an I/O error occurs during the execution of the command.