Created
July 30, 2018 03:17
-
-
Save swankjesse/e3f4a15d567f4bc8a78850e736479273 to your computer and use it in GitHub Desktop.
Okio 1.x to 2.x upgrade for Kotlin users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package okioupgrade | |
import okio.Buffer | |
import okio.ByteString | |
import okio.ByteString.Companion.decodeBase64 | |
import okio.ByteString.Companion.decodeHex | |
import okio.ByteString.Companion.encode | |
import okio.ByteString.Companion.encodeUtf8 | |
import okio.ByteString.Companion.readByteString | |
import okio.ByteString.Companion.toByteString | |
import okio.ForwardingSink | |
import okio.ForwardingSource | |
import okio.GzipSink | |
import okio.HashingSink | |
import okio.HashingSource | |
import okio.Pipe | |
import okio.Sink | |
import okio.Source | |
import okio.appendingSink | |
import okio.blackholeSink | |
import okio.buffer | |
import okio.sink | |
import okio.source | |
import okio.utf8Size | |
import java.io.File | |
import java.io.InputStream | |
import java.io.OutputStream | |
import java.net.Socket | |
import java.nio.ByteBuffer | |
import java.nio.charset.Charset | |
import java.nio.file.OpenOption | |
import java.nio.file.Path | |
/** | |
* Okio 1.x to 2.x Upgrade Guide | |
* ----------------------------- | |
* | |
* Okio 2.x is **.kt source-incompatible** with Okio 1.x. This release adopts Kotlin idioms where | |
* they are available. This guide attempts to make it fast, easy, and safe to make these changes. | |
* It assumes you're using a recent release of IntelliJ or Android Studio. | |
* | |
* 1. Change your build to use the new version of Okio. | |
* | |
* For Maven: | |
* | |
* ```xml | |
* <dependency> | |
* <groupId>com.squareup.okio</groupId> | |
* <artifactId>okio</artifactId> | |
* <version>2.0.0</version> | |
* </dependency> | |
* ``` | |
* | |
* For Gradle: | |
* | |
* ```groovy | |
* compile 'com.squareup.okio:okio:2.0.0' | |
* ``` | |
* | |
* 2. Copy this file into your project's production source directory. You won't need to check it in; | |
* it is only useful during the upgrade process. | |
* | |
* 3. Find build errors by attempting to build the project. For each `.kt` file with build errors: | |
* | |
* A. Paste in this import: | |
* | |
* ``` | |
* import okioupgrade.* | |
* ``` | |
* | |
* B. For each call to a static method on `ByteString`, replace it with with a static method call | |
* on `ByteStringUpgrade`. For example, change calls to `ByteString.encodeUtf8(...)` to | |
* `ByteStringUpgrade.encodeUtf8(...)`. | |
* | |
* Using the F2 keyboard shortcut to navigate between errors is helpful here. | |
* | |
* Don't tweak formatting yet: this call will be short lived! | |
* | |
* C. For each deprecation error, use alt+enter (option+return on Mac) to apply the "Replace | |
* Usages in Whole Project" quick fix. | |
* | |
* Using the F2 keyboard shortcut is also helpful here. | |
* | |
* D. Optimize imports. If you've fixed all deprecation warnings the imports added in step 3A | |
* will all be gone! | |
* | |
* 4. Remove this file from your project. You've finished your upgrade to Okio 2. | |
*/ | |
class Okio { | |
companion object { | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "file.appendingSink()", | |
imports = ["okio.appendingSink"])) | |
fun appendingSink(file: File) = file.appendingSink() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "sink.buffer()", | |
imports = ["okio.buffer"])) | |
fun buffer(sink: Sink) = sink.buffer() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "source.buffer()", | |
imports = ["okio.buffer"])) | |
fun buffer(source: Source) = source.buffer() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "file.sink()", | |
imports = ["okio.sink"])) | |
fun sink(file: File) = file.sink() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "outputStream.sink()", | |
imports = ["okio.sink"])) | |
fun sink(outputStream: OutputStream) = outputStream.sink() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "path.sink(*options)", | |
imports = ["okio.sink"])) | |
fun sink(path: Path, vararg options: OpenOption) = path.sink(*options) | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "socket.sink()", | |
imports = ["okio.sink"])) | |
fun sink(socket: Socket) = socket.sink() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "file.source()", | |
imports = ["okio.source"])) | |
fun source(file: File) = file.source() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "inputStream.source()", | |
imports = ["okio.source"])) | |
fun source(inputStream: InputStream) = inputStream.source() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "path.source(*options)", | |
imports = ["okio.source"])) | |
fun source(path: Path, vararg options: OpenOption) = path.source(*options) | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "socket.source()", | |
imports = ["okio.source"])) | |
fun source(socket: Socket) = socket.source() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "blackholeSink()", | |
imports = ["okio.blackholeSink"])) | |
fun blackhole() = blackholeSink() | |
} | |
} | |
class Utf8 { | |
companion object { | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "string.utf8Size()", | |
imports = ["okio.utf8Size"])) | |
fun size(string: String) = string.utf8Size() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "string.utf8Size(beginIndex, endIndex)", | |
imports = ["okio.utf8Size"])) | |
fun size(string: String, beginIndex: Int, endIndex: Int) = string.utf8Size(beginIndex, endIndex) | |
} | |
} | |
class ByteStringUpgrade { | |
companion object { | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "string.decodeBase64()", | |
imports = ["okio.ByteString.Companion.decodeBase64"])) | |
fun decodeBase64(string: String) = string.decodeBase64() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "string.decodeHex()", | |
imports = ["okio.ByteString.Companion.decodeHex"])) | |
fun decodeHex(string: String) = string.decodeHex() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "string.encode(charset)", | |
imports = ["okio.ByteString.Companion.encode"])) | |
fun encodeString(string: String, charset: Charset) = string.encode(charset) | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "string.encodeUtf8()", | |
imports = ["okio.ByteString.Companion.encodeUtf8"])) | |
fun encodeUtf8(string: String) = string.encodeUtf8() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "buffer.toByteString()", | |
imports = ["okio.ByteString.Companion.toByteString"])) | |
fun of(buffer: ByteBuffer) = buffer.toByteString() | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "array.toByteString(offset, byteCount)", | |
imports = ["okio.ByteString.Companion.toByteString"])) | |
fun of(array: ByteArray, offset: Int, byteCount: Int) = array.toByteString(offset, byteCount) | |
@Deprecated( | |
message = "moved to extension function", | |
replaceWith = ReplaceWith( | |
expression = "inputstream.readByteString(byteCount)", | |
imports = ["okio.ByteString.Companion.readByteString"])) | |
fun read(inputstream: InputStream, byteCount: Int) = inputstream.readByteString(byteCount) | |
} | |
} | |
@Deprecated( | |
message = "moved to operator function", | |
replaceWith = ReplaceWith(expression = "this[index]")) | |
fun Buffer.getByte(index: Long) = this[index] | |
@Deprecated( | |
message = "moved to operator function", | |
replaceWith = ReplaceWith(expression = "this[index]")) | |
fun ByteString.getByte(index: Int) = this[index] | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "size")) | |
fun Buffer.size() = size | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "size")) | |
fun ByteString.size() = size | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "delegate")) | |
fun ForwardingSink.delegate() = delegate | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "delegate")) | |
fun ForwardingSource.delegate() = delegate | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "deflater")) | |
fun GzipSink.deflater() = deflater | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "hash")) | |
fun HashingSink.hash() = hash | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "hash")) | |
fun HashingSource.hash() = hash | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "sink")) | |
fun Pipe.sink() = sink | |
@Deprecated( | |
message = "moved to val", | |
replaceWith = ReplaceWith(expression = "source")) | |
fun Pipe.source() = source | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment