Skip to content

Commit d8f6a3b

Browse files
committed
Format
1 parent 425dc7a commit d8f6a3b

File tree

18 files changed

+209
-274
lines changed

18 files changed

+209
-274
lines changed

zstd-kmp-okio/src/commonMain/kotlin/com/squareup/zstd/okio/ZstdCompressSink.kt

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ import okio.use
3131

3232
/**
3333
* This stages all written bytes to [inputBuffer], and then emits to [sink] on these triggers:
34-
*
35-
* * When [Buffer.completeSegmentByteCount] is greater than 0.
36-
* * On [flush].
37-
* * On [close].
34+
* * When [Buffer.completeSegmentByteCount] is greater than 0.
35+
* * On [flush].
36+
* * On [close].
3837
*
3938
* This emits directly to [BufferedSink.buffer]. To avoid that from exhausting memory, this always
4039
* calls [BufferedSink.emitCompleteSegments] after writing.
4140
*/
42-
internal class ZstdCompressSink internal constructor(
41+
internal class ZstdCompressSink
42+
internal constructor(
4343
/** The destination for our compressed data. We output directly to this sink's buffer. */
4444
@JvmField val sink: BufferedSink,
4545
private val compressor: ZstdCompressor,
@@ -76,23 +76,21 @@ internal class ZstdCompressSink internal constructor(
7676
if (closed) return
7777
closed = true
7878

79-
sink.use {
80-
compressor.use {
81-
compress(ZSTD_e_end)
82-
}
83-
}
79+
sink.use { compressor.use { compress(ZSTD_e_end) } }
8480
}
8581

8682
private fun compress(mode: Int) {
8783
// Decide how many bytes to write immediately.
88-
var inputRemaining = when (mode) {
89-
ZSTD_e_continue -> {
90-
inputBuffer.completeSegmentByteCount()
91-
.also { if (it == 0L) return@compress } // No bytes to write immediately.
92-
}
84+
var inputRemaining =
85+
when (mode) {
86+
ZSTD_e_continue -> {
87+
inputBuffer.completeSegmentByteCount().also {
88+
if (it == 0L) return@compress
89+
} // No bytes to write immediately.
90+
}
9391

94-
else -> inputBuffer.size
95-
}
92+
else -> inputBuffer.size
93+
}
9694

9795
do {
9896
var result: Long
@@ -104,29 +102,31 @@ internal class ZstdCompressSink internal constructor(
104102
// Compress some input. This might not produce any output!
105103
inputBuffer.readUnsafe(inputCursor).use { inputCursor ->
106104
inputCursor.next()
107-
result = compressor.compressStream2(
108-
outputByteArray = outputCursor.data!!,
109-
outputEnd = outputCursor.end,
110-
outputStart = outputCursor.start,
111-
inputByteArray = inputCursor.data!!,
112-
inputEnd = inputCursor.end,
113-
inputStart = inputCursor.start,
114-
mode = mode,
115-
)
105+
result =
106+
compressor.compressStream2(
107+
outputByteArray = outputCursor.data!!,
108+
outputEnd = outputCursor.end,
109+
outputStart = outputCursor.start,
110+
inputByteArray = inputCursor.data!!,
111+
inputEnd = inputCursor.end,
112+
inputStart = inputCursor.start,
113+
mode = mode,
114+
)
116115
}
117116
inputRemaining -= compressor.inputBytesProcessed
118117
inputBuffer.skip(compressor.inputBytesProcessed.toLong())
119118
} else {
120119
// No more input, but possibly more output.
121-
result = compressor.compressStream2(
122-
outputByteArray = outputCursor.data!!,
123-
outputEnd = outputCursor.end,
124-
outputStart = outputCursor.start,
125-
inputByteArray = emptyByteArray,
126-
inputEnd = 0,
127-
inputStart = 0,
128-
mode = mode,
129-
)
120+
result =
121+
compressor.compressStream2(
122+
outputByteArray = outputCursor.data!!,
123+
outputEnd = outputCursor.end,
124+
outputStart = outputCursor.start,
125+
inputByteArray = emptyByteArray,
126+
inputEnd = 0,
127+
inputStart = 0,
128+
mode = mode,
129+
)
130130
}
131131

132132
outputCursor.resizeBuffer(outputSizeBefore + compressor.outputBytesProcessed)
@@ -137,10 +137,11 @@ internal class ZstdCompressSink internal constructor(
137137
throw IOException("zstd compress failed: $errorName")
138138
}
139139

140-
val finished = when (mode) {
141-
ZSTD_e_continue -> inputRemaining == 0L
142-
else -> inputRemaining == 0L && result == 0L
143-
}
140+
val finished =
141+
when (mode) {
142+
ZSTD_e_continue -> inputRemaining == 0L
143+
else -> inputRemaining == 0L && result == 0L
144+
}
144145
} while (!finished)
145146
}
146147

zstd-kmp-okio/src/commonMain/kotlin/com/squareup/zstd/okio/ZstdDecompressSource.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import okio.use
2929

3030
/**
3131
* This satisfies reads with the following process:
32-
*
33-
* 1. Decompresses at least 1 byte from [source] into [outputBuffer].
34-
* 2. Serves the read request from [outputBuffer].
32+
* 1. Decompresses at least 1 byte from [source] into [outputBuffer].
33+
* 2. Serves the read request from [outputBuffer].
3534
*
3635
* Each attempt to decompress returns 0 if the frame is complete, and non-zero otherwise. This
3736
* tracks that most recent result so it can throw [EOFException] if the source is exhausted
3837
* mid-frame.
3938
*/
40-
internal class ZstdDecompressSource internal constructor(
39+
internal class ZstdDecompressSource
40+
internal constructor(
4141
@JvmField val source: BufferedSource,
4242
private val decompressor: ZstdDecompressor,
4343
) : Source {
@@ -50,8 +50,7 @@ internal class ZstdDecompressSource internal constructor(
5050

5151
private var lastDecompressResult = 0L
5252

53-
@JvmField
54-
var closed = false
53+
@JvmField var closed = false
5554

5655
override fun read(sink: Buffer, byteCount: Long): Long {
5756
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
@@ -68,10 +67,7 @@ internal class ZstdDecompressSource internal constructor(
6867

6968
outputBuffer.clear()
7069

71-
source.use {
72-
decompressor.use {
73-
}
74-
}
70+
source.use { decompressor.use {} }
7571
}
7672

7773
private fun refillIfNecessary() {
@@ -90,14 +86,15 @@ internal class ZstdDecompressSource internal constructor(
9086

9187
source.buffer.readUnsafe(inputCursor).use { inputCursor ->
9288
inputCursor.next()
93-
result = decompressor.decompressStream(
94-
outputByteArray = outputCursor.data!!,
95-
outputEnd = outputCursor.end,
96-
outputStart = outputCursor.start,
97-
inputByteArray = inputCursor.data!!,
98-
inputEnd = inputCursor.end,
99-
inputStart = inputCursor.start,
100-
)
89+
result =
90+
decompressor.decompressStream(
91+
outputByteArray = outputCursor.data!!,
92+
outputEnd = outputCursor.end,
93+
outputStart = outputCursor.start,
94+
inputByteArray = inputCursor.data!!,
95+
inputEnd = inputCursor.end,
96+
inputStart = inputCursor.start,
97+
)
10198
}
10299
source.skip(decompressor.inputBytesProcessed.toLong())
103100

zstd-kmp-okio/src/commonTest/kotlin/com/squareup/zstd/okio/ZstdCompressSinkTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ class ZstdCompressSinkTest {
4242

4343
private fun testRoundTrip(byteCount: Int) {
4444
val compressed = Buffer()
45-
compressed.zstdCompress().buffer().use {
46-
it.write(Random(1).nextBytes(byteCount))
47-
}
45+
compressed.zstdCompress().buffer().use { it.write(Random(1).nextBytes(byteCount)) }
4846
assertThat(compressed.referenceDecompress())
4947
.isEqualTo(Random(1).nextBytes(byteCount).toByteString())
5048
}

zstd-kmp-okio/src/commonTest/kotlin/com/squareup/zstd/okio/ZstdDecompressSourceTest.kt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,21 @@ class ZstdDecompressSourceTest {
4646
}
4747

4848
private fun testRoundTrip(byteCount: Int) {
49-
val compressed = Random(1).nextBytes(byteCount)
50-
.referenceCompress()
49+
val compressed = Random(1).nextBytes(byteCount).referenceCompress()
5150

52-
val decompressed = compressed.zstdDecompress().buffer().use {
53-
it.readByteString()
54-
}
51+
val decompressed = compressed.zstdDecompress().buffer().use { it.readByteString() }
5552

56-
assertThat(decompressed)
57-
.isEqualTo(Random(1).nextBytes(byteCount).toByteString())
53+
assertThat(decompressed).isEqualTo(Random(1).nextBytes(byteCount).toByteString())
5854
}
5955

6056
@Test
6157
fun sourceIsTruncated() {
62-
val compressed = Random(1).nextBytes(1024)
63-
.referenceCompress()
58+
val compressed = Random(1).nextBytes(1024).referenceCompress()
6459
val truncated = Buffer()
6560
truncated.write(compressed, compressed.size - 1L)
6661

6762
truncated.zstdDecompress().buffer().use {
68-
assertFailsWith<EOFException> {
69-
it.readByteString()
70-
}
63+
assertFailsWith<EOFException> { it.readByteString() }
7164
}
7265
}
7366

@@ -77,9 +70,7 @@ class ZstdDecompressSourceTest {
7770
truncated.writeUtf8("this is not zstd data")
7871

7972
truncated.zstdDecompress().buffer().use {
80-
val e = assertFailsWith<IOException> {
81-
it.readByteString()
82-
}
73+
val e = assertFailsWith<IOException> { it.readByteString() }
8374
assertThat(e).hasMessage("zstd decompress failed: Unknown frame descriptor")
8475
}
8576
}

zstd-kmp-okio/src/jniTest/kotlin/com/squareup/zstd/okio/JniZstdCompressSinkTest.kt

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
@file:Suppress(
17-
"CANNOT_OVERRIDE_INVISIBLE_MEMBER",
18-
"INVISIBLE_MEMBER",
19-
"INVISIBLE_REFERENCE",
20-
)
16+
@file:Suppress("CANNOT_OVERRIDE_INVISIBLE_MEMBER", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
2117

2218
package com.squareup.zstd.okio
2319

@@ -47,17 +43,18 @@ class JniZstdCompressSinkTest {
4743
fun writeFailure() {
4844
val delegate = Buffer()
4945
var sinkClosed = false
50-
val sink = object : Sink by delegate {
51-
override fun write(source: Buffer, byteCount: Long) {
52-
delegate.write(source, byteCount)
53-
throw IOException("boom!")
54-
}
46+
val sink =
47+
object : Sink by delegate {
48+
override fun write(source: Buffer, byteCount: Long) {
49+
delegate.write(source, byteCount)
50+
throw IOException("boom!")
51+
}
5552

56-
override fun close() {
57-
delegate.close()
58-
sinkClosed = true
53+
override fun close() {
54+
delegate.close()
55+
sinkClosed = true
56+
}
5957
}
60-
}
6158

6259
val compressor = JniZstdCompressor()
6360
val zstdCompressSink = ZstdCompressSink(sink.buffer(), compressor)
@@ -69,9 +66,7 @@ class JniZstdCompressSinkTest {
6966
assertThat(compressor.cctxPointer).isNotEqualTo(0L)
7067
assertThat(sinkClosed).isFalse()
7168

72-
assertFailsWith<IOException> {
73-
zstdCompressSink.close()
74-
}
69+
assertFailsWith<IOException> { zstdCompressSink.close() }
7570
assertThat(zstdCompressSink.closed).isTrue()
7671
assertThat(compressor.cctxPointer).isEqualTo(0L)
7772
assertThat(sinkClosed).isTrue()
@@ -98,9 +93,7 @@ class JniZstdCompressSinkTest {
9893
pipe.source.timeout().timeout(250, TimeUnit.MILLISECONDS)
9994

10095
ZstdInputStream(pipe.source.buffer().inputStream()).source().buffer().use { source ->
101-
pipe.sink.zstdCompress().buffer().use { sink ->
102-
sink.writeUtf8("hello world")
103-
}
96+
pipe.sink.zstdCompress().buffer().use { sink -> sink.writeUtf8("hello world") }
10497

10598
assertThat(source.readUtf8(11)).isEqualTo("hello world")
10699
}
@@ -115,9 +108,7 @@ class JniZstdCompressSinkTest {
115108
pipe.sink.zstdCompress().buffer().use { sink ->
116109
sink.writeUtf8("hello world")
117110

118-
assertFailsWith<InterruptedIOException> {
119-
source.readUtf8(11)
120-
}
111+
assertFailsWith<InterruptedIOException> { source.readUtf8(11) }
121112

122113
sink.flush()
123114
assertThat(source.readUtf8(11)).isEqualTo("hello world")
@@ -134,9 +125,7 @@ class JniZstdCompressSinkTest {
134125
pipe.sink.zstdCompress().buffer().use { sink ->
135126
sink.writeUtf8("hello world")
136127

137-
assertFailsWith<InterruptedIOException> {
138-
source.readUtf8(11)
139-
}
128+
assertFailsWith<InterruptedIOException> { source.readUtf8(11) }
140129
}
141130
assertThat(source.readUtf8(11)).isEqualTo("hello world")
142131
}

zstd-kmp-okio/src/jniTest/kotlin/com/squareup/zstd/okio/JniZstdDecompressSourceTest.kt

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
@file:Suppress(
17-
"CANNOT_OVERRIDE_INVISIBLE_MEMBER",
18-
"INVISIBLE_MEMBER",
19-
"INVISIBLE_REFERENCE",
20-
)
16+
@file:Suppress("CANNOT_OVERRIDE_INVISIBLE_MEMBER", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
2117

2218
package com.squareup.zstd.okio
2319

@@ -62,29 +58,27 @@ class JniZstdDecompressSourceTest {
6258
/** Confirm we can clean up even if reads aren't working. */
6359
@Test
6460
fun readFailure() {
65-
val delegate = Random(1).nextBytes(1024 * 1024)
66-
.referenceCompress()
61+
val delegate = Random(1).nextBytes(1024 * 1024).referenceCompress()
6762
var sourceClosed = false
6863
var explode = false
69-
val source = object : Source by delegate {
70-
override fun read(sink: Buffer, byteCount: Long): Long {
71-
val result = delegate.read(sink, byteCount)
72-
if (explode) throw IOException("boom!")
73-
return result
74-
}
64+
val source =
65+
object : Source by delegate {
66+
override fun read(sink: Buffer, byteCount: Long): Long {
67+
val result = delegate.read(sink, byteCount)
68+
if (explode) throw IOException("boom!")
69+
return result
70+
}
7571

76-
override fun close() {
77-
sourceClosed = true
72+
override fun close() {
73+
sourceClosed = true
74+
}
7875
}
79-
}
8076

8177
val decompressor = JniZstdDecompressor()
8278
val zstdDecompressSource = ZstdDecompressSource(source.buffer(), decompressor)
8379

8480
explode = true
85-
assertFailsWith<IOException> {
86-
zstdDecompressSource.buffer().require(1024L)
87-
}
81+
assertFailsWith<IOException> { zstdDecompressSource.buffer().require(1024L) }
8882
assertThat(zstdDecompressSource.closed).isFalse()
8983
assertThat(decompressor.dctxPointer).isNotEqualTo(0L)
9084
assertThat(sourceClosed).isFalse()

0 commit comments

Comments
 (0)