Package com.chatmotorapi.api.listener
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 Summary
Constructors Constructor Description MotorFileResponseListener(File file, Charset charset)
Constructor that accepts the file path where response chunks will be written.MotorFileResponseListener(String filePath, Charset charset)
Constructor that accepts the file path where response chunks will be written. -
Method Summary
Modifier and Type Method Description void
onChunkReceived(MotorResponse motorResponse)
Receives a chunk of response from the ChatMotor and writes it to the specified file.void
onError(Throwable throwable)
Called when an error occurs.void
onResponseCompleted()
Called when the response is completed.
-
Constructor Details
-
MotorFileResponseListener
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
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
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 interfaceMotorResponseListener
- Parameters:
motorResponse
- TheMotorResponse
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 interfaceMotorResponseListener
-
onError
Called when an error occurs. This method handles logging the error and ensures that resources are cleaned up properly.- Specified by:
onError
in interfaceMotorResponseListener
- Parameters:
throwable
- TheThrowable
that occurred during the response handling.
-