Skip to content

Commit b066b0a

Browse files
committed
Updated to Okio 2.0 release. Since Okio 2.0 is a binary compatible but Kotlin source incompatible, we will bumb our version number too.
1 parent 94e3dfd commit b066b0a

File tree

9 files changed

+23
-26
lines changed

9 files changed

+23
-26
lines changed

lib/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ group 'nl.nl2312'
22
version '1.0'
33

44
buildscript {
5-
ext.kotlin_version = '1.2.30'
5+
ext.kotlin_version = '1.2.61'
66

77
repositories {
88
mavenCentral()
@@ -20,7 +20,7 @@ repositories {
2020

2121
dependencies {
2222
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
23-
compile 'com.squareup.okio:okio:1.14.0'
23+
compile 'com.squareup.okio:okio:2.0.0'
2424

2525
testCompile 'junit:junit:4.12'
2626
testCompile 'com.google.truth:truth:0.39'

lib/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip

lib/src/main/java/nl/nl2312/okio/base64/Base64Sink.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class Base64Sink(delegate: Sink) : ForwardingSink(delegate) {
1414

1515
override fun write(source: Buffer, byteCount: Long) {
1616
// Read the requested number of bytes (or all available) from source
17-
val bytesToRead = byteCount.coerceAtMost(source.size())
17+
val bytesToRead = byteCount.coerceAtMost(source.size)
1818
val decoded = source.readByteString(bytesToRead)
1919

2020
// Base64-encode
2121
val encoded = decoded.base64()
2222

2323
val encodedSink = Buffer()
2424
encodedSink.writeUtf8(encoded)
25-
super.write(encodedSink, encodedSink.size())
25+
super.write(encodedSink, encodedSink.size)
2626
}
2727

2828
}

lib/src/main/java/nl/nl2312/okio/base64/Base64Source.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nl.nl2312.okio.base64
22

33
import okio.Buffer
44
import okio.ByteString
5+
import okio.ByteString.Companion.decodeBase64
56
import okio.ForwardingSource
67
import okio.Source
78

@@ -21,27 +22,27 @@ class Base64Source(source: Source) : ForwardingSource(source) {
2122
if (bytesRequested >= MAX_REQUEST_LENGTH) throw IllegalArgumentException("bytesRequested > max allowed")
2223

2324
// If we have the requested bytes already buffered, return directly
24-
if (decodeBuffer.size() >= bytesRequested) {
25+
if (decodeBuffer.size >= bytesRequested) {
2526
sink.write(decodeBuffer, bytesRequested)
2627
return bytesRequested
2728
}
2829

2930
var streamEnded = false
30-
while (decodeBuffer.size() < bytesRequested && !streamEnded) {
31+
while (decodeBuffer.size < bytesRequested && !streamEnded) {
3132
val bytesRead = super.read(sourceBuffer, bytesRequested)
3233
if (bytesRead < 0) {
3334
streamEnded = true
3435
}
3536

3637
// Decode all available blocks
37-
val allFullBlocks = BASE64_BLOCK * (sourceBuffer.size() / BASE64_BLOCK)
38-
val decoded: ByteString? = ByteString.decodeBase64(sourceBuffer.readUtf8(allFullBlocks))
38+
val allFullBlocks = BASE64_BLOCK * (sourceBuffer.size / BASE64_BLOCK)
39+
val decoded: ByteString? = sourceBuffer.readUtf8(allFullBlocks).decodeBase64()
3940
if (decoded == null) throw IllegalStateException("base64 decode failed")
4041
decodeBuffer.write(decoded)
4142
}
4243

4344
// Return the requested number of bytes (or all that were available)
44-
val bytesToReturn = bytesRequested.coerceAtMost(decodeBuffer.size())
45+
val bytesToReturn = bytesRequested.coerceAtMost(decodeBuffer.size)
4546
sink.write(decodeBuffer, bytesToReturn)
4647

4748
return if (streamEnded) -1 else bytesToReturn

lib/src/main/java/nl/nl2312/okio/cipher/CipherSink.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class CipherSink(
1717

1818
override fun write(source: Buffer, byteCount: Long) {
1919
// Read the requested number of bytes (or all available) from source
20-
val bytesToRead = byteCount.coerceAtMost(source.size())
20+
val bytesToRead = byteCount.coerceAtMost(source.size)
2121
val decrypted = source.readByteArray(bytesToRead)
2222

2323
// Encrypt
2424
val encrypted = cipher.doFinal(decrypted)
2525

2626
val encryptedSink = Buffer()
2727
encryptedSink.write(encrypted)
28-
super.write(encryptedSink, encryptedSink.size())
28+
super.write(encryptedSink, encryptedSink.size)
2929
}
3030

3131
}

lib/src/main/java/nl/nl2312/okio/cipher/CipherSource.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import okio.ForwardingSource
55
import okio.Source
66
import javax.crypto.Cipher
77

8-
98
/**
109
* Accepts an encrypted [Source] and deciphers it on the fly.
1110
*
@@ -28,8 +27,8 @@ class CipherSource(
2827
if (bytesToRead < 0) throw IllegalArgumentException("bytesRequested > max allowed")
2928

3029
var streamEnd = false
31-
while (sourceBuffer.size() < bytesToRead && !streamEnd) {
32-
val bytesRead = super.read(sourceBuffer, bytesToRead - sourceBuffer.size())
30+
while (sourceBuffer.size < bytesToRead && !streamEnd) {
31+
val bytesRead = super.read(sourceBuffer, bytesToRead - sourceBuffer.size)
3332
if (bytesRead < 0) {
3433
streamEnd = true
3534
}
@@ -50,7 +49,7 @@ class CipherSource(
5049
}
5150

5251
// Sink the requested number of bytes (or all that were still in the source)
53-
val bytesToReturn = bytesRequested.coerceAtMost(decipheredBuffer.size())
52+
val bytesToReturn = bytesRequested.coerceAtMost(decipheredBuffer.size)
5453
sink.write(decipheredBuffer, bytesToReturn)
5554

5655
// Return number of written deciphered bytes, or -1 if there is nothing more to decipher

lib/src/test/java/nl/nl2312/okio/base64/Base64SourceTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package nl.nl2312.okio.base64
22

33
import com.google.common.truth.Truth.assertThat
44
import okio.Buffer
5-
import okio.ByteString
5+
import okio.ByteString.Companion.toByteString
66
import org.junit.Test
77
import java.util.*
88

@@ -24,7 +24,7 @@ class Base64SourceTest {
2424
// Generate a very long random string
2525
val randomLongByteArray =
2626
Random().let { random -> (0..10_000).map { (random.nextInt()).toByte() } }.toByteArray()
27-
val randomLongBase64 = ByteString.of(randomLongByteArray, 0, randomLongByteArray.size)
27+
val randomLongBase64 = randomLongByteArray.toByteString(0, randomLongByteArray.size)
2828
val randomLongSource = Buffer().also { it.writeUtf8(randomLongBase64.base64()) }
2929

3030
val decoded = Base64Source(randomLongSource)

lib/src/test/java/nl/nl2312/okio/cipher/CipherSinkTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import javax.crypto.Cipher
99
import javax.crypto.spec.IvParameterSpec
1010
import javax.crypto.spec.SecretKeySpec
1111

12-
1312
class CipherSinkTest {
1413

1514
private val encodeCipher: Cipher

lib/src/test/java/nl/nl2312/okio/cipher/CipherSourceTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ package nl.nl2312.okio.cipher
22

33
import com.google.common.truth.Truth.assertThat
44
import nl.nl2312.okio.base64.Base64Source
5-
import okio.Buffer
6-
import okio.ByteString
7-
import okio.Okio
8-
import okio.Source
5+
import okio.*
6+
import okio.ByteString.Companion.encodeUtf8
7+
import okio.ByteString.Companion.toByteString
98
import org.junit.Test
109
import java.security.SecureRandom
1110
import javax.crypto.Cipher
1211
import javax.crypto.spec.IvParameterSpec
1312
import javax.crypto.spec.SecretKeySpec
1413

15-
1614
class CipherSourceTest {
1715

1816
private val encodeCipher: Cipher
@@ -115,7 +113,7 @@ class CipherSourceTest {
115113

116114
// Okio's RealBufferedSource.read(Buffer, Long) will only write to the given buffer when the
117115
// CipherSource.read(Buffer, Long) returns a non-negative byes read could
118-
val buffer = Okio.buffer(decoded as Source)
116+
val buffer = (decoded as Source).buffer()
119117
val output = Buffer()
120118
buffer.read(output, 8192)
121119
assertThat(output.readUtf8()).isEqualTo("okio oh my¿¡ okio oh my¿¡ okio oh my¿¡")
@@ -124,7 +122,7 @@ class CipherSourceTest {
124122
@Test
125123
fun read_base64Wrapped() {
126124
val ciphered = encodeCipher.doFinal("okio oh my¿¡".toByteArray())
127-
val base64 = ByteString.encodeUtf8(ByteString.of(ciphered, 0, ciphered.size).base64())
125+
val base64 = ciphered.toByteString(0, ciphered.size).base64().encodeUtf8()
128126
val base64Source = Buffer().write(base64)
129127

130128
val decoded = CipherSource(Base64Source(base64Source), decodeCipher)

0 commit comments

Comments
 (0)