1 package org.bouncycastle.crypto; 2 3 /** 4 * A parent class for block cipher modes that do not require block aligned data to be processed, but can function in 5 * a streaming mode. 6 */ 7 public abstract class StreamBlockCipher 8 implements BlockCipher, StreamCipher 9 { 10 private final BlockCipher cipher; 11 StreamBlockCipher(BlockCipher cipher)12 protected StreamBlockCipher(BlockCipher cipher) 13 { 14 this.cipher = cipher; 15 } 16 17 /** 18 * return the underlying block cipher that we are wrapping. 19 * 20 * @return the underlying block cipher that we are wrapping. 21 */ getUnderlyingCipher()22 public BlockCipher getUnderlyingCipher() 23 { 24 return cipher; 25 } 26 returnByte(byte in)27 public final byte returnByte(byte in) 28 { 29 return calculateByte(in); 30 } 31 processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)32 public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) 33 throws DataLengthException 34 { 35 if (inOff + len > in.length) 36 { 37 throw new DataLengthException("input buffer too small"); 38 } 39 if (outOff + len > out.length) 40 { 41 throw new OutputLengthException("output buffer too short"); 42 } 43 44 int inStart = inOff; 45 int inEnd = inOff + len; 46 int outStart = outOff; 47 48 while (inStart < inEnd) 49 { 50 out[outStart++] = calculateByte(in[inStart++]); 51 } 52 53 return len; 54 } 55 calculateByte(byte b)56 protected abstract byte calculateByte(byte b); 57 }