Skip to content

Commit

Permalink
~review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sandwwraith committed Sep 3, 2021
1 parent 9362f5f commit bd8c491
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.serialization.json.internal.*
import java.io.*

/**
* Serializes the [value] with [serializer] into a [stream] using JSON format and UTF-8 encoding..
* Serializes the [value] with [serializer] into a [stream] using JSON format and UTF-8 encoding.
*
* @throws [SerializationException] if the given value cannot be serialized to JSON.
* @throws [IOException] If an I/O error occurs and stream can't be written to.
Expand Down Expand Up @@ -71,6 +71,9 @@ public fun <T> Json.decodeFromStream(
* Deserializes the contents of given [stream] to to the value of type [T] using UTF-8 encoding and
* deserializer retrieved from the reified type parameter.
*
* Note that this functions expects that exactly one object would be present in the stream
* and throws an exception if there are any dangling bytes after an object.
*
* @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T].
* @throws [IOException] If an I/O error occurs and stream can't be read from.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization.json.internal

import java.io.OutputStream
Expand All @@ -15,7 +19,7 @@ internal class JsonToWriterStringBuilder(private val writer: Writer) : JsonStrin
val requiredSize = oldSize + additional
val currentSize = array.size
if (currentSize <= requiredSize) {
dumpAndReset(oldSize)
flush(oldSize)
if (additional > currentSize) {
// Handle strings that are longer than buffer:
// Ideally, we should make `ensureAdditionalCapacity` return boolean and fall back
Expand All @@ -28,13 +32,13 @@ internal class JsonToWriterStringBuilder(private val writer: Writer) : JsonStrin
return oldSize
}

private fun dumpAndReset(sz: Int = size) {
private fun flush(sz: Int = size) {
writer.write(array, 0, sz)
size = 0
}

override fun release() {
dumpAndReset()
flush()
writer.flush()
}
}
Expand Down

0 comments on commit bd8c491

Please sign in to comment.