我将文件分成多个块,每个块都编码为Base64。但是我在encodeToString
上收到OutOfMemory错误。我的代码如下:
try {
in = new FileInputStream(zipEntity.zip_file);
long bytesLeft = in.available();
long chunkOffset = 0;
int i = 0;
while (bytesLeft > 0) {
i++;
// TODO: what happens if int MAX_VALUE exceeded?
long maxChunkSize = chunkSizeBytes > 0 ? chunkSizeBytes : bytesLeft;
byte[] buffer = new byte[(int) Math.min(bytesLeft, maxChunkSize)];
int read = in.read(buffer, 0, buffer.length);
bytesLeft -= read;
Log.e("", "===== " + i + ", BYTES LEFT === " + bytesLeft);
boolean isLast = bytesLeft == 0;
chunks.add(new FileChunkEntity(Base64.encodeToString(buffer, Base64.DEFAULT), chunkOffset + "", isLast));
i++;
chunkOffset += buffer.length;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
此行出现错误:
chunks.add(new FileChunkEntity(Base64.encodeToString(buffer, Base64.DEFAULT), chunkOffset + "", isLast));
我已经尝试过使用Base64OutputStream
进行相同的操作,但是从Base64.encodeToString
获得的字符串与outputStream.toString
不匹配。
当我尝试outputStream.toByteArray)
时,没有出现内存不足错误,但是new String(outputStream.toByteArray)
与Base64.encodeToString()
不匹配