mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 10:44:47 +01:00
Split the wasm stream for reused in generation data section stream
This commit is contained in:
parent
02696a6488
commit
7a854ed786
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user