针对嵌入式系统进行开发时,面临在桌面环境中开发时所面临的许多挑战。我目前面临的挑战是将大文件(可能10-100MB)转换为字节数组,同时牢记我有限的资源(内存)。两个implemetation我一直使用造成可怕的将大文件转换为Android上的字节数组
java.lang.OutOfMemoryError
我开始这个implemeting自己:
/** Converts the given File to an array of bits. */
private byte[] fileToBytes(File file) {
InputStream input_stream = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[16384]; // 16K
int bytes_read;
while ((bytes_read = input_stream.read(data,0,data.length)) != -1) {
buffer.write(data, 0, bytes_read);
}
input_stream.close();
return buffer.toByteArray();
}
然后决定使用久经考验的Apache公地IO处理这对我来说,但同样的错误。这是可以在Android等移动环境中完成的事情,还是我运气不好?