add method compileToBinary(File)

This commit is contained in:
Volker Berlin 2017-04-02 10:04:23 +02:00
parent 7add526da1
commit 7d0f42c225

View File

@ -1,133 +1,150 @@
/* /*
* Copyright 2017 Volker Berlin (i-net software) * Copyright 2017 Volker Berlin (i-net software)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.inetsoftware.jwebassembly; package de.inetsoftware.jwebassembly;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.IOException;
import java.util.ArrayList; import java.io.OutputStream;
import java.util.List; import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nonnull;
import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter; import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
import de.inetsoftware.jwebassembly.text.TextModuleWriter; import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
/**
* The main class of the compiler. /**
* * The main class of the compiler.
* @author Volker Berlin *
*/ * @author Volker Berlin
public class JWebAssembly { */
public class JWebAssembly {
private List<File> classFiles = new ArrayList<File>();
private List<File> classFiles = new ArrayList<File>();
/**
* Create a instance. /**
*/ * Create a instance.
public JWebAssembly() { */
} public JWebAssembly() {
}
/**
* Add a classFile to compile /**
* * Add a classFile to compile
* @param classFile *
* the file * @param classFile
*/ * the file
public void addFile( @Nonnull File classFile ) { */
classFiles.add( classFile ); public void addFile( @Nonnull File classFile ) {
} classFiles.add( classFile );
}
/**
* Convert the added files to a WebAssembly module in text representation. /**
* * Convert the added files to a WebAssembly module in text representation.
* @return the module as string *
* @throws WasmException * @return the module as string
* if any conversion error occurs * @throws WasmException
*/ * if any conversion error occurs
public String compileToText() throws WasmException { */
StringBuilder output = new StringBuilder(); public String compileToText() throws WasmException {
compileToText( output ); StringBuilder output = new StringBuilder();
return output.toString(); compileToText( output );
} return output.toString();
}
/**
* Convert the added files to a WebAssembly module in text representation. /**
* * Convert the added files to a WebAssembly module in text representation.
* @param output *
* the target for the module data * @param output
* @throws WasmException * the target for the module data
* if any conversion error occurs * @throws WasmException
*/ * if any conversion error occurs
public void compileToText( Appendable output ) throws WasmException { */
try (TextModuleWriter writer = new TextModuleWriter( output )) { public void compileToText( Appendable output ) throws WasmException {
compile( writer ); try (TextModuleWriter writer = new TextModuleWriter( output )) {
} catch( Exception ex ) { compile( writer );
throw WasmException.create( ex ); } catch( Exception ex ) {
} throw WasmException.create( ex );
} }
}
/**
* Convert the added files to a WebAssembly module in binary representation. /**
* * Convert the added files to a WebAssembly module in binary representation.
* @return the module as string *
* @throws WasmException * @return the module as string
* if any conversion error occurs * @throws WasmException
*/ * if any conversion error occurs
public byte[] compileToBinary() throws WasmException { */
ByteArrayOutputStream output = new ByteArrayOutputStream(); public byte[] compileToBinary() throws WasmException {
compileToBinary( output ); ByteArrayOutputStream output = new ByteArrayOutputStream();
return output.toByteArray(); compileToBinary( output );
} return output.toByteArray();
}
/**
* Convert the added files to a WebAssembly module in binary representation. /**
* * Convert the added files to a WebAssembly module in binary representation.
* @param output *
* the target for the module data * @param file
* @throws WasmException * the target for the module data
* if any conversion error occurs * @throws WasmException
*/ * if any conversion error occurs
public void compileToBinary( OutputStream output ) throws WasmException { */
try (BinaryModuleWriter writer = new BinaryModuleWriter( output )) { public void compileToBinary( File file ) throws WasmException {
compile( writer ); try (FileOutputStream output = new FileOutputStream( file )) {
} catch( Exception ex ) { compileToBinary( output );
throw WasmException.create( ex ); } catch( Exception ex ) {
} throw WasmException.create( ex );
} }
}
/**
* Convert the added files to a WebAssembly module. /**
* * Convert the added files to a WebAssembly module in binary representation.
* @param writer *
* the formatter * @param output
* @throws IOException * the target for the module data
* if any I/O error occur * @throws WasmException
* @throws WasmException * if any conversion error occurs
* if any conversion error occurs */
*/ public void compileToBinary( OutputStream output ) throws WasmException {
private void compile( ModuleWriter writer ) throws IOException, WasmException { try (BinaryModuleWriter writer = new BinaryModuleWriter( output )) {
for( File file : classFiles ) { compile( writer );
ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) ); } catch( Exception ex ) {
writer.write( classFile ); throw WasmException.create( ex );
} }
} }
}
/**
* 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 );
}
}
}