From 3c2ec0f516a28ba1eeea53e7699e72d9c2e692de Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Tue, 9 Oct 2018 22:33:14 +0200 Subject: [PATCH] add writeString() --- .../jwebassembly/binary/WasmOutputStream.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java index d110e32..4699b8e 100644 --- a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java +++ b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.module.ValueType; @@ -182,6 +183,20 @@ class WasmOutputStream extends FilterOutputStream { } } + /** + * Write a string as UTF8 encoded. + * + * @param str + * the string + * @throws IOException + * if any I/O error occur + */ + void writeString( @Nonnull String str ) throws IOException { + byte[] bytes = str.getBytes( StandardCharsets.UTF_8 ); + writeVaruint32( bytes.length ); + write( bytes ); + } + /** * Write a section with header and data. * @@ -203,9 +218,7 @@ class WasmOutputStream extends FilterOutputStream { writeVaruint32( type.ordinal() ); writeVaruint32( size ); if( type == SectionType.Custom ) { - byte[] bytes = name.getBytes( StandardCharsets.UTF_8 ); - writeVaruint32( bytes.length ); - write( bytes ); + writeString( name ); } baos.writeTo( this ); }