Skip to content

Commit

Permalink
Java Sample: Fix logic for reading from a FileInputStream and other m…
Browse files Browse the repository at this point in the history
…inor 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
  • Loading branch information
dargilco authored Dec 4, 2020
1 parent 995d20d commit ee8139f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,18 @@ else if (e.getResult().getReason() == ResultReason.NoMatch) {
// Push audio read from the file into the PushStream.
// The audio can be pushed into the stream before, after, or during recognition
// and recognition will continue as data becomes available.
while( inputStream.read(readBuffer) != -1)
int bytesRead;
while ((bytesRead = inputStream.read(readBuffer)) != -1)
{
pushStream.write(readBuffer);
if (bytesRead == readBuffer.length)
{
pushStream.write(readBuffer);
}
else
{
// Last buffer read from the WAV file is likely to have less bytes
pushStream.write(Arrays.copyOfRange(readBuffer, 0, bytesRead));
}
}

pushStream.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public InputStream parseWavHeader(InputStream reader) throws IOException {
int formatTag = ReadUInt16(reader);
int channels = ReadUInt16(reader);
int samplesPerSec = (int) ReadUInt32(reader);
@SuppressWarnings("unused")
int avgBytesPerSec = (int) ReadUInt32(reader);
@SuppressWarnings("unused")
int blockAlign = ReadUInt16(reader);
int bitsPerSample = ReadUInt16(reader);
ThrowIfFalse(formatTag == 1, "PCM"); // PCM
Expand Down

0 comments on commit ee8139f

Please sign in to comment.