From 49060e1e87836d313d0bc7bab4dffbcd0e5881f3 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Fri, 24 Mar 2017 22:48:28 +0100 Subject: [PATCH] Add compileToBinary() to the public interface --- .../jwebassembly/JWebAssembly.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index 7dc7292..cfbad7c 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -16,15 +16,18 @@ package de.inetsoftware.jwebassembly; import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.annotation.Nonnull; import de.inetsoftware.classparser.ClassFile; +import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter; import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.text.TextModuleWriter; @@ -82,6 +85,35 @@ public class JWebAssembly { } } + /** + * Convert the added files to a WebAssembly module in binary representation. + * + * @return the module as string + * @throws WasmException + * if any conversion error occurs + */ + public byte[] compileToBinary() throws WasmException { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + compileToBinary( output ); + return output.toByteArray(); + } + + /** + * Convert the added files to a WebAssembly module in binary representation. + * + * @param output + * the target for the module data + * @throws WasmException + * if any conversion error occurs + */ + public void compileToBinary( OutputStream output ) throws WasmException { + try (BinaryModuleWriter writer = new BinaryModuleWriter( output )) { + compile( writer ); + } catch( Exception ex ) { + throw WasmException.create( ex ); + } + } + /** * Convert the added files to a WebAssembly module. *