...

Text file src/github.com/google/flatbuffers/docs/source/KotlinUsage.md

Documentation: github.com/google/flatbuffers/docs/source

     1Use in Kotlin    {#flatbuffers_guide_use_kotlin}
     2==============
     3
     4## Before you get started
     5
     6Before diving into the FlatBuffers usage in Kotlin, it should be noted that
     7the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
     8general FlatBuffers usage in all of the supported languages (including K).
     9
    10This page is designed to cover the nuances of FlatBuffers usage, specific to Kotlin.
    11
    12You should also have read the [Building](@ref flatbuffers_guide_building)
    13documentation to build `flatc` and should be familiar with
    14[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
    15[Writing a schema](@ref flatbuffers_guide_writing_schema).
    16
    17## Kotlin and FlatBuffers Java code location
    18
    19Code generated for Kotlin currently uses the flatbuffers java runtime library. That means that Kotlin generated code can only have Java virtual machine as target architecture (which includes Android). Kotlin Native and Kotlin.js are currently not supported.
    20
    21The code for the FlatBuffers Java library can be found at
    22`flatbuffers/java/com/google/flatbuffers`. You can browse the library on the
    23[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
    24java/com/google/flatbuffers).
    25
    26## Testing FlatBuffers Kotlin
    27
    28The test code for Java is located in [KotlinTest.java](https://github.com/google
    29/flatbuffers/blob/master/tests/KotlinTest.kt).
    30
    31To run the tests, use  [KotlinTest.sh](https://github.com/google/
    32flatbuffers/blob/master/tests/KotlinTest.sh) shell script.
    33
    34*Note: These scripts require that [Kotlin](https://kotlinlang.org/) is installed.*
    35
    36## Using the FlatBuffers Kotlin library
    37
    38*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
    39example of how to use FlatBuffers in Kotlin.*
    40
    41FlatBuffers supports reading and writing binary FlatBuffers in Kotlin.
    42
    43To use FlatBuffers in your own code, first generate Java classes from your
    44schema with the `--kotlin` option to `flatc`.
    45Then you can include both FlatBuffers and the generated code to read
    46or write a FlatBuffer.
    47
    48For example, here is how you would read a FlatBuffer binary file in Kotlin:
    49First, import the library and generated code. Then, you read a FlatBuffer binary
    50file into a `ByteArray`.  You then turn the `ByteArray` into a `ByteBuffer`, which you
    51pass to the `getRootAsMyRootType` function:
    52
    53~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
    54    import MyGame.Example.*
    55    import com.google.flatbuffers.FlatBufferBuilder
    56
    57    // This snippet ignores exceptions for brevity.
    58    val data = RandomAccessFile(File("monsterdata_test.mon"), "r").use {
    59        val temp = ByteArray(it.length().toInt())
    60        it.readFully(temp)
    61        temp
    62    }
    63
    64    val bb = ByteBuffer.wrap(data)
    65    val monster = Monster.getRootAsMonster(bb)
    66~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    67
    68Now you can access the data from the `Monster monster`:
    69
    70~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
    71    val hp = monster.hp
    72    val pos = monster.pos!!;
    73~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    74
    75
    76
    77## Differences between Kotlin and Java code
    78
    79Kotlin generated code was designed to be as close as possible to the java counterpart, as for now, we only support kotlin on java virtual machine. So the differences in implementation and usage are basically the ones introduced by the Kotlin language itself. You can find more in-depth information [here](https://kotlinlang.org/docs/reference/comparison-to-java.html).
    80
    81The most obvious ones are:
    82
    83* Fields as accessed as Kotlin [properties](https://kotlinlang.org/docs/reference/properties.html)
    84* Static methods are accessed in [companion object](https://kotlinlang.org/docs/reference/classes.html#companion-objects)

View as plain text