@@ -0,0 +1,2707 @@
+package caesarxorstreams;%0A%0Aimport java.io.OutputStream;%0Aimport java.io.IOException;%0A%0Aimport static caesarxorstreams.CaesarCipher.*;%0A%0A/**%0A *  An output stream for encrypting/decrypting using Caesar cipher.%0A */%0Apublic class CaesarOutputStream extends OutputStream%0A%7B%0A  private OutputStream out;%0A  private int key;%0A  private boolean encrypt;%0A%0A  /**%0A   *  Creates a new CaesarOutputStream which encrypts/decrypts given OutputStream%0A   *  using the given key.%0A   *%0A   *  @param out     OutputStream to encrypt/decrypt%0A   *  @param key     encryption/decryption key%0A   *  @param encrypt encrypts output if true; decrypts output if false%0A   */%0A  public CaesarOutputStream(OutputStream out, int key, boolean encrypt) throws IOException%0A  %7B%0A    this.out     = out;%0A    this.key     = key;%0A    this.encrypt = encrypt;%0A  %7D%0A%0A  /**%0A   *  Creates a new CaesarOutputStream which decrypts given OutputStream%0A   *  using the given key.%0A   *%0A   *  @param out     OutputStream to encrypt/decrypt%0A   *  @param key     decryption key%0A   */%0A  public CaesarOutputStream(OutputStream out, int key) throws IOException%0A  %7B%0A    this(out, key, false);%0A  %7D%0A%0A  @Override%0A  /**%0A   *  Enciphers and writes the specified byte to this output stream.%0A   *%0A   *  @param b the byte%0A   */%0A  public void write(int b) throws IOException%0A  %7B%0A    if (encrypt)%0A      out.write(encipher(b, key));%0A    else%0A      out.write(decipher(b, key));%0A  %7D%0A%0A  @Override%0A  /**%0A   *  Writes b.length bytes from the specified byte array to this output stream.%0A   *%0A   *  @param b the data %0A   */%0A  public void write(byte%5B%5D b) throws IOException%0A  %7B%0A    int length = b.length;%0A%0A    if (length %3E 0)%0A    %7B%0A      if (encrypt)%0A        encipher(b, length, key);%0A      else%0A        decipher(b, length, key);%0A    %7D%0A%0A    out.write(b);%0A  %7D%0A%0A  @Override%0A  /**%0A   *  Writes len bytes from the specified byte array starting at offset off to this output stream.%0A   *%0A   *  @param b   the data.%0A   *  @param off the start offset in the data.%0A   *  @param len the number of bytes to write.%0A   */%0A  public void write(byte%5B%5D b, int off, int len) throws IOException%0A  %7B%0A    int length = Math.min(b.length, len);%0A%0A    if (length %3E 0)%0A    %7B%0A      if (encrypt)%0A        encipher(b, length, key);%0A      else%0A        decipher(b, length, key);%0A    %7D%0A%0A    out.write(b, off, len);%0A  %7D%0A%0A  /**%0A   *  Sets whether the stream should be encrypted or decrypted.%0A   *%0A   *  @param encrypt encrypts output if true; decrypts output if false%0A   */%0A  public void setEncrypt(boolean encrypt)%0A  %7B%0A    this.encrypt = encrypt;%0A  %7D%0A%0A  /**%0A   *  Sets the encrytion/decryption key to the given key.%0A   *%0A   *  @param key encryption/decryption key%0A   */%0A  public void setKey(int key)%0A  %7B%0A    this.key = key;%0A  %7D%0A%7D%0A

Implement CaesarOutputStream