3 changed files with 92 additions and 14 deletions
@ -0,0 +1,5 @@ |
|||
package crypto; |
|||
|
|||
public class CryptoOutputSocket |
|||
{ |
|||
} |
@ -0,0 +1,63 @@ |
|||
package crypto; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.OutputStream; |
|||
|
|||
public class InterleavedOutput |
|||
{ |
|||
private byte length; |
|||
private byte buffers[][]; |
|||
private int heads[]; |
|||
private byte nstreams; |
|||
private byte buffer; |
|||
|
|||
private OutputStream underlying; |
|||
|
|||
public InterleavedOutput(OutputStream underlying, byte length, byte nstreams) |
|||
{ |
|||
this.underlying = underlying; |
|||
this.length = length; |
|||
this.nstreams = nstreams; |
|||
this.buffers = new byte[Byte.toUnsignedInt(nstreams)][Byte.toUnsignedInt(length)]; |
|||
} |
|||
|
|||
synchronized void writeBuffer(byte which) throws IOException |
|||
{ |
|||
underlying.write(which); |
|||
underlying.write(buffers[Byte.toUnsignedInt(which)]); |
|||
} |
|||
|
|||
void writeTo(byte which, byte v) throws IOException |
|||
{ |
|||
int idx = Byte.toUnsignedInt(which); |
|||
buffers[idx][heads[idx]] = v; |
|||
if (heads[idx] == length) |
|||
{ |
|||
writeBuffer(which); |
|||
heads[idx] = 0; |
|||
} |
|||
} |
|||
|
|||
public PaddedOutputStream getStream(byte which) throws IOException |
|||
{ |
|||
if (Byte.toUnsignedInt(which) >= nstreams) |
|||
throw new IOException("Stream number too high"); |
|||
return new PaddedOutputStream(new InterleavedOutputStream(which), length); |
|||
} |
|||
|
|||
class InterleavedOutputStream extends OutputStream |
|||
{ |
|||
private byte which; |
|||
|
|||
public InterleavedOutputStream(byte which) |
|||
{ |
|||
this.which = which; |
|||
} |
|||
|
|||
@Override |
|||
public void write(int i) throws IOException |
|||
{ |
|||
writeTo(which, (byte) i); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue