我需要做一个简单的加密,以便普通用户无法轻易访问某些文件。
由FileInputStream读取的文件包括HTML文件、PNG、JPEG和不同的简单文本文件(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(...)
方法是本地方法,因此我无法覆盖它们。
请在我的黑暗中传递光明...
super
并没有过时。 - Perception