我需要进行一种简单的加密,以使普通最终用户无法轻松访问某些文件。
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(...)
机的,所以我不能覆盖它们。
请在我的黑暗中传播光明...