Skip to content

Commit

Permalink
generalize the size() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Mar 31, 2019
1 parent 5e40f16 commit 82bf9f7
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2018 Volker Berlin (i-net software)
* Copyright 2017 - 2019 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.
Expand Down Expand Up @@ -32,6 +32,8 @@
*/
class WasmOutputStream extends FilterOutputStream {

private int count;

/**
* Create a in memory stream.
*/
Expand All @@ -49,6 +51,24 @@ 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.
*
Expand Down Expand Up @@ -245,13 +265,12 @@ void writeTo( OutputStream output ) throws IOException {
}

/**
* The count of bytes in the stream. Work only for in memory stream.
* The count of bytes in the stream.
*
* @return the data size
*/
int size() {
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
return baos.size();
return count;
}

/**
Expand All @@ -260,5 +279,6 @@ int size() {
void reset() {
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
baos.reset();
count = 0;
}
}

0 comments on commit 82bf9f7

Please sign in to comment.