Show buffer copy

This commit is contained in:
J-Jamet
2021-02-06 20:57:28 +01:00
parent d13aa047d5
commit 2f15d6c9f2
3 changed files with 27 additions and 13 deletions

View File

@@ -124,11 +124,9 @@ class BinaryAttachment : Parcelable {
if (!isCompressed) {
// Encrypt the new gzipped temp file
val fileBinaryCompress = File(concreteDataFile.parent, concreteDataFile.name + "_temp")
GZIPOutputStream(buildOutputStream(fileBinaryCompress, cipherKey)).use { outputStream ->
getInputDataStream(cipherKey).use { inputStream ->
inputStream.readAllBytes(bufferSize) { buffer ->
outputStream.write(buffer)
}
GZIPOutputStream(buildOutputStream(fileBinaryCompress, cipherKey)).use { outputStream ->
inputStream.copyTo(outputStream, bufferSize)
}
}
// Remove ungzip file
@@ -148,11 +146,9 @@ class BinaryAttachment : Parcelable {
if (isCompressed) {
// Encrypt the new ungzipped temp file
val fileBinaryDecompress = File(concreteDataFile.parent, concreteDataFile.name + "_temp")
buildOutputStream(fileBinaryDecompress, cipherKey).use { outputStream ->
getUnGzipInputDataStream(cipherKey).use { inputStream ->
inputStream.readAllBytes(bufferSize) { buffer ->
outputStream.write(buffer)
}
buildOutputStream(fileBinaryDecompress, cipherKey).use { outputStream ->
inputStream.copyTo(outputStream, bufferSize)
}
}
// Remove gzip file
@@ -225,6 +221,12 @@ class BinaryAttachment : Parcelable {
init {
length = 0
}
override fun beforeWrite(n: Int) {
super.beforeWrite(n)
length = byteCount
}
override fun close() {
super.close()
length = byteCount

View File

@@ -400,9 +400,9 @@ class AttachmentFileNotificationService: LockNotificationService() {
var dataUploaded = 0L
val fileSize = contentResolver.openFileDescriptor(attachmentFromDownloadUri, "r")?.statSize ?: 0
UriUtil.getUriInputStream(contentResolver, attachmentFromDownloadUri)?.let { inputStream ->
BufferedInputStream(inputStream).use { attachmentBufferedInputStream ->
Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
binaryAttachment.getGzipOutputDataStream(binaryCipherKey).use { outputStream ->
BufferedInputStream(inputStream).use { attachmentBufferedInputStream ->
attachmentBufferedInputStream.readAllBytes(bufferSize) { buffer ->
outputStream.write(buffer)
dataUploaded += buffer.size

View File

@@ -32,7 +32,19 @@ import java.util.*
*/
@Throws(IOException::class)
fun InputStream.readAllBytes(bufferSize: Int, readBytes: (bytesRead: ByteArray) -> Unit) {
readBytes.invoke(readBytes(bufferSize))
val buffer = ByteArray(bufferSize)
var read = 0
while (read != -1) {
read = this.read(buffer, 0, buffer.size)
if (read != -1) {
val optimizedBuffer: ByteArray = if (buffer.size == read) {
buffer
} else {
buffer.copyOf(read)
}
readBytes.invoke(optimizedBuffer)
}
}
}
/**