Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Okioに愛を込めて

 Okioに愛を込めて

OkioはSquareが開発しているjava.ioとjava.nioの取り回しを楽にしてくれるラッパーライブラリです。言わずと知れたOkHttpやMoshiのベースにも使用されているライブラリです。
Kotlinで書き直された2.0.0よりMPP(KMP)対応が行われ、3.0.0でファイルシステム周りの機能が追加されました。
KMP(android, iOS)プロジェクトで実際に使用した私がありがたいと感じた部分についてお話しします。

本セッションでは下記のことを取り扱います。

・標準ライブラリが強力なKotlinでなぜOkioを見直すのか
・KMP実装にIO周辺実装での諸課題
・KMP実装においてもOkioがもたらすメリット
・Okioを使用する際の注意するべき前提

Avatar for RyuNen344

RyuNen344

June 22, 2024
Tweet

More Decks by RyuNen344

Other Decks in Technology

Transcript

  1. • What we get/lost after Kotlinize/KMPnize? • About Okio •

    Source/Sink/Buffer/Segment • Big Love to ByteString • FileSystem and Path • How much does okio ommit our codes? • Okio? kotlinx-io? Outline
  2. What we lost after Kotlinize? Q. Is Kotlin compatible with

    the Java programming language? A. Yes. Kotlin is 100% interoperable with https://kotlinlang.org/docs/faq.html
  3. Q1. java.util.Base64࢖͑ͳ͍͚ͲͲ͏͢Δʁ What we lost after KMPnize? A1. Plan1. expect/actualΛ࢖ͬͯࣗલ࣮૷

    Plan2. kotlin.io.encoding.Base64(from Kotlin 1.8)Λ࢖͏ stdlibʹػೳ͕֦ு͞ΕΕ͹ࣗલͰͳͯ͘Α͍ ʢkotlin.io.encoding.Base64͸platform͝ͱʹ࣮૷͕෼͔ΕΔ͔֬ ఆ͍ͯ͠ͳ͍ͷͰ஫ҙ͢Δඞཁ͸͋Δʣ
  4. Source/Sink/Bu ff er/Segment Source/ Sink • {Input|Output}StreamʹରԠ͢ΔOkioಠࣗͷ interface • KtorͳͲͷϥΠϒϥϦ͸ࣗલͰಉ༷ͷ࣮૷Λ

    ߦͳ͍ͬͯͨΓ͢Δ • KMPʹ͸{Input|Output}Stream͕ͳ͍ͨΊɺ ͜ͷinterface͕KMPͱ࣮ͯ͠૷͞Ε͍ͯΔ͜ͱ ͕͋Γ͕͍ͨ
  5. Source/Sink/Bu ff er/Segment Buffer/ Segment • ݩʑ͸java.nio.Bufferͷ֦ுΛ໨తͱͨ͠Ϋϥε • Buffer͸࣮σʔλͷίϯςφͰ͋ΔSegmentͷ॥؀Ϧ ετͰ࣮૷͞Ε͍ͯΔͨΊՄม௕

    • σʔλͷҠಈΛSegmentͷ෇͚ସ͑ʹΑ࣮ͬͯݱ͢Δ • ҆શʹσʔλΛ௥ՃͰ͖Δ • ୯ʹByteArrayͷΠϯελϯεΛ৽͘͠࡞੒ͯ͠ί ϐʔ͢ΔΑΓলίετͰ஋ΛҠಈͰ͖Δ
  6. Source/Sink/Bu ff er/Segment Buffer/ Segment • ݩʑ͸java.nio.Bufferͷ֦ுΛ໨తͱͨ͠Ϋϥε • Buffer͸࣮σʔλͷίϯςφͰ͋ΔSegmentͷ॥؀Ϧ ετͰ࣮૷͞Ε͍ͯΔͨΊՄม௕

    • σʔλͷҠಈΛSegmentͷ෇͚ସ͑ʹΑ࣮ͬͯݱ͢Δ • ҆શʹσʔλΛ௥ՃͰ͖Δ • ୯ʹByteArrayͷΠϯελϯεΛ৽͘͠࡞੒ͯ͠ί ϐʔ͢ΔΑΓলίετͰ஋ΛҠಈͰ͖Δ sizeΛؾʹͨ͠ByteArray࣮૷͕ෆཁͰ͋Γ͕͍ͨ KMPͰ΋ಛੑͦͷ··ͳͷͰ͋Γ͕͍ͨ
  7. • Τϯίʔυॲཧ͖ͭByteArray • HEX, UTF-8, Base64, MD5, SHA256, HMAC-SHA͕ϝιουҰͭͰॲ ཧͰ͖Δ

    • UTF-8ΛΤϯίʔυ͢Δࡍ͸ϝϞϦ্ʹΩϟογϡͯ͘͠ΕΔͷͰ࠶ Τϯίʔυ࣌ͷύϑΥʔϚϯε͕Α͍ Big Love to ByteString
  8. • OkioશൠͰUTF-8Λલఏͱͨ͠IF͕૊·Ε͍ͯΔ • Java String͕಺෦తʹUTF-16ͰจࣈྻΛѻ͏ॾ໰୊Λߟྀ͠ͳͯ͘Α ͘ͳΔ • K/N͸Posix APIͰ͋Ε͜ΕΛૢ࡞͢ΔͷͰטΈ߹͍͕ྑ͍ •

    toString΋ࣗಈͰΤϯίʔυ͞ΕΔͷͰϓϦϯτσόοά͠΍͍͢ • e.g.) [size=0], [size=12, hex=AEADBFF] Big Love to ByteString ByteStringલఏͰ࣮૷͢Δͱ ೰ΉՕॴ͕ݮͬͯ͋Γ͕͍ͨ
  9. • ͦ΋ͦ΋JVM(android)͚ͩͰ΋ϑΝΠϧͷऔಘɺૢ࡞͸΍Γํ͕͍ͬͺ͍ ͋Δ • ClassLoader, TempDir, CacheDir, Asset, ExternalStorageɾɾɾɾ •

    ͡Ό͋ଞͷPlatform΋࣮૷͢Δͱͨ͠Βɾɾɾʁ • e.g.) Bundle, NSHomeDirectoryɾɾɾɾ • KotlinͷFileૢ࡞ܥͷAPI͸JVM onlyͷͨΊKMP͸ϑΝΠϧૢ࡞͕ۤखɾɾɾ FileSystem and Path
  10. • java.nio. fi le.FileSystemͱjava.nio. fi le.PathʹͦΕͧΕରԠ͢Δokio.FileSystemͱ okio.PathͰߏ੒͞ΕΔ • JVM͸java.io or

    java.nioɺNative͸Posix APIʹΑΔ࣮૷ • androidͷAssetɺNode΍WASMͳͲ֤Platformઐ༻ͷ֦ு΋༻ҙͯ͋͠Δ • ֤PlatformͷAPIͷ্ʹinterfaceΛ༻ҙ͞Ε͍ͯΔͷͰ • ϢʔβʔಠࣗͷFileSystem࣮૷΋͓खܰͰ͋Γ͕͍ͨ • ςελϏϦςΟ͕޲্ͯ͋͠Γ͕͍ͨ FileSystem and Path
  11. Shared CommonTest Resource with Okio • commonTest/resourcesʹ fi xture(json, proto...etc)Λ഑ஔ͢Δ૝ఆ

    • commonTest/resources഑ԼͷϑΝΠϧΛࢀর͢Δʹ͸Platform͝ͱ ʹ޻෉͕ඞཁ
  12. Shared CommonTest Resource with Okio androidUnitTest iosTest current directory project

    root NSFileManager#currentDirectoryPath /Users/$name/Library/Developer/ CoreSimulator/Devices/$UUID/data resource directory ClassLoaderܦ༝ src/androidUnitTest/resource NSBundle.mainBundle.bundlePath build/bin/ios${arch}/debugTest
  13. Shared CommonTest Resource with Okio androidUnitTest iosTest current directory project

    root NSFileManager#currentDirectoryPath /Users/$name/Library/Developer/ CoreSimulator/Devices/$UUID/data resource directory ClassLoaderܦ༝ src/androidUnitTest/resource NSBundle.mainBundle.bundlePath build/bin/ios${arch}/debugTest android΋iOS΋όϥόϥ😂
  14. • Base64΍HASHؔ਺पΓ͸࣮૷͞Ε͍ͯͳ͍ • CipherStream౳ͷJVM Speci fi cͳAPI͸࣮૷͞Ε͍ͯͳ͍ • FileSystem͕sealed interfaceͰએݴ͞Ε͍ͯͯಠࣗͷFileSystemΛ࡞

    ੒Ͱ͖ͳ͍ • FileSystemͷ࣮૷͕ΑΓPlatform Speci fi cͳAPIʹͳ͍ͬͯΔ • e.g.) iOSͷϑΝΠϧ؅ཧ͸NSFileManagerΛ࢖࣮ͬͯ૷ Okio? kotlinx-io?
  15. EOF