Slide 1

Slide 1 text

OkioʹѪΛࠐΊͯ RyuNen344 2024.06.22 Kotlin Fest

Slide 2

Slide 2 text

Today's TL;DR

Slide 3

Slide 3 text

Today's TL;DR ೰ΉͳΒ͍ͬͦOkio

Slide 4

Slide 4 text

• 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

Slide 5

Slide 5 text

What we get after Kotlinize?

Slide 6

Slide 6 text

Null Safety What we get after Kotlinize? એݴΛݟΔ͚ͩͰnullΛ ͱΓ͏Δͷ͔͕Θ͔Δ

Slide 7

Slide 7 text

Immutability What we get after Kotlinize? ෆมΛ༏ઌͱͨ͠APIઃܭ

Slide 8

Slide 8 text

Unboxed Primitive Types What we get after Kotlinize? ΑΓݎ࿚ͳ੩తܕγεςϜ

Slide 9

Slide 9 text

Null Safety What we get after Kotlinize? Immutability Unboxed Primitive Types

Slide 10

Slide 10 text

Null Safety Unboxed Primitive Types Immutability What we get after Kotlinize? ࣦͬͨ΋ͷ͸ͳ͍ɾɾɾʁ

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

What we lost after Kotlinize? Q. ຊ౰ʹʁ A. ৚݅෇͖(ݸਓͷҙݟؚΉ)

Slide 13

Slide 13 text

• Primitive Array͸Immutableͱͯ͠એݴ͍ͯͯ͠΋஋ΛมߋͰ͖Δ What we lost after Kotlinize?

Slide 14

Slide 14 text

• ΑΓݎ࿚ͳܕγεςϜʹͳͬͨͨΊNumberͱPrimitiveͳArrayཁૉͷ ҉໧తܕม׵͕ࣦΘΕΔέʔε͕͋Δ • e.g.) Java: byte[]ͱKotlin: ByteArray What we lost after Kotlinize?

Slide 15

Slide 15 text

• ΑΓݎ࿚ͳܕγεςϜʹͳͬͨͨΊNumberͱPrimitiveͳArrayཁૉͷ ҉໧తܕม׵͕ࣦΘΕΔέʔε͕͋Δ What we lost after Kotlinize?

Slide 16

Slide 16 text

• ΑΓݎ࿚ͳܕγεςϜʹͳͬͨͨΊNumberͱPrimitiveͳArrayཁૉͷ ҉໧తܕม׵͕ࣦΘΕΔέʔε͕͋Δ What we lost after Kotlinize? intͱbyteͷ҉໧తͳม׵

Slide 17

Slide 17 text

• ΑΓݎ࿚ͳܕγεςϜʹͳͬͨͨΊNumberͱPrimitiveͳArrayཁૉͷ ҉໧తܕม׵͕ࣦΘΕΔέʔε͕͋Δ What we lost after Kotlinize? Int΋͘͠͸Byte΁ͷ໌ࣔతͳม׵

Slide 18

Slide 18 text

What we lost after KMPnize?

Slide 19

Slide 19 text

What we lost after KMPnize? java.* API ؙͬͱ࢖͑ͳ͍

Slide 20

Slide 20 text

Q. java.* API͕࢖͑ͳ͍ͱͲ͏ͳΔʁ What we lost after KMPnize? A. ࣮૷͕େมʹͳΔύλʔϯ͕͋Δ

Slide 21

Slide 21 text

Q1. java.util.Base64࢖͑ͳ͍͚ͲͲ͏͢Δʁ What we lost after KMPnize? A1. Plan1. expect/actualΛ࢖ͬͯࣗલ࣮૷ Plan2. kotlin.io.encoding.Base64(from Kotlin 1.8)Λ࢖͏

Slide 22

Slide 22 text

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͝ͱʹ࣮૷͕෼͔ΕΔ͔֬ ఆ͍ͯ͠ͳ͍ͷͰ஫ҙ͢Δඞཁ͸͋Δʣ

Slide 23

Slide 23 text

Q2. java.time API࢖͑ͳ͍͚ͲͲ͏͢Δʁ What we lost after KMPnize? A2. kotlinx-datetimeΛಋೖ͢Δ

Slide 24

Slide 24 text

Q2. java.time API࢖͑ͳ͍͚ͲͲ͏͢Δʁ What we lost after KMPnize? A2. kotlinx-datetimeΛಋೖ͢Δ ެࣜϥΠϒϥϦΛಋೖ͢Δ͜ͱͰղܾ͢ΔͷͰ͋Ε͹ͦΕͰΑ͍

Slide 25

Slide 25 text

Q3. java.io.{Input|Output}Stream͸ʁjava.io.File͸ʁ What we lost after KMPnize? A3.

Slide 26

Slide 26 text

Q3. java.io.{Input|Output}Stream͸ʁjava.io.File͸ʁ What we lost after KMPnize? A3. OkioΛೖΕ·͠ΐ͏ kotlinx-io͋Δ͚Ͳͦͷ࿩͸͋ͱͰ....

Slide 27

Slide 27 text

• Block(چSquare)͕ࣾ؅ཧ͢ΔOSS • ݩʑ͸OkHttpͷҰ෦ͱͯ͠java.io, java.nioͷऔΓճ͠ΛΑ͘͢Δͨ Ίʹ࡞ΒΕ͍ͯͨ • MoshiɺRetro fi tͰ΋core෦෼͸OkioΛ࢖࣮ͬͯ૷͞Ε͍ͯΔ About Okio

Slide 28

Slide 28 text

Source/Sink/Bu ff er/Segment Source/ Sink Buffer/ Segment

Slide 29

Slide 29 text

Source/Sink/Bu ff er/Segment Source/ Sink • {Input|Output}StreamʹରԠ͢ΔOkioಠࣗͷ interface • KtorͳͲͷϥΠϒϥϦ͸ࣗલͰಉ༷ͷ࣮૷Λ ߦͳ͍ͬͯͨΓ͢Δ • KMPʹ͸{Input|Output}Stream͕ͳ͍ͨΊɺ ͜ͷinterface͕KMPͱ࣮ͯ͠૷͞Ε͍ͯΔ͜ͱ ͕͋Γ͕͍ͨ

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

• Τϯίʔυॲཧ͖ͭByteArray • HEX, UTF-8, Base64, MD5, SHA256, HMAC-SHA͕ϝιουҰͭͰॲ ཧͰ͖Δ • UTF-8ΛΤϯίʔυ͢Δࡍ͸ϝϞϦ্ʹΩϟογϡͯ͘͠ΕΔͷͰ࠶ Τϯίʔυ࣌ͷύϑΥʔϚϯε͕Α͍ Big Love to ByteString

Slide 33

Slide 33 text

• 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

Slide 34

Slide 34 text

• 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લఏͰ࣮૷͢Δͱ ೰ΉՕॴ͕ݮͬͯ͋Γ͕͍ͨ

Slide 35

Slide 35 text

• ͦ΋ͦ΋JVM(android)͚ͩͰ΋ϑΝΠϧͷऔಘɺૢ࡞͸΍Γํ͕͍ͬͺ͍ ͋Δ • ClassLoader, TempDir, CacheDir, Asset, ExternalStorageɾɾɾɾ • ͡Ό͋ଞͷPlatform΋࣮૷͢Δͱͨ͠Βɾɾɾʁ • e.g.) Bundle, NSHomeDirectoryɾɾɾɾ • KotlinͷFileૢ࡞ܥͷAPI͸JVM onlyͷͨΊKMP͸ϑΝΠϧૢ࡞͕ۤखɾɾɾ FileSystem and Path

Slide 36

Slide 36 text

• 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

Slide 37

Slide 37 text

• ࣮ࡍʹͲΕ͙Β͍ίʔυ͕ݮΔͷ͔ʁ • ByteStringɿJWS(HS256) • FileSystem,PathɿcommonTestͷڞ௨Ϧιʔε How much does okio ommit our codes? Okioͷ༗ແͰൺֱͯ͠Έ·͠ΐ͏

Slide 38

Slide 38 text

Create JWS(HS256) with Okio eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMj M0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxN TE2MjM5MDIyfQ.kK9JnTXZwzNo3BYNXJT57PGLnQk- Xyu7IBhRWFmc4C0

Slide 39

Slide 39 text

Create JWS(HS256) with Okio eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMj M0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxN TE2MjM5MDIyfQ.kK9JnTXZwzNo3BYNXJT57PGLnQk- Xyu7IBhRWFmc4C0 ${header}.${payload}.${signature}

Slide 40

Slide 40 text

$header = base64url(json) $payload = base64url(json) $signature = base64url(HMAC-SHA256($ {header}.${payload})) $JWS = ${header}.${payload}. ${signature} Create JWS(HS256) with Okio

Slide 41

Slide 41 text

expect/actualͰ࡞Βͳ͖Ό͍͚ͳ͍΋ͷ͸ɾɾɾɾ 1. Base64ɿjava.util.Base64͕ແ͍ 2. HMAC-SHA256ɿjavax.crypto.Mac͕ແ͍ Create JWS(HS256) with Okio

Slide 42

Slide 42 text

Create JWS(HS256) with Okio

Slide 43

Slide 43 text

Create JWS(HS256) with Okio

Slide 44

Slide 44 text

Create JWS(HS256) with Okio

Slide 45

Slide 45 text

Create JWS(HS256) with Okio ७ਮʹ࣮૷͕ଟ͍ Platform͕૿͑Ε͹ͦͷ෼࣮૷΋૿͑Δ

Slide 46

Slide 46 text

Create JWS(HS256) with Okio

Slide 47

Slide 47 text

Create JWS(HS256) with Okio commonίʔυ͚ͩͰ׬݁ͯ͋͠Γ͕͍ͨ ϓϦϯτσόοά΋͠΍ͯ͋͘͢Γ͕͍ͨ

Slide 48

Slide 48 text

Shared CommonTest Resource with Okio • commonTest/resourcesʹ fi xture(json, proto...etc)Λ഑ஔ͢Δ૝ఆ • commonTest/resources഑ԼͷϑΝΠϧΛࢀর͢Δʹ͸Platform͝ͱ ʹ޻෉͕ඞཁ

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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΋όϥόϥ😂

Slide 51

Slide 51 text

Shared CommonTest Resource with Okio • commonTest/resourcesͷ fi leΛࢀর͢Δexpect/actualΛ࡞੒͢Δ

Slide 52

Slide 52 text

Shared CommonTest Resource with Okio • commonTest/resourcesͷ fi leΛࢀর͢Δexpect/actualΛ࡞੒͢Δ ϑΝΠϧܾΊଧͪͰ͸औಘͰ͖Δ͕ ϑΝΠϧҰཡΛऔಘ͢Δ౳͕Ͱ͖ͳ͍

Slide 53

Slide 53 text

Shared CommonTest Resource with Okio • commonTest/resourcesͷPathΛฦ٫͢Δexpect/actualΛ࡞੒͢Δ

Slide 54

Slide 54 text

Shared CommonTest Resource with Okio • commonTest/resourcesͷPathΛฦ٫͢Δexpect/actualΛ࡞੒͢Δ

Slide 55

Slide 55 text

Shared CommonTest Resource with Okio • commonTest/resourcesͷPathΛฦ٫͢Δexpect/actualΛ࡞੒͢Δ OkioͷFileSystemͱPathΛ࢖͏͜ͱͰ ֦ுੑߴ͘resourceΛಡΈࠐΊΔΑ͏ʹͳͬͯ͋Γ͕͍ͨ

Slide 56

Slide 56 text

Okio? kotlinx-io? Q. ެࣜϥΠϒϥϦɺͳ͍ͷʁ A. ͋Γ·͢

Slide 57

Slide 57 text

Okio? kotlinx-io? Q. ެࣜϥΠϒϥϦɺͳ͍ͷʁ A. ͋Γ·͢

Slide 58

Slide 58 text

• Kotlin/kotlinx-io • ͠͹Β͘։ൃ͕ࢭ·͍͕ͬͯͨ1೥લ͙Β͍͔Β࠶։͞Εͨ experimentalͳϥΠϒϥϦʔ • جຊίϯηϓτ͸OkioͷSource, Sink, Buffer, Segment, ByteStringͱ ڞ௨ • ktor-io͸kotlinx-ioʹஔ͖׵͍͑ͯ͘༧ఆʢKTOR-6036ʣ Okio? kotlinx-io?

Slide 59

Slide 59 text

• kotlinx-ioͷ։ൃ͕࠶։͞ΕΔͷʹؾ͍ͮͨࣗ෼͕ڻ͍͍ͯΔπΠʔτ ͕ͪΐ͏Ͳ1೥લ͙Β͍Ͱͨ͠ Okio? kotlinx-io? https://twitter.com/RyuNen344/status/1668992754704105480

Slide 60

Slide 60 text

• 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?

Slide 61

Slide 61 text

Summary • KMP͸IO͕ۤख • OkioΛ࢖͏ͱจࣈΤϯίʔυɺόΠτ഑ྻૢ࡞Ͱ೰Ή৔໘͕ݮΔ • kotlinx-io΋։ൃ͞Ε͍ͯΔ͕ݱঢ়͸·ͩOkioΑΓػೳ͕ͳ͍

Slide 62

Slide 62 text

About Me • ଟ෼5೥͘Β͍androidΞϓϦ։ൃऀ • KMP޷͖ • Kodee͘Μͷ͵͍͙ΔΈ͕ཉ͍͠

Slide 63

Slide 63 text

OkioʹIOࠐΊͯ w w 2024.06.22 Kotlin Fest RyuNen344

Slide 64

Slide 64 text

Refs • https://square.github.io/okio/ • https://developer.squareup.com/blog/kotlin-multiplatform-shared- test-resources/ • https://x.com/ttuusskk/status/1722922182475694293/

Slide 65

Slide 65 text

EOF