[英]use FileInputStream and FileOutputStream as a buffer
我想将硬盘驱动器用作音频信号的缓冲区。 我的想法是只将字节形式的样本写入文件,并通过另一个线程读取它们。 但是,我不得不FIS.read(byte[]);
返回0,给我一个空的缓冲区。
这里有什么问题?
这是我写字节的操作:
try {
bufferOS.write(audioChunk);
bufferOS.flush();
} catch (IOException ex) {
//...
}
这就是我的读者所做的:
byte audioChunk[] = new byte[bufferSize];
int readBufferSize;
int freeBufferSize = line.available(); // line = audioline, available returns free space in buffer
try {
readBufferSize = bufferIS.read(audioChunk,freeBufferSize, 0);
} catch(IOException e) {
//...
}
我用相同的文件创建bufferOS
和bufferIS
,两者都可以工作。
编写器工作,文件被创建,并且其中包含正确的数据。
但是bufferIS.read();
-call始终返回0
。
fileInputStream
通过buffer.available();
返回正确的可用字节buffer.available();
以及诸如freeBufferSize
和audioChunk.length
类的参数正确。
在Windows中的同一文件上运行FileInputStream
和FileOutputStream
是否存在问题?
您正在以错误的顺序将参数传递给read
调用,它应该是:
readBufferSize = bufferIS.read(audioChunk, 0, freeBufferSize);
现在,您要传递freeBufferSize
作为存储read
调用结果的偏移量,并将0
作为要读取的最大字节数。 毫不奇怪,如果您告诉read
调用最多读取零字节,则它返回已读取零字节。
Javadoc:
* @param b the buffer into which the data is read. * @param off the start offset in array <code>b</code> * at which the data is written. * @param len the maximum number of bytes to read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached.
public abstract class InputStream implements Closeable {
// ....
public int read(byte b[], int off, int len) throws IOException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.