Skip to content

Commit

Permalink
Add an example on how to encode using Kotlin (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
derlin authored Oct 15, 2021
1 parent 5f5f4ee commit 4f89eee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/modules/setup/examples/Encode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import java.util.Base64
import java.util.zip.Deflater

object Encode {

fun encode(decoded: String): String =
String(Base64.getUrlEncoder().encode(compress(decoded.toByteArray())), Charsets.UTF_8)

private fun compress(source: ByteArray): ByteArray {
val deflater = Deflater()
deflater.setInput(source)
deflater.finish()
val bytesCompressed = ByteArray(Short.MAX_VALUE.toInt())
val numberOfBytesAfterCompression = deflater.deflate(bytesCompressed)
val returnValues = ByteArray(numberOfBytesAfterCompression)
System.arraycopy(bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression)
return returnValues
}
}
9 changes: 9 additions & 0 deletions docs/modules/setup/pages/encode-diagram.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ On this page you will learn how to encode a diagram using:
- xref:javascript[]
- xref:python[]
- xref:java[]
- xref:kotlin[]
- xref:go[]
- xref:php[]
Expand Down Expand Up @@ -115,6 +116,14 @@ include::example$encode.py[]
include::example$Encode.java[]
----

[#kotlin]
== Kotlin

[source,kotlin]
----
include::example$Encode.kt[]
----

[#go]
== Go

Expand Down

0 comments on commit 4f89eee

Please sign in to comment.