From ad2f5681b609ce38159fd93144cf38afe221b6b0 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 1 Dec 2019 18:13:10 +0300 Subject: [PATCH] Fix everything bu function server --- .../hep/dataforge/io/EnvelopeBuilder.kt | 10 ++++---- .../kotlin/hep/dataforge/io/EnvelopeParts.kt | 11 ++++++++- .../hep/dataforge/io/TaggedEnvelopeFormat.kt | 4 +--- .../hep/dataforge/io/TaglessEnvelopeFormat.kt | 2 +- .../kotlin/hep/dataforge/io/MultipartTest.kt | 23 +++++++++++-------- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeBuilder.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeBuilder.kt index a0b21b64..b5a32590 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeBuilder.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeBuilder.kt @@ -22,13 +22,13 @@ class EnvelopeBuilder { var name by metaBuilder.string(key = Envelope.ENVELOPE_NAME_KEY) /** - * Construct a binary and transform it into byte-array based buffer + * Construct a data binary from given builder */ + @ExperimentalIoApi fun data(block: Output.() -> Unit) { - val bytes = buildBytes { - block() - } - data = ArrayBinary(bytes.toByteArray()) + val bytes = buildBytes(builder = block) + data = bytes.toByteArray().asBinary() //save data to byte array so + bytes.close() } internal fun build() = SimpleEnvelope(metaBuilder.seal(), data) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeParts.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeParts.kt index d7d981c4..471ee3be 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeParts.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeParts.kt @@ -4,12 +4,15 @@ import hep.dataforge.context.Global import hep.dataforge.io.EnvelopeParts.FORMAT_META_KEY import hep.dataforge.io.EnvelopeParts.FORMAT_NAME_KEY import hep.dataforge.io.EnvelopeParts.INDEX_KEY +import hep.dataforge.io.EnvelopeParts.MULTIPART_DATA_SEPARATOR import hep.dataforge.io.EnvelopeParts.MULTIPART_DATA_TYPE import hep.dataforge.io.EnvelopeParts.SIZE_KEY import hep.dataforge.meta.* import hep.dataforge.names.asName import hep.dataforge.names.plus import hep.dataforge.names.toName +import kotlinx.io.text.readRawString +import kotlinx.io.text.writeRawString object EnvelopeParts { val MULTIPART_KEY = "multipart".asName() @@ -18,6 +21,8 @@ object EnvelopeParts { val FORMAT_NAME_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "format" val FORMAT_META_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "meta" + const val MULTIPART_DATA_SEPARATOR = "#~PART~#\r\n" + const val MULTIPART_DATA_TYPE = "envelope.multipart" } @@ -41,6 +46,7 @@ fun EnvelopeBuilder.multipart( data { format(formatMeta).run { envelopes.forEach { + writeRawString(MULTIPART_DATA_SEPARATOR) writeEnvelope(it) } } @@ -68,7 +74,8 @@ fun EnvelopeBuilder.multipart( format.run { var counter = 0 envelopes.forEach { (key, envelope) -> - writeObject(envelope) + writeRawString(MULTIPART_DATA_SEPARATOR) + writeEnvelope(envelope) meta { append(INDEX_KEY, buildMeta { "key" put key @@ -105,6 +112,8 @@ fun Envelope.parts(io: IOPlugin = Global.plugins.fetch(IOPlugin)): Sequence envelopes.sumBy { it.data!!.size.toInt() } } - val reconstructed = TaggedEnvelopeFormat.readByteArray(bytes) - val parts = reconstructed.parts()?.toList() ?: emptyList() - assertEquals(2, parts[2].meta["value"].int) - println(reconstructed.data!!.size) + TaggedEnvelopeFormat.run { + val singleEnvelopeData = writeBytes(envelopes[0]) + val singleEnvelopeSize = singleEnvelopeData.size + val bytes = writeBytes(partsEnvelope) + assertTrue(5*singleEnvelopeSize < bytes.size) + val reconstructed = bytes.readWith(this) + val parts = reconstructed.parts()?.toList() ?: emptyList() + assertEquals(2, parts[2].meta["value"].int) + println(reconstructed.data!!.size) + } } } \ No newline at end of file