Reputation: 45
I'm working on an audio recording system. How make array short[] from input stream in java
Upvotes: 3
Views: 2439
Reputation: 12672
First, use InputStream.read(byte[] buffer)
to store your data into a byte array. Then use something like this to convert it into SHORT[].
byte[] bBuffer; // your buffer containing your byte[] data
short[] sBuffer;
ByteBuffer.wrap(bBuffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(sBuffer);
Although unless you really need a short[], I would do processing using bytes on the fly so it's more optimized.
To go the other way, you can just use put()
into the short buffer representation of the ByteBuffer. So, something like:
byte[] bBuffer;
short[] sBuffer; // your buffer containing your byte[] data
yourByteBuffer.asShortBuffer().put(sBuffer);
Upvotes: 3
Reputation: 1007
Well... If you just want to read from the InputStream and store it on a byte[] variable, you only have to use the InputStream's read method:
InputStream inputStream;
byte[] bytes;
...
inputStream.read(bytes);
But, as you said you're working on an audio record system, I suggest you to look at the AudioRecord Class. You can easily use it to record sound and read the data.
Upvotes: 0