Browse Source

Made some things protected instead of private, may or may not actually use them

crypto
Thomas Johnson 3 years ago
parent
commit
d31369247c
  1. 34
      crypto/PaddedInputStream.java
  2. 46
      crypto/PaddedOutputStream.java

34
crypto/PaddedInputStream.java

@ -5,29 +5,43 @@ import java.io.InputStream;
public class PaddedInputStream extends InputStream
{
private int length;
private int remaining;
private byte buffer[];
private InputStream underlying;
protected int length;
protected int received;
protected int head;
protected byte buffer[];
protected InputStream underlying;
public PaddedInputStream(InputStream underlying, int length)
{
this.underlying = underlying;
this.length = length;
this.remaining = 0;
this.received = 0;
this.head = 0;
this.buffer = new byte[length];
}
protected byte popBuffer()
{
head += 1;
return buffer[head - 1];
}
protected void readIntoBuffer() throws IOException
{
for (int ii = 0; ii < length; ii++)
buffer[ii] = (byte) underlying.read();
received = length - Byte.toUnsignedInt(buffer[length - 1]);
head = 0;
}
@Override
public int read() throws IOException
{
if (remaining == 0)
if (head >= received)
{
underlying.read(buffer, 0, length);
remaining = length - Byte.toUnsignedInt(buffer[length - 1]);
readIntoBuffer();
return read();
}
remaining -= 1;
return buffer[remaining];
return popBuffer();
}
}

46
crypto/PaddedOutputStream.java

@ -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();
}
}

Loading…
Cancel
Save