@Override public InputStream createDecryptionStream(InputStream in) { try { return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv)); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public OutputStream createEncryptionStream(OutputStream out) { if (!initialized) { reset(); } try { return new CryptoOutputStream(cipherMode, properties, out, key, new IvParameterSpec(iv)); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Overrides the {@link CryptoInputStream#close()}. Closes this input stream * and releases any system resources associated with the stream. * * @throws IOException if an I/O error occurs. */ @Override public void close() throws IOException { if (!isOpen()) { return; } cleanBufferPool(); super.close(); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { try { cis.close(); } finally { super.channelInactive(ctx); } } }
@Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { try { cos.close(); } finally { super.close(ctx, promise); } } }
/** * Overrides the {@link java.io.OutputStream#write(byte[])}. Writes the * specified byte to this output stream. * * @param b the data. * @throws IOException if an I/O error occurs. */ @Override public void write(int b) throws IOException { oneByteBuf[0] = (byte) (b & 0xff); write(oneByteBuf, 0, oneByteBuf.length); }
/** * Overrides the {@link java.io.InputStream#read()}. Reads the next byte of * data from the input stream. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @throws IOException if an I/O error occurs. */ @Override public int read() throws IOException { int n; while ((n = read(oneByteBuf, 0, 1)) == 0) { //NOPMD /* no op */ } return (n == -1) ? -1 : oneByteBuf[0] & 0xff; }
@Override public long getPos() throws IOException { return super.getStreamPosition(); }
@Override public void close() throws IOException { super.close(); }
/** Forcibly free the direct buffers. */ protected void freeBuffers() { CryptoInputStream.freeDirectBuffer(inBuffer); CryptoInputStream.freeDirectBuffer(outBuffer); }
/** * Reads the specified number of bytes from a given position within a * stream. This does not change the current offset of the stream and is * thread-safe. * * @param position the offset from the start of the stream. * @param buffer the buffer into which the data is read. * @throws IOException if an I/O error occurs. */ public void readFully(long position, byte[] buffer) throws IOException { readFully(position, buffer, 0, buffer.length); }
/** * Does final encryption of the last data. * * @throws IOException if an I/O error occurs. */ @Override protected void encryptFinal() throws IOException { // The same as the normal encryption for Counter mode encrypt(); }
@Override public int read(byte[] bytes, int offset, int length) throws IOException { return super.read(bytes, offset, length); }
@Override public void seek(long offset) throws IOException { super.seek(offset); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { try { cis.close(); } finally { super.channelInactive(ctx); } } }
@Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { try { cos.close(); } finally { super.close(ctx, promise); } } }
/** Forcibly free the direct buffers. */ protected void freeBuffers() { CryptoInputStream.freeDirectBuffer(inBuffer); CryptoInputStream.freeDirectBuffer(outBuffer); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { try { cis.close(); } finally { super.channelInactive(ctx); } } }
@Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { try { cos.close(); } finally { super.close(ctx, promise); } } }
/** Clean direct buffer pool */ private void cleanBufferPool() { ByteBuffer buf; while ((buf = bufferPool.poll()) != null) { CryptoInputStream.freeDirectBuffer(buf); } }