|
|
@ -5,10 +5,10 @@ import java.io.OutputStream; |
|
|
|
|
|
|
|
public class PaddedOutputStream extends OutputStream |
|
|
|
{ |
|
|
|
private byte length; |
|
|
|
private byte accumulated; |
|
|
|
private byte buffer[]; |
|
|
|
private OutputStream underlying; |
|
|
|
protected byte length; |
|
|
|
protected byte accumulated; |
|
|
|
protected byte buffer[]; |
|
|
|
protected OutputStream underlying; |
|
|
|
|
|
|
|
public PaddedOutputStream(OutputStream underlying, byte length) |
|
|
|
{ |
|
|
@ -18,16 +18,37 @@ public class PaddedOutputStream extends OutputStream |
|
|
|
this.accumulated = 0; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void write(int i) throws IOException |
|
|
|
protected boolean addToBuffer(int i) |
|
|
|
{ |
|
|
|
buffer[accumulated] = (byte) i; |
|
|
|
accumulated += 1; |
|
|
|
if (accumulated + 1 == length) |
|
|
|
{ |
|
|
|
underlying.write(buffer); |
|
|
|
underlying.write((byte) 1); |
|
|
|
accumulated = 0; |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
protected void writeBuffer() throws IOException |
|
|
|
{ |
|
|
|
underlying.write(buffer, 0, accumulated); |
|
|
|
for (int ii = 0; ii < length - accumulated; ii++) { |
|
|
|
underlying.write(length - accumulated); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected void resetBuffer() |
|
|
|
{ |
|
|
|
accumulated = 0; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void write(int i) throws IOException |
|
|
|
{ |
|
|
|
if (addToBuffer(i)) |
|
|
|
{ |
|
|
|
writeBuffer(); |
|
|
|
resetBuffer(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -35,11 +56,8 @@ public class PaddedOutputStream extends OutputStream |
|
|
|
public void flush() throws IOException |
|
|
|
{ |
|
|
|
if (accumulated != 0) { |
|
|
|
underlying.write(buffer, 0, accumulated); |
|
|
|
for (int ii = 0; ii < length - accumulated; ii++) { |
|
|
|
underlying.write(length - accumulated); |
|
|
|
} |
|
|
|
accumulated = 0; |
|
|
|
writeBuffer(); |
|
|
|
resetBuffer(); |
|
|
|
underlying.flush(); |
|
|
|
} |
|
|
|
} |
|
|
|