From 940195cdf09f2617b9f4dc5e50aa20ad603cbac5 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Fri, 24 Mar 2017 18:19:25 +0100 Subject: [PATCH] Split function to more generic use --- .../jwebassembly/JWebAssembly.java | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index 7661ead..7dc7292 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -18,12 +18,14 @@ package de.inetsoftware.jwebassembly; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.annotation.Nonnull; import de.inetsoftware.classparser.ClassFile; +import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.text.TextModuleWriter; /** @@ -60,14 +62,40 @@ public class JWebAssembly { */ public String compileToText() throws WasmException { StringBuilder output = new StringBuilder(); + compileToText( output ); + return output.toString(); + } + + /** + * Convert the added files to a WebAssembly module in text representation. + * + * @param output + * the target for the module data + * @throws WasmException + * if any conversion error occurs + */ + public void compileToText( Appendable output ) throws WasmException { try (TextModuleWriter writer = new TextModuleWriter( output )) { - for( File file : classFiles ) { - ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) ); - writer.write( classFile ); - } + compile( writer ); } catch( Exception ex ) { throw WasmException.create( ex ); } - return output.toString(); + } + + /** + * Convert the added files to a WebAssembly module. + * + * @param writer + * the formatter + * @throws IOException + * if any I/O error occur + * @throws WasmException + * if any conversion error occurs + */ + private void compile( ModuleWriter writer ) throws IOException, WasmException { + for( File file : classFiles ) { + ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) ); + writer.write( classFile ); + } } }