【发布时间】:2011-11-26 05:32:05
【问题描述】:
我需要做一个简单的加密,这样普通的最终用户就不能轻易访问一些文件。
FileInputStream 读取的文件是 html 文件、pngs、jpegs 和不同的简单文本文件(javascript、xml、...)
我现在做的是这样的:
public static byte[] toEncryptedByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output, true);
return output.toByteArray();
}
public synchronized static final int copy(final InputStream input, final OutputStream output, final boolean modify) throws IOException {
if (input == null || output == null) {
throw new IOException("One stream is null!");
}
byte[] mBuffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n;
while ((n = input.read(mBuffer)) != -1) {
if (modify) {
for ( int i = 0 ; i < n ; i++ ) {
mBuffer[i] = (byte) ~mBuffer[i]; // byte manipulation
}
}
output.write(mBuffer, 0, n);
output.flush();
count += n;
}
mBuffer = null;
return count;
}
内存占用很大,因为我的内存中有完整的字节数组(我们谈论的是内存中 >2mb 的位图)。
我想我可以简单地扩展 FileInputStream 类并在读取文件内容时进行字节操作。这将减少内存使用量,因为我可以使用 Bitmap.decodeStream(inputstream) 而不是创建字节数组来从中获取位图......但在这里我完全被卡住了。 read() 和 readBytes(...) 方法是原生的,所以我不能覆盖它们。
请在我的黑暗中传播光明...
【问题讨论】:
-
只有 final 方法不能被覆盖。
-
read() 和 readBytes(...) 方法可能是原生的,但调用
super并没有过时,我上次检查过。 -
@Dan 你能覆盖原生方法吗?我不这么认为...
-
是的,这是可能的,因为我编写的代码就是这样做的。
标签: java android inputstream fileinputstream