Class MotorFileResponseListener

java.lang.Object
com.chatmotorapi.api.listener.MotorFileResponseListener
All Implemented Interfaces:
MotorResponseListener

public class MotorFileResponseListener
extends Object
implements MotorResponseListener
A concrete implementation of MotorResponseListener that outputs response chunks to a file. This listener is designed to handle streamed responses from the ChatMotor and write each chunk received directly to a specified file.
  • Constructor Details

    • MotorFileResponseListener

      public MotorFileResponseListener​(String filePath, Charset charset)
      Constructor that accepts the file path where response chunks will be written.
      Parameters:
      filePath - The path of the file to write the response chunks.
      charset - The character set to use when writing the response chunks. Can be null. The default charset is used if null.
      Throws:
      IllegalArgumentException - if the filePath is null.
    • MotorFileResponseListener

      public MotorFileResponseListener​(File file, Charset charset)
      Constructor that accepts the file path where response chunks will be written.
      Parameters:
      file - The file to write the response chunks.
      charset - The character set to use when writing the response chunks. Can be null. The default charset is used if null.
      Throws:
      IllegalArgumentException - if the filePath is null.
  • Method Details

    • onChunkReceived

      public void onChunkReceived​(MotorResponse motorResponse)
      Receives a chunk of response from the ChatMotor and writes it to the specified file. This method is called automatically when a new response chunk is available.
      Specified by:
      onChunkReceived in interface MotorResponseListener
      Parameters:
      motorResponse - The MotorResponse containing the chunk of data to be written. The response is expected to be non-null and ready to be written.
    • onResponseCompleted

      public void onResponseCompleted()
      Called when the response is completed. This ensures that any remaining data is written and resources are properly closed.
      Specified by:
      onResponseCompleted in interface MotorResponseListener
    • onError

      public void onError​(Throwable throwable)
      Called when an error occurs. This method handles logging the error and ensures that resources are cleaned up properly.
      Specified by:
      onError in interface MotorResponseListener
      Parameters:
      throwable - The Throwable that occurred during the response handling.