com.kirkwarez.kirkpack.io.uru.wdys
Class WDYSOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended byjava.io.FilterOutputStream
          extended bycom.kirkwarez.kirkpack.io.uru.wdys.WDYSOutputStream

public class WDYSOutputStream
extends FilterOutputStream

This stream streams in plain input data and streams out Uru WDYS encrypted data. Note that a length attribute is required upon construction. This is because the WDYS header requires the length of the unencrypted data to be set, and it cannot be determined from the stream. The encryption requires blocks of 2 times 4 bytes to work, and any padding will be done with zeroes.

Since:
3.00
Version:
3.00
Author:
Berend "Kirk" Wouda

Field Summary
protected  byte[] block
          The xTEA dual block buffer.
private static int[] KEY
          The key.
protected  int length
          The length of the unencrypted data.
protected  int total
          The amount of bytes written out.
protected  int unwritten
          How much there's yet to be written to the buffers to make them full.
static String WDYS_HEADER
          The WDYS header.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
WDYSOutputStream(OutputStream output, int length)
          Creates a stream over the given stream.
 
Method Summary
protected  int byteArrayToInt(byte[] bytes)
          Makes a byte array of an int.
protected  byte[][] encrypt(byte[][] v)
          The xTEA encryption algorithm.
 int getLength()
          Returns the length of the unencrypted data, as written to the fileheader.
protected  byte[] intToByteArray(int value)
          Makes an int out of a byte array.
protected  void setLength(int length)
          Sets the length of the unencrypted data, as written to the fileheader.
 void write(byte[] buffer, int offset, int length)
          Writes the given byte array to the underlying steam using the given subselection.
 void write(int value)
          Writes the given byte (well, int) to the underlying steam.
protected  void writePadding()
          Writes out padding to fill up the blocksize.
protected  void writeToBuffer(byte[] buffer, int offset, int length)
          Writes length bytes from the given array to the buffers from offset on.
 
Methods inherited from class java.io.FilterOutputStream
close, flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

block

protected byte[] block
The xTEA dual block buffer.


unwritten

protected int unwritten
How much there's yet to be written to the buffers to make them full. A value between 0 and 8.


length

protected int length
The length of the unencrypted data.


total

protected int total
The amount of bytes written out.


KEY

private static int[] KEY
The key. Ssh!


WDYS_HEADER

public static final String WDYS_HEADER
The WDYS header.

See Also:
Constant Field Values
Constructor Detail

WDYSOutputStream

public WDYSOutputStream(OutputStream output,
                        int length)
                 throws IOException
Creates a stream over the given stream.

Parameters:
output - The stream to write to.
length - The length of the data that will be written into this stream (needed for the header, and also for padding).
Throws:
IOException - When the given stream does not cooperate (we need to write the header and the length attribute).
Method Detail

write

public void write(int value)
           throws IOException
Writes the given byte (well, int) to the underlying steam. When the length indicated at construction time is reached, no more bytes are written out.

Parameters:
value - An int that represents a byte to be written out.
Throws:
IOException - When, you know, IO stuff.
See Also:
OutputStream.write(int)

write

public void write(byte[] buffer,
                  int offset,
                  int length)
           throws IOException
Writes the given byte array to the underlying steam using the given subselection. When the length indicated at construction time is reached, no more bytes are written out.

Parameters:
buffer - The byte buffer with data.
offset - The starting point of where to start getting the data.
length - The amount of data to write, taken from offset on.
Throws:
IOException
See Also:
OutputStream.write(byte[], int, int)

writePadding

protected void writePadding()
                     throws IOException
Writes out padding to fill up the blocksize. The blocksize is 8 bytes.

Throws:
IOException - When writing fails and stuff.

writeToBuffer

protected void writeToBuffer(byte[] buffer,
                             int offset,
                             int length)
                      throws IOException
Writes length bytes from the given array to the buffers from offset on.

Parameters:
buffer - The buffer to write the data from.
offset - The starting offset.
length - The amount of bytes to write.
Throws:
IOException - When your HDD explodes.

encrypt

protected byte[][] encrypt(byte[][] v)
The xTEA encryption algorithm.

Parameters:
v - The encrypted input blocks.
Returns:
The decrypted input blocks. These are not the same byte arrays, however the container byte array is.

intToByteArray

protected byte[] intToByteArray(int value)
Makes an int out of a byte array. It'll be using Little Endian, for the sake of the decryption algorithm implemented by this class.

Parameters:
value - The int to turn into a byte array.
Returns:
The byte array that used to be the int.

byteArrayToInt

protected int byteArrayToInt(byte[] bytes)
Makes a byte array of an int. It'll be using Little Endian, for the sake of the decryption algorithm implemented by this class.

Parameters:
bytes - The byte array to turn into an int.
Returns:
The int that used to be a byte array.

getLength

public int getLength()
Returns the length of the unencrypted data, as written to the fileheader.

Returns:
length The length of the unencrypted data.

setLength

protected void setLength(int length)
Sets the length of the unencrypted data, as written to the fileheader.

Parameters:
length - The length of the unencrypted data.