简体   繁体   English

BufferedInputStream.read(byte [])引起问题。 有人以前有这个问题吗?

[英]BufferedInputStream.read(byte[]) Causes problems. Anyone have this problem before?

I've written a Java program that downloads audio files for me and I'm using BufferedInputStream. 我已经编写了一个Java程序来为我下载音频文件,并且我正在使用BufferedInputStream。 The read() function works fine but is really slow so I've tried using the overloaded version using byte[]. read()函数工作正常,但速度确实很慢,因此我尝试使用byte []使用重载版本。

For some reason, the audio becomes lossy and strange after download. 由于某些原因,下载后音频会变得有损和奇怪。 I'm not totally sure what I'm doing wrong so any help is appreciated! 我不确定自己在做什么错,因此我们将不胜感激! Here's the simplified, sloppy version of the code. 这是代码的简化,草率版本。

BufferedInputStream bin = new BufferedInputStream((new URL(url)).openConnection().getInputStream());
File file = new File(fileName);
FileOutputStream fop = new FileOutputStream(file);
int rd = bin.read();
while(rd != -1)
{
     fop.write(rd);
     rd = bin.read();
}

Remember that when you read in bytes, read will return the number of bytes that were actually received. 请记住,以字节为单位读取时,read将返回实际接收的字节数。 You need to pass that count to write because you may have extraneous data in your byte array. 您需要传递该计数以进行写入,因为字节数组中可能有多余的数据。 This is especially true in the last block of data. 在最后一个数据块中尤其如此。

the read method you're using actually returns a byte inside that int it returns, which means that only 8 bits of the 32 bits in your variable "rd" are actually used. 您正在使用的read方法实际上在返回的int中返回一个字节,这意味着变量“ rd”中的32位中仅实际使用了8位。

the method write states that it takes an int as argument, but writes "the specified byte to this file output stream." 方法write声明它以int作为参数,但是将“指定的字节写入此文件输出流”。 which means you should be ok.. but i'm thinking you're not D: 这意味着你应该没事..但是我在想你不是D:

try using the read-method that takes a byte-array as argument instead, and then write that byte-array to your output stream. 请尝试使用以字节数组作为参数的读取方法,然后将该字节数组写入输出流。

however, be aware that this other read-method, the one that takes a byte array as argument, it also returns an int. 但是,请注意,另一种读取方法(以字节数组为参数的读取方法)也会返回一个int值。 but unlike the method you're using in your example (which returns one of the bytes from the input data), this other read-method returns the number of bytes that was read. 但是与示例中使用的方法不同(该方法从输入数据中返回一个字节),该另一种读取方法返回已读取的字节数。 this isn't necessary the number of bytes that your byte array can contain. 这不是您的字节数组可以包含的字节数。 in that case, you must make sure that you don't write more bytes to the output stream than was actually read from that last read operation. 在这种情况下,必须确保向输出流中写入的字节数不超过从上次读取操作实际读取的字节数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用BufferedInputStream.read()获得真实的字节顺序? - How to get true byte order with BufferedInputStream.read()? BufferedInputStream.read(byte [] b,int off,int len)中的off参数 - The off parameter in BufferedInputStream.read(byte[] b, int off, int len) Can BufferedInputStream.read(byte [] b,int off,int len)是否会返回0? 是否有重要的,破坏的InputStream可能会导致这种情况? - Can BufferedInputStream.read(byte[] b, int off, int len) ever return 0? Are there significant, broken InputStreams that might cause this? 客户端的BufferedInputStream.read()while循环永不停止 - Client's BufferedInputStream.read() while loop never stops 为什么我的BufferedInputStream.read()无法接收-1? - Why can't my BufferedInputStream.read() receive -1? 不一致的BufferedInputStream read(byte [])行为 - inconsistent BufferedInputStream read(byte[]) behaviour 什么时候使用read()或read(byte [])或BufferedInputStream? - When to use read() or read(byte[]) or BufferedInputStream? Java中InputStream.read(byte b)和BufferedInputStream的区别 - Difference between InputStream.read(byte b) and BufferedInputStream in Java 从BufferedInputStream读取byte [] - Reading byte[] from BufferedInputStream 为什么使用 BufferedInputStream 比使用 FileInputStream 更快地逐字节读取文件? - Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM