Skip to content

Commit 9aeaf56

Browse files
committed
WIP
1 parent 4088f5c commit 9aeaf56

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/main/kotlin/edu/ba/twoDimensionalRLE/encoder/mixed/MixedEncoder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class MixedEncoder : Encoder {
132132
var totalSize = input.length()
133133
val huffmanEncoder = HuffmanEncoder()
134134
var chunks = DataChunk.readChunksFromFile(inputFile, byteArraySize, log)
135-
val huffBuffer = StringBuffer()
135+
val huffBuffer = mutableListOf<Boolean>()
136136

137137
analyzer.analyzeFile(input)
138138
analyzer.addBWTSymbolsToMapping()
@@ -158,15 +158,15 @@ class MixedEncoder : Encoder {
158158

159159
printEncodingInfo()
160160

161-
log.info("Collecting all bytes to encode with huffman encoding to build dictionary...")
161+
log.info("Collecting all bits to encode with huffman encoding to build dictionary...")
162162
//TODO improve buffer collecting
163163
chunks.forEach {
164164
for (i in HUFF_BIT_RANGE) {
165-
huffBuffer.append(it.getLineFromChunkAsStringBuffer(i))
165+
huffBuffer.addAll(it.getLineFromChunkAsBoolList(i))
166166
}
167167
}
168168

169-
log.debug("Creating byte array from ${huffBuffer.length} bit large buffer...")
169+
log.debug("Creating byte array from ${huffBuffer.size} bit large buffer...")
170170
//TODO create better mapping creation to use less ram
171171
val huffBufferArray = huffBuffer.toByteArray()
172172
val huffmanMapping = huffmanEncoder.getHuffmanMapping(256, huffBufferArray)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package edu.ba.twoDimensionalRLE.extensions
2+
3+
4+
fun List<Boolean>.toByteArray(): ByteArray {
5+
var result = ByteArray(0)
6+
7+
this.chunked(8).forEach { chunk ->
8+
var chunkValue = 0
9+
chunk.forEachIndexed { index , bit ->
10+
if (bit) {
11+
chunkValue += 2.pow(index)
12+
}
13+
}
14+
15+
result += chunkValue.toByte()
16+
}
17+
return result
18+
}

src/main/kotlin/edu/ba/twoDimensionalRLE/model/DataChunk.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ open class DataChunk(val input: ByteArray) {
5555
rleRange: IntRange,
5656
huffRange: IntRange,
5757
huffDecodedBytes: ByteArray,
58-
binRleBuffer: StringBuffer,
58+
binRleBuffer: List<Boolean>,
5959
log: Logger
6060
): List<DataChunk> {
6161
log.info("Starting to reconstruct DataChunks from parsed buffers...")
62-
var currentLinesOfChunk = mutableMapOf<Int, String>()
62+
var currentLinesOfChunk = mutableMapOf<Int, List<Boolean>>()
6363
var remainingSize = totalSize
6464

6565
log.info("Building buffer from bytes...")
@@ -79,13 +79,13 @@ open class DataChunk(val input: ByteArray) {
7979

8080
//build bin rle decoded lines
8181
binRleRange.forEach { line ->
82-
currentLinesOfChunk[line] = binRleBuffer.substring(0, currentLength)
83-
binRleBuffer.delete(0, currentLength)
82+
currentLinesOfChunk[line] = binRleBuffer.subList(0, currentLength)
83+
binRleBuffer.subList(currentLength, binRleBuffer.size)
8484
}
8585

8686
//huff decoded lines
8787
huffRange.forEach { line ->
88-
currentLinesOfChunk[line] = remainingHuffmanBuffer.substring(0, currentLength)
88+
currentLinesOfChunk[line] = remainingHuffmanBuffer.subSequence(0, currentLength)
8989
remainingHuffmanBuffer.delete(0, currentLength)
9090
}
9191

@@ -153,6 +153,18 @@ open class DataChunk(val input: ByteArray) {
153153
return bits
154154
}
155155

156+
fun getLineFromChunkAsBoolList(line: Int): List<Boolean> {
157+
val bits = mutableListOf<Boolean>()
158+
bytes.forEach { byte ->
159+
if (byte.toInt().shl(7 - line).toUByte().toInt().shr(7) == 1) {
160+
bits.add(true)
161+
} else {
162+
bits.add(false)
163+
}
164+
}
165+
return bits
166+
}
167+
156168
fun applyByteMapping(mapping: Map<Byte, Byte>): DataChunk {
157169
val result = mutableListOf<Byte>()
158170
bytes.forEach { byte ->

0 commit comments

Comments
 (0)