java 从 InputStream 读取时出现 IOException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1273300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOException while reading from InputStream
提问by DJayC
I'm running into a strange problem while reading from an InputStreamon the Android platform. I'm not sure if this is an Android specific issue, or something I'm doing wrong in general.
我在从Android 平台上的InputStream读取时遇到了一个奇怪的问题。我不确定这是 Android 特定的问题,还是我一般做错的事情。
The only thing that is Android specific is this call:
唯一特定于 Android 的是这个调用:
InputStream is = getResources().openRawResource(R.raw.myfile);
This returns an InputStreamfor a file from the Android assets. Anyways, here's where I run into the issue:
这将从 Android 资产返回一个文件的InputStream。无论如何,这就是我遇到问题的地方:
bytes[] buffer = new bytes[2];
is.read(buffer);
When the read() executes it throws an IOException. The weird thing is that if I do two sequential single byte reads (or any number of single byte reads), there is no exception. In example, this works:
当 read() 执行时,它会抛出一个IOException。奇怪的是,如果我进行两次连续的单字节读取(或任意数量的单字节读取),也不例外。例如,这有效:
byte buffer;
buffer = (byte)buffer.read();
buffer = (byte)buffer.read();
Any idea why two sequential single byte reads work but one call to read both at once throws an exception? The InputStreamseems fine... is.available()returns over a million bytes (as it should).
知道为什么两个连续的单字节读取工作但一次读取两个的调用会引发异常吗?该InputStream中似乎罚款......is.available()超过一百万字节的回报(因为它应该)。
Stack trace shows these lines just before the InputStream.read():
堆栈跟踪在 之前显示这些行InputStream.read():
java.io.IOException
at android.content.res.AssetManager.readAsset(Native Method)
at android.content.res.AssetManager.access0(AssetManager.java:36)
at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:542)
Changing the buffer size to a single byte still throws the error. It looks like the exception is only raised when reading into a byte array.
将缓冲区大小更改为单个字节仍然会引发错误。看起来只有在读入字节数组时才会引发异常。
If I truncate the file to 100,000 bytes (file is: 1,917,408 bytes originally) it works fine. Is there a problem with files over a certain size?
如果我将文件截断为 100,000 字节(文件最初为:1,917,408 字节),它可以正常工作。超过一定大小的文件是否有问题?
Any help is appreciated!
Thanks!
任何帮助表示赞赏!
谢谢!
回答by CommonsWare
(my post to android-developers isn't showing up, so I'll try reposting it here)
(我发给 android-developers 的帖子没有显示出来,所以我会尝试在此处重新发布)
IIRC, this problem comes from trying to access files that were compressed as part of building the APK.
IIRC,此问题来自尝试访问作为构建 APK 的一部分而压缩的文件。
Hence, to work around the issue, give it a file extension that won't be compressed. I forget the list of what extensions are skipped, but file types known to already be compressed (e.g., mp3, jpg) may work.
因此,要解决此问题,请为其指定一个不会被压缩的文件扩展名。我忘记了跳过哪些扩展名的列表,但已知已被压缩的文件类型(例如 mp3、jpg)可能会起作用。
回答by mmathieum
Changing the file extension to .mp3to avoid the file compression does work, but the APK of the app is much bigger (in my case 2.3 MB instead of 0.99 MB).
将文件扩展名更改为.mp3以避免文件压缩确实有效,但应用程序的 APK 更大(在我的情况下为 2.3 MB 而不是 0.99 MB)。
Is there any other way to avoid this issue ?
有没有其他方法可以避免这个问题?
Here is my answer: Load files bigger than 1M from assets folder
这是我的答案: 从资产文件夹加载大于 1M 的文件
回答by Marcus
You can compress the file for yourself with GZIP and unpack it with GZIPInputStream class.
您可以使用 GZIP 为自己压缩文件,并使用 GZIPInputStream 类将其解压缩。
http://developer.android.com/reference/java/util/zip/GZIPInputStream.html
http://developer.android.com/reference/java/util/zip/GZIPInputStream.html
回答by vol
You are correct, in that there is a certain size limit for extracting files. You may wish to split larger files into 1MB pieces, and have a method by which you know which files are made of which pieces, stitching them back together again when your app runs.
您是对的,因为提取文件有一定的大小限制。您可能希望将较大的文件分成 1MB 的部分,并有一种方法可以让您知道哪些文件由哪些部分组成,并在您的应用程序运行时将它们重新拼接在一起。

