Skip to content

Commit 061e1fa

Browse files
committed
Implement new file writing operation algorithm
1 parent 4c434fc commit 061e1fa

File tree

1 file changed

+21
-16
lines changed
  • android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder

1 file changed

+21
-16
lines changed

android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/FileWriter.kt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,38 @@ internal class FileWriter(private val outputStream: DataOutputStream) {
3434
lastSkippedData: LinkedList<ByteArray>,
3535
data: ByteArray
3636
) {
37-
if (lastSkippedData.isNotEmpty()) {
38-
lastSkippedData.forEach { outputStream.write(it) }
37+
val totalSize = lastSkippedData.sumOf { it.size } + data.size
38+
val byteBuffer = ByteBuffer.allocate(totalSize).order(ByteOrder.LITTLE_ENDIAN)
39+
40+
lastSkippedData.forEach { byteArray ->
41+
byteBuffer.put(byteArray)
3942
}
43+
44+
byteBuffer.put(data)
45+
46+
outputStream.write(byteBuffer.array())
4047
lastSkippedData.clear()
41-
outputStream.write(data)
4248
}
4349

4450
fun writeDataToStream(
4551
lastSkippedData: LinkedList<FloatArray>,
4652
data: FloatArray
4753
) {
48-
if (lastSkippedData.isNotEmpty()) {
49-
lastSkippedData.forEach { floatArray ->
50-
floatArray.forEach {
51-
val bytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN)
52-
.putFloat(it).array()
53-
outputStream.write(bytes)
54-
}
54+
val totalFloats = lastSkippedData.sumOf { it.size } + data.size
55+
val totalSize = totalFloats * 4
56+
val byteBuffer = ByteBuffer.allocate(totalSize).order(ByteOrder.LITTLE_ENDIAN)
57+
58+
lastSkippedData.forEach { floatArray ->
59+
floatArray.forEach { floatValue ->
60+
byteBuffer.putFloat(floatValue)
5561
}
5662
}
57-
lastSkippedData.clear()
58-
data.forEach {
59-
val bytes =
60-
ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(it)
61-
.array()
62-
outputStream.write(bytes)
63+
data.forEach { floatValue ->
64+
byteBuffer.putFloat(floatValue)
6365
}
66+
67+
outputStream.write(byteBuffer.array())
68+
lastSkippedData.clear()
6469
}
6570

6671
}

0 commit comments

Comments
 (0)