1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-07 12:42:57 +03:00

Okio upgrade post kotlin (#4814)

Okio 2.2.2 upgrade and adopt kotlin friendly API
This commit is contained in:
Yuri Schimke
2019-03-29 21:54:10 +00:00
committed by GitHub
parent 41c0fd9cf4
commit e2cfcb35ea
9 changed files with 52 additions and 37 deletions

View File

@@ -15,7 +15,7 @@ buildscript {
'junit': '4.12',
'kotlin': '1.3.20',
'moshi': '1.8.0',
'okio': '1.17.2',
'okio': '2.2.2',
]
ext.deps = [

View File

@@ -17,7 +17,7 @@ package okhttp3.dnsoverhttps
import okio.Buffer
import okio.ByteString
import okio.Utf8
import okio.utf8Size
import java.io.EOFException
import java.net.InetAddress
import java.net.UnknownHostException
@@ -47,7 +47,7 @@ object DnsRecordCodec {
val nameBuf = Buffer()
val labels = host.split('.').dropLastWhile { it.isEmpty() }.toTypedArray()
for (label in labels) {
val utf8ByteCount = Utf8.size(label)
val utf8ByteCount = label.utf8Size()
if (utf8ByteCount != label.length.toLong()) {
throw IllegalArgumentException("non-ascii hostname: $host")
}
@@ -56,7 +56,7 @@ object DnsRecordCodec {
}
nameBuf.writeByte(0) // end
nameBuf.copyTo(this, 0, nameBuf.size())
nameBuf.copyTo(this, 0, nameBuf.size)
writeShort(type)
writeShort(1) // CLASS_IN
}.readByteString()

View File

@@ -15,6 +15,7 @@
*/
package okhttp3.dnsoverhttps;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
@@ -140,7 +141,7 @@ public class DnsOverHttpsTest {
fail();
} catch (IOException ioe) {
assertThat(ioe).hasMessage("google.com");
assertThat(ioe.getCause()).isInstanceOf(RuntimeException.class);
assertThat(ioe.getCause()).isInstanceOf(EOFException.class);
}
}

View File

@@ -26,13 +26,15 @@ import okhttp3.internal.http.HttpMethod
import okhttp3.internal.http.StatusLine
import okhttp3.internal.io.FileSystem
import okhttp3.internal.platform.Platform
import okio.buffer
import okio.Buffer
import okio.BufferedSink
import okio.BufferedSource
import okio.ByteString
import okio.ByteString.Companion.decodeBase64
import okio.ByteString.Companion.encodeUtf8
import okio.ForwardingSink
import okio.ForwardingSource
import okio.Okio
import okio.Sink
import okio.Source
import java.io.Closeable
@@ -328,7 +330,7 @@ class Cache internal constructor(
while (delegate.hasNext()) {
try {
delegate.next().use { snapshot ->
val metadata = Okio.buffer(snapshot.getSource(ENTRY_METADATA))
val metadata = snapshot.getSource(ENTRY_METADATA).buffer()
nextUrl = metadata.readUtf8LineStrict()
return true
}
@@ -506,7 +508,7 @@ class Cache internal constructor(
@Throws(IOException::class)
internal constructor(rawSource: Source) {
try {
val source = Okio.buffer(rawSource)
val source = rawSource.buffer()
url = source.readUtf8LineStrict()
requestMethod = source.readUtf8LineStrict()
val varyHeadersBuilder = Headers.Builder()
@@ -571,7 +573,7 @@ class Cache internal constructor(
@Throws(IOException::class)
fun writeTo(editor: DiskLruCache.Editor) {
val sink = Okio.buffer(editor.newSink(ENTRY_METADATA))
val sink = editor.newSink(ENTRY_METADATA).buffer()
sink.writeUtf8(url).writeByte('\n'.toInt())
sink.writeUtf8(requestMethod).writeByte('\n'.toInt())
sink.writeDecimalLong(varyHeaders.size().toLong()).writeByte('\n'.toInt())
@@ -620,7 +622,7 @@ class Cache internal constructor(
for (i in 0 until length) {
val line = source.readUtf8LineStrict()
val bytes = Buffer()
bytes.write(ByteString.decodeBase64(line)!!)
bytes.write(line.decodeBase64()!!)
result.add(certificateFactory.generateCertificate(bytes.inputStream()))
}
return result
@@ -689,13 +691,13 @@ class Cache internal constructor(
init {
val source = snapshot.getSource(ENTRY_BODY)
bodySource = Okio.buffer(object : ForwardingSource(source) {
bodySource = object : ForwardingSource(source) {
@Throws(IOException::class)
override fun close() {
snapshot.close()
super.close()
}
})
}.buffer()
}
override fun contentType(): MediaType? {
@@ -720,7 +722,7 @@ class Cache internal constructor(
private const val ENTRY_COUNT = 2
@JvmStatic
fun key(url: HttpUrl): String = ByteString.encodeUtf8(url.toString()).md5().hex()
fun key(url: HttpUrl): String = url.toString().encodeUtf8().md5().hex()
@Throws(IOException::class)
@JvmStatic

View File

@@ -17,6 +17,7 @@ package okhttp3
import okhttp3.internal.tls.CertificateChainCleaner
import okio.ByteString
import okio.ByteString.Companion.decodeBase64
import java.security.cert.Certificate
import java.security.cert.X509Certificate
import java.util.Objects
@@ -254,11 +255,11 @@ class CertificatePinner internal constructor(
when {
pin.startsWith("sha1/") -> {
this.hashAlgorithm = "sha1/"
this.hash = ByteString.decodeBase64(pin.substring("sha1/".length))!!
this.hash = pin.substring("sha1/".length).decodeBase64()!!
}
pin.startsWith("sha256/") -> {
this.hashAlgorithm = "sha256/"
this.hash = ByteString.decodeBase64(pin.substring("sha256/".length))!!
this.hash = pin.substring("sha256/".length).decodeBase64()!!
}
else -> throw IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': $pin")
}

View File

@@ -15,7 +15,7 @@
*/
package okhttp3
import okio.ByteString
import okio.ByteString.Companion.encode
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets.ISO_8859_1
@@ -28,7 +28,7 @@ object Credentials {
charset: Charset = ISO_8859_1
): String {
val usernameAndPassword = "$username:$password"
val encoded = ByteString.encodeString(usernameAndPassword, charset).base64()
val encoded = usernameAndPassword.encode(charset).base64()
return "Basic $encoded"
}
}

View File

@@ -58,7 +58,7 @@ class FormBody internal constructor(
*/
private fun writeOrCountBytes(sink: BufferedSink?, countBytes: Boolean): Long {
var byteCount = 0L
val buffer: Buffer = if (countBytes) Buffer() else sink!!.buffer()
val buffer: Buffer = if (countBytes) Buffer() else sink!!.buffer
for (i in 0 until encodedNames.size) {
if (i > 0) buffer.writeByte('&'.toInt())
@@ -68,7 +68,7 @@ class FormBody internal constructor(
}
if (countBytes) {
byteCount = buffer.size()
byteCount = buffer.size
buffer.clear()
}

View File

@@ -32,6 +32,7 @@ import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.platform.Platform;
import okio.Buffer;
import okio.ByteString;
@@ -171,13 +172,18 @@ public final class HttpHeaders {
for (int h = 0; h < responseHeaders.size(); h++) {
if (headerName.equalsIgnoreCase(responseHeaders.name(h))) {
Buffer header = new Buffer().writeUtf8(responseHeaders.value(h));
parseChallengeHeader(result, header);
try {
parseChallengeHeader(result, header);
} catch (EOFException e) {
Platform.get().log(Platform.WARN, "Unable to parse challenge", e);
}
}
}
return result;
}
private static void parseChallengeHeader(List<Challenge> result, Buffer header) {
private static void parseChallengeHeader(List<Challenge> result, Buffer header)
throws EOFException {
String peek = null;
while (true) {
@@ -237,15 +243,17 @@ public final class HttpHeaders {
}
/** Returns true if any commas were skipped. */
private static boolean skipWhitespaceAndCommas(Buffer buffer) {
private static boolean skipWhitespaceAndCommas(Buffer buffer) throws EOFException {
boolean commaFound = false;
while (!buffer.exhausted()) {
byte b = buffer.getByte(0);
if (b == ',') {
buffer.readByte(); // Consume ','.
// Consume ','.
buffer.readByte();
commaFound = true;
} else if (b == ' ' || b == '\t') {
buffer.readByte(); // Consume space or tab.
// Consume space or tab.
buffer.readByte();
} else {
break;
}
@@ -253,7 +261,7 @@ public final class HttpHeaders {
return commaFound;
}
private static int skipAll(Buffer buffer, byte b) {
private static int skipAll(Buffer buffer, byte b) throws EOFException {
int count = 0;
while (!buffer.exhausted() && buffer.getByte(0) == b) {
count++;
@@ -267,7 +275,7 @@ public final class HttpHeaders {
* each sequence. Returns the unescaped string, or null if the buffer isn't prefixed with a
* double-quoted string.
*/
private static String readQuotedString(Buffer buffer) {
private static String readQuotedString(Buffer buffer) throws EOFException {
if (buffer.readByte() != '\"') throw new IllegalArgumentException();
Buffer result = new Buffer();
while (true) {
@@ -276,13 +284,15 @@ public final class HttpHeaders {
if (buffer.getByte(i) == '"') {
result.write(buffer, i);
buffer.readByte(); // Consume '"'.
// Consume '"'.
buffer.readByte();
return result.readUtf8();
}
if (buffer.size() == i + 1L) return null; // Dangling escape.
result.write(buffer, i);
buffer.readByte(); // Consume '\'.
// Consume '\'.
buffer.readByte();
result.write(buffer, 1L); // The escaped character.
}
}

View File

@@ -17,6 +17,7 @@ package okhttp3.internal.http2
import okhttp3.internal.Util
import okio.ByteString
import okio.ByteString.Companion.encodeUtf8
/** HTTP header: the name is an ASCII string, but the value can be UTF-8. */
class Header(
@@ -25,12 +26,12 @@ class Header(
/** Value in UTF-8 encoding. */
@JvmField val value: ByteString
) {
@JvmField internal val hpackSize = 32 + name.size() + value.size()
@JvmField internal val hpackSize = 32 + name.size + value.size
// TODO: search for toLowerCase and consider moving logic here.
constructor(name: String, value: String) : this(ByteString.encodeUtf8(name), ByteString.encodeUtf8(value))
constructor(name: String, value: String) : this(name.encodeUtf8(), value.encodeUtf8())
constructor(name: ByteString, value: String) : this(name, ByteString.encodeUtf8(value))
constructor(name: ByteString, value: String) : this(name, value.encodeUtf8())
override fun equals(other: Any?): Boolean {
return other is Header
@@ -51,7 +52,7 @@ class Header(
companion object {
// Special header names defined in HTTP/2 spec.
@JvmField val PSEUDO_PREFIX: ByteString = ByteString.encodeUtf8(":")
@JvmField val PSEUDO_PREFIX: ByteString = ":".encodeUtf8()
const val RESPONSE_STATUS_UTF8 = ":status"
const val TARGET_METHOD_UTF8 = ":method"
@@ -59,10 +60,10 @@ class Header(
const val TARGET_SCHEME_UTF8 = ":scheme"
const val TARGET_AUTHORITY_UTF8 = ":authority"
@JvmField val RESPONSE_STATUS: ByteString = ByteString.encodeUtf8(RESPONSE_STATUS_UTF8)
@JvmField val TARGET_METHOD: ByteString = ByteString.encodeUtf8(TARGET_METHOD_UTF8)
@JvmField val TARGET_PATH: ByteString = ByteString.encodeUtf8(TARGET_PATH_UTF8)
@JvmField val TARGET_SCHEME: ByteString = ByteString.encodeUtf8(TARGET_SCHEME_UTF8)
@JvmField val TARGET_AUTHORITY: ByteString = ByteString.encodeUtf8(TARGET_AUTHORITY_UTF8)
@JvmField val RESPONSE_STATUS: ByteString = RESPONSE_STATUS_UTF8.encodeUtf8()
@JvmField val TARGET_METHOD: ByteString = TARGET_METHOD_UTF8.encodeUtf8()
@JvmField val TARGET_PATH: ByteString = TARGET_PATH_UTF8.encodeUtf8()
@JvmField val TARGET_SCHEME: ByteString = TARGET_SCHEME_UTF8.encodeUtf8()
@JvmField val TARGET_AUTHORITY: ByteString = TARGET_AUTHORITY_UTF8.encodeUtf8()
}
}