001package com.streamconverter;
002
003import com.streamconverter.command.IStreamCommand;
004import com.streamconverter.command.impl.SampleStreamCommand;
005import java.io.ByteArrayInputStream;
006import java.io.ByteArrayOutputStream;
007import java.io.IOException;
008import java.io.InputStream;
009import java.nio.charset.StandardCharsets;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Main class for the StreamConverter application.
015 *
016 * <p>This class demonstrates the usage of the StreamConverter with sample commands.
017 */
018public final class Main {
019  private static final Logger LOG = LoggerFactory.getLogger(Main.class);
020
021  /** Prevent instantiation. */
022  private Main() {}
023
024  /**
025   * Main method to run the StreamConverter application.
026   *
027   * @param args Command line arguments (not used).
028   * @throws IOException If an I/O error occurs during the execution.
029   */
030  public static void main(String[] args) throws IOException {
031    LOG.info("Starting StreamConverter application");
032    final IStreamCommand[] commands = {
033      new SampleStreamCommand("0"), new SampleStreamCommand("1"), new SampleStreamCommand("2")
034    };
035    final StreamConverter converter = new StreamConverter(commands);
036    try (InputStream inputStream =
037            new ByteArrayInputStream("any message".getBytes(StandardCharsets.UTF_8));
038        ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
039      converter.run(inputStream, outputStream);
040      if (LOG.isInfoEnabled()) {
041        LOG.info("Processing result: {}", outputStream.toString(StandardCharsets.UTF_8));
042      }
043    } catch (IOException e) {
044      if (LOG.isErrorEnabled()) {
045        LOG.error("Application execution failed: {}", e.getMessage(), e);
046      }
047    }
048
049    if (LOG.isInfoEnabled()) {
050      LOG.info("StreamConverter application completed");
051    }
052  }
053}