Skip to content

Commit

Permalink
fix Compression expect/actual class and function declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
LeHaine committed Dec 2, 2024
1 parent 2933f88 commit 8a26c3d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Compression {
* @param input uncompressed data.
* @return compressed data ByteBuffer.
*/
suspend fun compress(input: ByteBuffer): ByteBuffer = ByteBuffer(compress(input.toArray()))
suspend fun compress(input: ByteBuffer): ByteBuffer

/**
* Compress a block of data
Expand All @@ -32,7 +32,7 @@ interface Compression {
* @param input compressed data
* @return decompressed data ByteBuffer.
*/
suspend fun decompress(input: ByteBuffer): ByteBuffer = ByteBuffer(decompress(input.toArray()))
suspend fun decompress(input: ByteBuffer): ByteBuffer

/**
* Decompress a block of data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.littlekt.file.compression

import com.littlekt.file.ByteBuffer

/**
* An implementation of the gzip algorithm.
*
* @author Colton Daily
* @date 12/2/2024
*/
expect class CompressionGZIP : Compression {
override suspend fun compress(input: ByteBuffer): ByteBuffer

override suspend fun compress(input: ByteArray): ByteArray

override suspend fun decompress(input: ByteBuffer): ByteBuffer

override suspend fun decompress(input: ByteArray): ByteArray

companion object {
operator fun invoke(): CompressionGZIP
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.littlekt.file.compression

import com.littlekt.file.ByteBuffer
import kotlinx.coroutines.await
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
Expand All @@ -11,8 +12,10 @@ import org.khronos.webgl.get
* @date 12/2/2024
*/
actual class CompressionGZIP : Compression {
actual override suspend fun compress(input: ByteBuffer): ByteBuffer =
ByteBuffer(compress(input.toArray()))

override suspend fun compress(input: ByteArray): ByteArray {
actual override suspend fun compress(input: ByteArray): ByteArray {
val uint8Array = Uint8Array(input.toTypedArray())
val readableStream =
ReadableStream(
Expand All @@ -38,7 +41,10 @@ actual class CompressionGZIP : Compression {
return ByteArray(output.length) { output[it] }
}

override suspend fun decompress(input: ByteArray): ByteArray {
actual override suspend fun decompress(input: ByteBuffer): ByteBuffer =
ByteBuffer(decompress(input.toArray()))

actual override suspend fun decompress(input: ByteArray): ByteArray {
val uint8Array = Uint8Array(input.toTypedArray())
val readableStream =
ReadableStream(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.littlekt.file.compression

import com.littlekt.file.ByteBuffer
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPInputStream
Expand All @@ -14,8 +15,10 @@ import kotlinx.coroutines.withContext
* @date 12/2/2024
*/
actual class CompressionGZIP : Compression {
actual override suspend fun compress(input: ByteBuffer): ByteBuffer =
ByteBuffer(compress(input.toArray()))

override suspend fun compress(input: ByteArray): ByteArray {
actual override suspend fun compress(input: ByteArray): ByteArray {
val compressedData =
withContext(Dispatchers.IO) {
ByteArrayOutputStream(input.size).use { outputStream ->
Expand All @@ -26,7 +29,10 @@ actual class CompressionGZIP : Compression {
return compressedData
}

override suspend fun decompress(input: ByteArray): ByteArray {
actual override suspend fun decompress(input: ByteBuffer): ByteBuffer =
ByteBuffer(decompress(input.toArray()))

actual override suspend fun decompress(input: ByteArray): ByteArray {
val uncompressData =
withContext(Dispatchers.IO) {
ByteArrayInputStream(input).use { inputStream ->
Expand Down

0 comments on commit 8a26c3d

Please sign in to comment.