Skip to content

Commit

Permalink
Change NbtCompression.decompress() and .compress() to use parameter i…
Browse files Browse the repository at this point in the history
…nstead of receivers
  • Loading branch information
BenWoodworth committed Sep 17, 2021
1 parent 33299d3 commit 2bb8d97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
17 changes: 9 additions & 8 deletions src/commonMain/kotlin/NbtCompression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,31 @@ import net.benwoodworth.knbt.internal.asZlibSource
import okio.BufferedSource
import okio.Sink
import okio.Source
import okio.buffer

public abstract class NbtCompression private constructor() {
internal abstract fun Source.decompress(): Source
internal abstract fun Sink.compress(level: Int?): Sink
internal abstract fun decompress(source: Source): Source
internal abstract fun compress(sink: Sink, level: Int?): Sink

public companion object;

public object None : NbtCompression() {
override fun Source.decompress(): Source = this
override fun Sink.compress(level: Int?): Sink = this
override fun decompress(source: Source): Source = source
override fun compress(sink: Sink, level: Int?): Sink = sink

override fun toString(): String = "None"
}

public object Gzip : NbtCompression() {
override fun Source.decompress(): Source = this.asGzipSource()
override fun Sink.compress(level: Int?): Sink = this.asGzipSink(level ?: -1)
override fun decompress(source: Source): Source = source.asGzipSource()
override fun compress(sink: Sink, level: Int?): Sink = sink.asGzipSink(level ?: -1)

override fun toString(): String = "Gzip"
}

public object Zlib : NbtCompression() {
override fun Source.decompress(): Source = this.asZlibSource()
override fun Sink.compress(level: Int?): Sink = this.asZlibSink(level ?: -1)
override fun decompress(source: Source): Source = source.asZlibSource()
override fun compress(sink: Sink, level: Int?): Sink = sink.asZlibSink(level ?: -1)

override fun toString(): String = "Zlib"
}
Expand Down
6 changes: 3 additions & 3 deletions src/commonMain/kotlin/internal/BinaryNbtReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ internal class BinaryNbtReader(nbt: Nbt, source: Source) : NbtReader, Closeable
throw NbtDecodingException("Expected compression to be $compression, but was $detectedCompression")
}

this.source = with(compression) {
variant.getBinarySource(nonClosingSource.decompress().buffer())
}
this.source = variant.getBinarySource(
compression.decompress(nonClosingSource).buffer()
)
}

override fun close(): Unit = source.close()
Expand Down
8 changes: 3 additions & 5 deletions src/commonMain/kotlin/internal/BinaryNbtWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ internal class BinaryNbtWriter(nbt: Nbt, sink: Sink) : NbtWriter, Closeable {
private var compoundNesting = 0
private var wroteRootEntry = false

private val sink: BinarySink = with(nbt.configuration.compression) {
nbt.configuration.variant.getBinarySink(
NonClosingSink(sink).compress(nbt.configuration.compressionLevel).buffer()
)
}
private val sink: BinarySink = nbt.configuration.variant.getBinarySink(
nbt.configuration.compression.compress(NonClosingSink(sink), nbt.configuration.compressionLevel).buffer()
)

override fun close(): Unit = sink.close()

Expand Down

0 comments on commit 2bb8d97

Please sign in to comment.