Package com.storedobject.common
Class StringCollector
java.lang.Object
com.storedobject.common.StringCollector
- All Implemented Interfaces:
Runnable
The StringCollector class reads input data asynchronously from a given Reader or InputStream
and collects it into a single concatenated string. It is designed to be executed on a virtual thread.
This class also allows retrieval of any exception encountered during the reading process.
StringCollector starts a virtual thread that reads and processes lines from the input source.
The collected string can be accessed after the reading process is completed, and users can
wait for the process to complete using the provided methods. Typically, you invoke the
Object.toString()
method to the string being read.- Author:
- Syam
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a StringCollector that reads input data from the given InputStream asynchronously and collects the data into a single concatenated string.Constructs a new StringCollector that reads input data from the given Reader object. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the exception that occurred during the reading process, if any.Returns the concatenated string that has been collected from the input source.void
join()
Waits for the virtual thread, responsible for collecting input data, to complete execution.void
run()
Executes the main logic for collecting input data from the provided BufferedReader.
-
Constructor Details
-
StringCollector
Creates a StringCollector that reads input data from the given InputStream asynchronously and collects the data into a single concatenated string.- Parameters:
in
- the InputStream to read input data from
-
StringCollector
Constructs a new StringCollector that reads input data from the given Reader object. The input is read asynchronously using a virtual thread and stored in a concatenated string.- Parameters:
in
- the Reader from which the input data will be read
-
-
Method Details
-
run
public void run()Executes the main logic for collecting input data from the provided BufferedReader. This method reads lines of text iteratively from the input source and concatenates them into a singleStringBuilder
instance, separating lines with a newline character. If an IOException occurs during the reading process, the exception is captured and stored for later retrieval. The input stream is closed in the cleanup step to release associated system resources. The method runs on the virtual thread started during the instantiation of the StringCollector, facilitating asynchronous data collection. Any exception encountered can be accessed via thegetException()
method, and the concatenated string can be fetched throughgetString()
. -
getException
Retrieves the exception that occurred during the reading process, if any. If no exception occurred, this method returns null.- Returns:
- the exception encountered during the reading process, or null if no exception occurred.
-
getString
Returns the concatenated string that has been collected from the input source. This method waits for the internal thread to finish processing before returning the result, ensuring that all input data has been read and appended to the string.- Returns:
- The complete string collected from the input source.
-
join
public void join()Waits for the virtual thread, responsible for collecting input data, to complete execution. This method blocks until the associated collector thread has finished processing or is interrupted. If the thread is interrupted while waiting, the interruption is silently ignored and the thread continues to wait.
-