This is an automated email from the ASF dual-hosted git repository.
oscerd pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ef8b5e1b9f4e CAMEL-24488: camel-ibm-watson-speech-to-text - close the
audio FileInputStream opened by the producer (#25737)
ef8b5e1b9f4e is described below
commit ef8b5e1b9f4e96b1c61c8713f2285eb8cab6032a
Author: Andrea Cosentino <[email protected]>
AuthorDate: Wed Aug 26 09:15:29 2026 +0200
CAMEL-24488: camel-ibm-watson-speech-to-text - close the audio
FileInputStream opened by the producer (#25737)
WatsonSpeechToTextProducer.recognize() opened a FileInputStream for the
audio
input (from the CamelIbmWatsonSttAudioFile header or a File body) but never
closed it, leaking a file descriptor on every File-based invocation and on
any
exception thrown during recognition. Track an ownStream flag (set only for
the
two producer-opened FileInputStream cases) and close the stream via
IOHelper.close in a finally block around the recognition. A stream supplied
as
the exchange body is owned by the caller and is left untouched.
Signed-off-by: Andrea Cosentino <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
---
.../ibm/watson/stt/WatsonSpeechToTextProducer.java | 56 +++++++++++++---------
1 file changed, 33 insertions(+), 23 deletions(-)
diff --git
a/components/camel-ibm/camel-ibm-watson-speech-to-text/src/main/java/org/apache/camel/component/ibm/watson/stt/WatsonSpeechToTextProducer.java
b/components/camel-ibm/camel-ibm-watson-speech-to-text/src/main/java/org/apache/camel/component/ibm/watson/stt/WatsonSpeechToTextProducer.java
index c732fecfbe7f..eaed1831268d 100644
---
a/components/camel-ibm/camel-ibm-watson-speech-to-text/src/main/java/org/apache/camel/component/ibm/watson/stt/WatsonSpeechToTextProducer.java
+++
b/components/camel-ibm/camel-ibm-watson-speech-to-text/src/main/java/org/apache/camel/component/ibm/watson/stt/WatsonSpeechToTextProducer.java
@@ -35,6 +35,7 @@ import
com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.IOHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -100,15 +101,18 @@ public class WatsonSpeechToTextProducer extends
DefaultProducer {
// Get audio input from header or body
File audioFile =
exchange.getIn().getHeader(WatsonSpeechToTextConstants.AUDIO_FILE, File.class);
InputStream audioStream = null;
+ boolean ownStream = false;
if (audioFile != null) {
audioStream = new FileInputStream(audioFile);
+ ownStream = true;
} else {
audioStream = exchange.getIn().getBody(InputStream.class);
if (audioStream == null) {
File bodyFile = exchange.getIn().getBody(File.class);
if (bodyFile != null) {
audioStream = new FileInputStream(bodyFile);
+ ownStream = true;
}
}
}
@@ -130,32 +134,38 @@ public class WatsonSpeechToTextProducer extends
DefaultProducer {
LOG.trace("Recognizing audio with STT: model={}, contentType={}",
model, contentType);
- RecognizeOptions options = new RecognizeOptions.Builder()
- .audio(audioStream)
- .model(model)
- .contentType(contentType)
- .timestamps(timestamps)
- .wordConfidence(wordConfidence)
- .speakerLabels(speakerLabels)
- .build();
-
- SpeechRecognitionResults results =
stt.recognize(options).execute().getResult();
-
- // Extract transcript text
- StringBuilder transcript = new StringBuilder();
- if (results.getResults() != null && !results.getResults().isEmpty()) {
- for (SpeechRecognitionResult result : results.getResults()) {
- if (result.getAlternatives() != null &&
!result.getAlternatives().isEmpty()) {
-
transcript.append(result.getAlternatives().get(0).getTranscript());
+ try {
+ RecognizeOptions options = new RecognizeOptions.Builder()
+ .audio(audioStream)
+ .model(model)
+ .contentType(contentType)
+ .timestamps(timestamps)
+ .wordConfidence(wordConfidence)
+ .speakerLabels(speakerLabels)
+ .build();
+
+ SpeechRecognitionResults results =
stt.recognize(options).execute().getResult();
+
+ // Extract transcript text
+ StringBuilder transcript = new StringBuilder();
+ if (results.getResults() != null &&
!results.getResults().isEmpty()) {
+ for (SpeechRecognitionResult result : results.getResults()) {
+ if (result.getAlternatives() != null &&
!result.getAlternatives().isEmpty()) {
+
transcript.append(result.getAlternatives().get(0).getTranscript());
+ }
}
}
- }
- Message message = getMessageForResponse(exchange);
- message.setBody(results);
- message.setHeader(WatsonSpeechToTextConstants.TRANSCRIPT,
transcript.toString());
- message.setHeader(WatsonSpeechToTextConstants.MODEL, model);
- message.setHeader(WatsonSpeechToTextConstants.CONTENT_TYPE,
contentType);
+ Message message = getMessageForResponse(exchange);
+ message.setBody(results);
+ message.setHeader(WatsonSpeechToTextConstants.TRANSCRIPT,
transcript.toString());
+ message.setHeader(WatsonSpeechToTextConstants.MODEL, model);
+ message.setHeader(WatsonSpeechToTextConstants.CONTENT_TYPE,
contentType);
+ } finally {
+ if (ownStream) {
+ IOHelper.close(audioStream);
+ }
+ }
}
private void listModels(Exchange exchange) {