Mercurial > 510Connectbot
diff src/ch/ethz/ssh2/crypto/cipher/CipherOutputStream.java @ 308:42b15aaa7ac7 ganymed
merge
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 30 Jul 2014 14:21:50 -0700 |
parents | 071eccdff8ea |
children |
line wrap: on
line diff
--- a/src/ch/ethz/ssh2/crypto/cipher/CipherOutputStream.java Wed Jul 30 13:38:04 2014 -0700 +++ b/src/ch/ethz/ssh2/crypto/cipher/CipherOutputStream.java Wed Jul 30 14:21:50 2014 -0700 @@ -13,143 +13,122 @@ * @author Christian Plattner * @version $Id: CipherOutputStream.java 85 2014-04-07 14:05:09Z dkocher@sudo.ch $ */ -public class CipherOutputStream -{ - BlockCipher currentCipher; - OutputStream bo; - byte[] buffer; - byte[] enc; - int blockSize; - int pos; - - /* - * We cannot use java.io.BufferedOutputStream, since that is not available - * in J2ME. Everything could be improved here alot. - */ +public class CipherOutputStream { + BlockCipher currentCipher; + OutputStream bo; + byte[] buffer; + byte[] enc; + int blockSize; + int pos; - private static final int BUFF_SIZE = 8192; - byte[] out_buffer = new byte[BUFF_SIZE]; - int out_buffer_pos = 0; + /* + * We cannot use java.io.BufferedOutputStream, since that is not available + * in J2ME. Everything could be improved here alot. + */ - public CipherOutputStream(BlockCipher tc, OutputStream bo) - { - this.bo = bo; - changeCipher(tc); - } + private static final int BUFF_SIZE = 8192; + byte[] out_buffer = new byte[BUFF_SIZE]; + int out_buffer_pos = 0; - private void internal_write(byte[] src, int off, int len) throws IOException - { - while (len > 0) - { - int space = BUFF_SIZE - out_buffer_pos; - int copy = (len > space) ? space : len; - - System.arraycopy(src, off, out_buffer, out_buffer_pos, copy); + public CipherOutputStream(BlockCipher tc, OutputStream bo) { + this.bo = bo; + changeCipher(tc); + } - off += copy; - out_buffer_pos += copy; - len -= copy; + private void internal_write(byte[] src, int off, int len) throws IOException { + while (len > 0) { + int space = BUFF_SIZE - out_buffer_pos; + int copy = (len > space) ? space : len; + System.arraycopy(src, off, out_buffer, out_buffer_pos, copy); + off += copy; + out_buffer_pos += copy; + len -= copy; - if (out_buffer_pos >= BUFF_SIZE) - { - bo.write(out_buffer, 0, BUFF_SIZE); - out_buffer_pos = 0; - } - } - } + if (out_buffer_pos >= BUFF_SIZE) { + bo.write(out_buffer, 0, BUFF_SIZE); + out_buffer_pos = 0; + } + } + } + + private void internal_write(int b) throws IOException { + out_buffer[out_buffer_pos++] = (byte) b; - private void internal_write(int b) throws IOException - { - out_buffer[out_buffer_pos++] = (byte) b; - if (out_buffer_pos >= BUFF_SIZE) - { - bo.write(out_buffer, 0, BUFF_SIZE); - out_buffer_pos = 0; - } - } + if (out_buffer_pos >= BUFF_SIZE) { + bo.write(out_buffer, 0, BUFF_SIZE); + out_buffer_pos = 0; + } + } - public void flush() throws IOException - { - if (pos != 0) - { - throw new IOException("FATAL: cannot flush since crypto buffer is not aligned."); - } + public void flush() throws IOException { + if (pos != 0) { + throw new IOException("FATAL: cannot flush since crypto buffer is not aligned."); + } - if (out_buffer_pos > 0) - { - bo.write(out_buffer, 0, out_buffer_pos); - out_buffer_pos = 0; - } - bo.flush(); - } + if (out_buffer_pos > 0) { + bo.write(out_buffer, 0, out_buffer_pos); + out_buffer_pos = 0; + } + + bo.flush(); + } - public void changeCipher(BlockCipher bc) - { - this.currentCipher = bc; - blockSize = bc.getBlockSize(); - buffer = new byte[blockSize]; - enc = new byte[blockSize]; - pos = 0; - } + public void changeCipher(BlockCipher bc) { + this.currentCipher = bc; + blockSize = bc.getBlockSize(); + buffer = new byte[blockSize]; + enc = new byte[blockSize]; + pos = 0; + } - private void writeBlock() throws IOException - { - try - { - currentCipher.transformBlock(buffer, 0, enc, 0); - } - catch (Exception e) - { - throw new IOException("Error while decrypting block.", e); - } + private void writeBlock() throws IOException { + try { + currentCipher.transformBlock(buffer, 0, enc, 0); + } + catch (Exception e) { + throw new IOException("Error while decrypting block.", e); + } - internal_write(enc, 0, blockSize); - pos = 0; - } + internal_write(enc, 0, blockSize); + pos = 0; + } - public void write(byte[] src, int off, int len) throws IOException - { - while (len > 0) - { - int avail = blockSize - pos; - int copy = Math.min(avail, len); + public void write(byte[] src, int off, int len) throws IOException { + while (len > 0) { + int avail = blockSize - pos; + int copy = Math.min(avail, len); + System.arraycopy(src, off, buffer, pos, copy); + pos += copy; + off += copy; + len -= copy; - System.arraycopy(src, off, buffer, pos, copy); - pos += copy; - off += copy; - len -= copy; + if (pos >= blockSize) { + writeBlock(); + } + } + } - if (pos >= blockSize) - { - writeBlock(); - } - } - } + public void write(int b) throws IOException { + buffer[pos++] = (byte) b; + + if (pos >= blockSize) { + writeBlock(); + } + } - public void write(int b) throws IOException - { - buffer[pos++] = (byte) b; - if (pos >= blockSize) - { - writeBlock(); - } - } + public void writePlain(int b) throws IOException { + if (pos != 0) { + throw new IOException("Cannot write plain since crypto buffer is not aligned."); + } + + internal_write(b); + } - public void writePlain(int b) throws IOException - { - if (pos != 0) - { - throw new IOException("Cannot write plain since crypto buffer is not aligned."); - } - internal_write(b); - } + public void writePlain(byte[] b, int off, int len) throws IOException { + if (pos != 0) { + throw new IOException("Cannot write plain since crypto buffer is not aligned."); + } - public void writePlain(byte[] b, int off, int len) throws IOException - { - if (pos != 0) - { - throw new IOException("Cannot write plain since crypto buffer is not aligned."); - } - internal_write(b, off, len); - } + internal_write(b, off, len); + } }