diff options
author | David Hook <dgh@cryptoworkshop.com> | 2014-05-26 04:56:20 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2014-05-26 04:56:20 +0400 |
commit | bcb4a3c919781b429528bd554537a3aae84f9410 (patch) | |
tree | 2e84a3c92e74d4392cf3d3e60a61cb7b298f6a39 /core/src/main/java/org/bouncycastle/crypto | |
parent | 8a8f2e75d2d5d2ee1438f577b3d283f03031c29a (diff) |
added optional buffer setting, added general checking for skipping cipher, removed use of instanceof.
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java index 1589b149..d5662829 100644 --- a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java +++ b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java @@ -25,13 +25,16 @@ public class CipherInputStream { private static final int INPUT_BUF_SIZE = 2048; + private final SkippingCipher skippingCipher; + private final byte[] inBuf; + private BufferedBlockCipher bufferedBlockCipher; private StreamCipher streamCipher; private AEADBlockCipher aeadBlockCipher; private byte[] buf; private byte[] markBuf; - private final byte[] inBuf = new byte[INPUT_BUF_SIZE]; + private int bufOff; private int maxBuf; @@ -47,28 +50,73 @@ public class CipherInputStream InputStream is, BufferedBlockCipher cipher) { + this(is, cipher, INPUT_BUF_SIZE); + } + + /** + * Constructs a CipherInputStream from an InputStream and a StreamCipher. + */ + public CipherInputStream( + InputStream is, + StreamCipher cipher) + { + this(is, cipher, INPUT_BUF_SIZE); + } + + /** + * Constructs a CipherInputStream from an InputStream and an AEADBlockCipher. + */ + public CipherInputStream( + InputStream is, + AEADBlockCipher cipher) + { + this(is, cipher, INPUT_BUF_SIZE); + } + + /** + * Constructs a CipherInputStream from an InputStream and a + * BufferedBlockCipher. + */ + public CipherInputStream( + InputStream is, + BufferedBlockCipher cipher, + int bufSize) + { super(is); this.bufferedBlockCipher = cipher; + this.inBuf = new byte[bufSize]; + this.skippingCipher = (cipher instanceof SkippingCipher) ? (SkippingCipher)cipher : null; } + /** + * Constructs a CipherInputStream from an InputStream and a StreamCipher. + */ public CipherInputStream( InputStream is, - StreamCipher cipher) + StreamCipher cipher, + int bufSize) { super(is); this.streamCipher = cipher; + this.inBuf = new byte[bufSize]; + this.skippingCipher = (cipher instanceof SkippingCipher) ? (SkippingCipher)cipher : null; } /** * Constructs a CipherInputStream from an InputStream and an AEADBlockCipher. */ - public CipherInputStream(InputStream is, AEADBlockCipher cipher) + public CipherInputStream( + InputStream is, + AEADBlockCipher cipher, + int bufSize) { super(is); this.aeadBlockCipher = cipher; + this.inBuf = new byte[bufSize]; + this.skippingCipher = (cipher instanceof SkippingCipher) ? (SkippingCipher)cipher : null; } /** @@ -245,7 +293,7 @@ public class CipherInputStream return 0; } - if (streamCipher instanceof SkippingCipher) + if (skippingCipher != null) { int avail = available(); if (n <= avail) @@ -259,7 +307,7 @@ public class CipherInputStream long skip = in.skip(n - avail); - long cSkip = ((SkippingCipher)streamCipher).skip(skip); + long cSkip = skippingCipher.skip(skip); if (skip != cSkip) { @@ -371,11 +419,9 @@ public class CipherInputStream public void mark(int readlimit) { in.mark(readlimit); - if (streamCipher instanceof SkippingCipher) + if (skippingCipher != null) { - SkippingCipher skip = (SkippingCipher)streamCipher; - - markPosition = skip.getPosition(); + markPosition = skippingCipher.getPosition(); } if (buf != null) @@ -395,14 +441,14 @@ public class CipherInputStream public void reset() throws IOException { - if (!(streamCipher instanceof SkippingCipher)) + if (skippingCipher == null) { throw new IOException("cipher must implement SkippingCipher to be used with reset()"); } in.reset(); - SkippingCipher skip = (SkippingCipher)streamCipher; - skip.seekTo(markPosition); + + skippingCipher.seekTo(markPosition); if (markBuf != null) { @@ -420,7 +466,7 @@ public class CipherInputStream */ public boolean markSupported() { - if (streamCipher instanceof SkippingCipher) + if (skippingCipher != null) { return in.markSupported(); } |