語境使用ByteArrayInputStream進行的FileInputStream的
我適配現有項目的部分到GAE項目。原來的項目使用FileInputStream
和FileOutputStream
但由於GAE不接受FileOutputStream
我與ByteArrayInputStream
和ByteArrayOutputStream
替換它們。原始代碼加載了一些本地文件,我用Datastore Entities
替換了那些將這些文件的內容保存在其某個屬性中的文件。
問題
它主要似乎工作,但我得到一個ArrayIndexOutOfBoundsException
在這段代碼:
private byte[] loadKey(Entity file) {
byte[] b64encodedKey = null;
ByteArrayInputStream fis = null;
try {
fis = fileToStreamAdapter.objectToInputStreamConverter(file);
b64encodedKey = new byte[(int) fis.available()];
fis.read(b64encodedKey);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return b64encodedKey;
}
fileToStreamAdapter.objectToInputStreamConverter(file)
需要Datastore Entity
並打開其屬性之一的內容爲ByteArrayInputStream
。
原始代碼:
private byte[] loadKey(String path) {
byte[] b64encodedKey = null;
File fileKey = new File(path);
FileInputStream fis = null;
try {
fis = new FileInputStream(fileKey);
b64encodedKey = new byte[(int) fileKey.length()];
fis.read(b64encodedKey);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return b64encodedKey;
}
有我丟失的東西在FileInputStream
和ByteArrayInputStream
之間的差異可能導致這個錯誤?
什麼是異常堆棧? – 2015-03-13 13:44:07