IOException异常,而从InputStream中读取 [英] IOException while reading from InputStream

查看:1467
本文介绍了IOException异常,而从InputStream中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,同时从的InputStream 的阅读在Android平台上。我不知道这是否是一个Android的具体问题,什么我做错了一般。

I'm running into a strange problem while reading from an InputStream on the Android platform. I'm not sure if this is an Android specific issue, or something I'm doing wrong in general.

这是Android的特定的唯一的事情是这样的电话:

The only thing that is Android specific is this call:

InputStream is = getResources().openRawResource(R.raw.myfile);

这将返回的的InputStream 的用于从Android资产的文件。不管怎么说,这里就是我碰到的问题:

This returns an InputStream for a file from the Android assets. Anyways, here's where I run into the issue:

bytes[] buffer = new bytes[2];
is.read(buffer);

在读()执行它抛出的 IOException异常的。奇怪的是,如果我做两个连续的单字节读取(或任意数量的单字节读取),没有例外。在例子中,这个作品:

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:

byte buffer;
buffer = (byte)buffer.read();
buffer = (byte)buffer.read();

任何想法,为什么两个连续的单字节读取工作,但一个电话读两个同时抛出一个异常?在的InputStream 的似乎不错... is.available()收益超过一百万个字节(因为它应该)。

Any idea why two sequential single byte reads work but one call to read both at once throws an exception? The InputStream seems fine... is.available() returns over a million bytes (as it should).

堆栈跟踪显示这些行之前的 InputStream.read()

Stack trace shows these lines just before the InputStream.read():

java.io.IOException
at android.content.res.AssetManager.readAsset(Native Method)
at android.content.res.AssetManager.access$800(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.

如果我截断文件到100,000字节(文件为:1917408字节原)能正常工作。有没有超过一定大小的文件有问题?

任何帮助是AP preciated!
谢谢!

Any help is appreciated!
Thanks!

推荐答案

(我的职位,Android的开发人员没有显示出来,所以我会尽力在这里重新发布)

(my post to android-developers isn't showing up, so I'll try reposting it here)

IIRC,这个问题是来自试图访问是COM pressed文件作为构建APK的一部分。

IIRC, this problem comes from trying to access files that were compressed as part of building the APK.

因此​​,要解决这个问题,给它一个文件扩展名,将不会COM pressed。我忘了什么扩展会被跳过的名单,但已知文件类型已经融为一体pressed(例如,MP3,JPG格式),可正常工作。

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.

这篇关于IOException异常,而从InputStream中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆