Skip to content

Commit ee8139f

Browse files
authored
Java Sample: Fix logic for reading from a FileInputStream and other minor issues (#885)
* Fix issue #874. Properly handle shorter read buffers at the end of the WAV file. * Suppress two warnings of unused variables (they are required for code readability) * Resolve one warning of unused import
1 parent 995d20d commit ee8139f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/SpeechRecognitionSamples.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,18 @@ else if (e.getResult().getReason() == ResultReason.NoMatch) {
356356
// Push audio read from the file into the PushStream.
357357
// The audio can be pushed into the stream before, after, or during recognition
358358
// and recognition will continue as data becomes available.
359-
while( inputStream.read(readBuffer) != -1)
359+
int bytesRead;
360+
while ((bytesRead = inputStream.read(readBuffer)) != -1)
360361
{
361-
pushStream.write(readBuffer);
362+
if (bytesRead == readBuffer.length)
363+
{
364+
pushStream.write(readBuffer);
365+
}
366+
else
367+
{
368+
// Last buffer read from the WAV file is likely to have less bytes
369+
pushStream.write(Arrays.copyOfRange(readBuffer, 0, bytesRead));
370+
}
362371
}
363372

364373
pushStream.close();

samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/TranslationSamples.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//
66

77
import java.io.IOException;
8-
import java.util.ArrayList;
98
import java.util.Map;
109
import java.util.Scanner;
1110
import java.util.concurrent.ExecutionException;

samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/WavStream.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public InputStream parseWavHeader(InputStream reader) throws IOException {
9191
int formatTag = ReadUInt16(reader);
9292
int channels = ReadUInt16(reader);
9393
int samplesPerSec = (int) ReadUInt32(reader);
94+
@SuppressWarnings("unused")
9495
int avgBytesPerSec = (int) ReadUInt32(reader);
96+
@SuppressWarnings("unused")
9597
int blockAlign = ReadUInt16(reader);
9698
int bitsPerSample = ReadUInt16(reader);
9799
ThrowIfFalse(formatTag == 1, "PCM"); // PCM

0 commit comments

Comments
 (0)