From 7a854ed7866c1826cd2314831c0ffed6822fa08d Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 26 Feb 2020 10:05:59 +0100 Subject: [PATCH] Split the wasm stream for reused in generation data section stream --- .../jwebassembly/binary/WasmOutputStream.java | 59 +---------- .../wasm/LittleEndianOutputStream.java | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+), 56 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/wasm/LittleEndianOutputStream.java diff --git a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java index 6332a93..a8bedc9 100644 --- a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java +++ b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java @@ -16,7 +16,6 @@ package de.inetsoftware.jwebassembly.binary; import java.io.ByteArrayOutputStream; -import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; @@ -26,20 +25,19 @@ import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.wasm.AnyType; +import de.inetsoftware.jwebassembly.wasm.LittleEndianOutputStream; import de.inetsoftware.jwebassembly.wasm.ValueType; /** * @author Volker Berlin */ -class WasmOutputStream extends FilterOutputStream { - - private int count; +class WasmOutputStream extends LittleEndianOutputStream { /** * Create a in memory stream. */ WasmOutputStream() { - super( new ByteArrayOutputStream() ); + super(); } /** @@ -52,24 +50,6 @@ class WasmOutputStream extends FilterOutputStream { super( output ); } - /** - * {@inheritDoc} - */ - @Override - public void write( int b ) throws IOException { - out.write( b ); - count++; - } - - /** - * {@inheritDoc} - */ - @Override - public void write( byte[] b, int off, int len ) throws IOException { - out.write( b, off, len ); - count += len; - } - /** * Write a binary operation code. * @@ -145,21 +125,6 @@ class WasmOutputStream extends FilterOutputStream { } } - /** - * Write a integer little endian (ever 4 bytes) - * - * @param value - * the value - * @throws IOException - * if an I/O error occurs. - */ - void writeInt32( int value ) throws IOException { - write( value >>> 0 ); - write( value >>> 8 ); - write( value >>> 16 ); - write( value >>> 24 ); - } - /** * Write an unsigned integer. * @@ -312,22 +277,4 @@ class WasmOutputStream extends FilterOutputStream { ByteArrayOutputStream baos = (ByteArrayOutputStream)out; baos.writeTo( output ); } - - /** - * The count of bytes in the stream. - * - * @return the data size - */ - int size() { - return count; - } - - /** - * Reset the stream. Work only for in memory stream. - */ - void reset() { - ByteArrayOutputStream baos = (ByteArrayOutputStream)out; - baos.reset(); - count = 0; - } } diff --git a/src/de/inetsoftware/jwebassembly/wasm/LittleEndianOutputStream.java b/src/de/inetsoftware/jwebassembly/wasm/LittleEndianOutputStream.java new file mode 100644 index 0000000..aba4de0 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/wasm/LittleEndianOutputStream.java @@ -0,0 +1,99 @@ +/* + * Copyright 2020 Volker Berlin (i-net software) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.inetsoftware.jwebassembly.wasm; + +import java.io.ByteArrayOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * outputStream with little endian encoding like in Wasm. + * + * @author Volker Berlin + */ +public class LittleEndianOutputStream extends FilterOutputStream { + + private int count; + + /** + * Create a in memory stream. + */ + public LittleEndianOutputStream() { + super( new ByteArrayOutputStream() ); + } + + /** + * Create a wrapped stream. + * + * @param output + * the target of data + */ + public LittleEndianOutputStream( OutputStream output ) { + super( output ); + } + + /** + * {@inheritDoc} + */ + @Override + public void write( int b ) throws IOException { + out.write( b ); + count++; + } + + /** + * {@inheritDoc} + */ + @Override + public void write( byte[] b, int off, int len ) throws IOException { + out.write( b, off, len ); + count += len; + } + + /** + * Write a integer little endian (ever 4 bytes) + * + * @param value + * the value + * @throws IOException + * if an I/O error occurs. + */ + public void writeInt32( int value ) throws IOException { + write( value >>> 0 ); + write( value >>> 8 ); + write( value >>> 16 ); + write( value >>> 24 ); + } + + /** + * The count of bytes in the stream. + * + * @return the data size + */ + public int size() { + return count; + } + + /** + * Reset the stream. Work only for in memory stream. + */ + public void reset() { + ByteArrayOutputStream baos = (ByteArrayOutputStream)out; + baos.reset(); + count = 0; + } +}