可以fileInputStream.阅读读取奇数字节吗?
我有一种读取16位波文件的方法:
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[8*Number];
do {
readSize = in.read(bytes, 0, 8*Number);
...
} while (...);
然后,我需要将bytes []
中的每个字节转换为简短。也就是说,我最好确保readsize
偶数!我知道readsize
不一定等于8*数字
,并且可以更小...
是readsize
以某种方式保证的这种奇偶校验通过读取方法,还是我必须在in.Read
行之后检查?
edit ,
因为我还需要读取
等于8*数字
,而不仅仅是我使用此解决方法再一次,直到我得到所有我需要的数据:
FileInputStream in = new FileInputStream(f);
int i, readSize, chunkPerBar = 8*Number;
byte[] bytes = new byte[chunkPerBar];
double mean;
do {
// If I cannot read *exactly* chunkPerBar bytes, keep adding or exit
readSize = in.read(bytes, 0, chunkPerBar);
while ((readSize < chunkPerBar) && (in.available() > 0))
readSize += in.read(bytes, readSize, chunkPerBar - readSize);
if (readSize < chunkPerBar) break; // Discard the last chunk if < chunkPerBar
mean = 0;
for (i = 0; i < chunkPerBar / 2; i++) {
aux = (short)( ( bytes[2*i] & 0xff )|( bytes[2*i+1] << 8 ) );
mean += aux * aux;
}
waveform.drawNewBar(mean/chunkPerBar);
} while (in.available() > 0);
in.close();
} catch {...}
I have a method that reads a 16 bit WAVE file, like this:
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[8*Number];
do {
readSize = in.read(bytes, 0, 8*Number);
...
} while (...);
I then need to convert each pair of bytes in bytes[]
into a short. That is, I better be sure that readSize
is even! I know that readSize
is not necessarily equal to 8*Number
, and that it can be smaller...
Is this parity of readSize
somehow guaranteed by the read method, or I must check after the in.read
line?
EDIT
Since I also need that readSize
to be equal to 8*Number
and not just even, I use this workaround, where I am reading once and again until I get all the data I need:
FileInputStream in = new FileInputStream(f);
int i, readSize, chunkPerBar = 8*Number;
byte[] bytes = new byte[chunkPerBar];
double mean;
do {
// If I cannot read *exactly* chunkPerBar bytes, keep adding or exit
readSize = in.read(bytes, 0, chunkPerBar);
while ((readSize < chunkPerBar) && (in.available() > 0))
readSize += in.read(bytes, readSize, chunkPerBar - readSize);
if (readSize < chunkPerBar) break; // Discard the last chunk if < chunkPerBar
mean = 0;
for (i = 0; i < chunkPerBar / 2; i++) {
aux = (short)( ( bytes[2*i] & 0xff )|( bytes[2*i+1] << 8 ) );
mean += aux * aux;
}
waveform.drawNewBar(mean/chunkPerBar);
} while (in.available() > 0);
in.close();
} catch {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论