private SSLEngineResult unwrap( SSLEngine engine, ByteBuf in, int readerIndex, int len, ByteBuf out) throws SSLException { int nioBufferCount = in.nioBufferCount(); int writerIndex = out.writerIndex(); final SSLEngineResult result; if (engine instanceof OpenSslEngine && nioBufferCount > 1) { /** * If {@link OpenSslEngine} is in use, we can use a special {@link * OpenSslEngine#unwrap(ByteBuffer[], ByteBuffer[])} method that accepts multiple {@link * ByteBuffer}s without additional memory copies. */ OpenSslEngine opensslEngine = (OpenSslEngine) engine; try { singleBuffer[0] = toByteBuffer(out, writerIndex, out.writableBytes()); result = opensslEngine.unwrap(in.nioBuffers(readerIndex, len), singleBuffer); out.writerIndex(writerIndex + result.bytesProduced()); } finally { singleBuffer[0] = null; } } else { result = engine.unwrap( toByteBuffer(in, readerIndex, len), toByteBuffer(out, writerIndex, out.writableBytes())); } out.writerIndex(writerIndex + result.bytesProduced()); return result; }
@Override public boolean renameTo(File dest) throws IOException { if (dest == null) { throw new NullPointerException("dest"); } if (byteBuf == null) { // empty file dest.createNewFile(); isRenamed = true; return true; } int length = byteBuf.readableBytes(); FileOutputStream outputStream = new FileOutputStream(dest); FileChannel fileChannel = outputStream.getChannel(); int written = 0; if (byteBuf.nioBufferCount() == 1) { ByteBuffer byteBuffer = byteBuf.nioBuffer(); while (written < length) { written += fileChannel.write(byteBuffer); } } else { ByteBuffer[] byteBuffers = byteBuf.nioBuffers(); while (written < length) { written += fileChannel.write(byteBuffers); } } fileChannel.force(false); fileChannel.close(); outputStream.close(); isRenamed = true; return written == length; }
private void verifySignature( final ByteBuf buf, final int readerBefore, final int len, final boolean donePayload) throws SignatureException, InvalidKeyException { if (!message.isSign()) { return; } // if we read the complete data, we also read the signature // for the verification, we should not use this for the signature final int length = donePayload ? len - signatureFactory.signatureSize() : len; ByteBuffer[] byteBuffers = buf.nioBuffers(readerBefore, length); if (signature == null) { signature = signatureFactory.update(message.publicKey(0), byteBuffers); } else { for (int i = 0; i < byteBuffers.length; i++) { signature.update(byteBuffers[i]); } } if (donePayload) { byte[] signatureReceived = message.receivedSignature().encode(); LOG.debug("Verifying received signature: {}", Arrays.toString(signatureReceived)); if (signature.verify(signatureReceived)) { // set public key only if signature is correct message.setVerified(); LOG.debug("Signature check OK."); } else { LOG.warn("Signature check NOT OK. Message: {}.", message); } } }
public ByteBuf getBytes(int var1, ByteBuf var2, int var3, int var4) { this.checkDstIndex(var1, var4, var3, var2.capacity()); if (var2.hasArray()) { this.getBytes(var1, var2.array(), var2.arrayOffset() + var3, var4); } else if (var2.nioBufferCount() > 0) { ByteBuffer[] var5 = var2.nioBuffers(var3, var4); int var6 = var5.length; for (int var7 = 0; var7 < var6; ++var7) { ByteBuffer var8 = var5[var7]; int var9 = var8.remaining(); this.getBytes(var1, var8); var1 += var9; } } else { var2.setBytes(var3, (ByteBuf) this, var1, var4); } return this; }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * * @param buf the {@link ByteBuf} from which the bytes should be written * @return amount the amount of written bytes */ private int doWriteBytes(ByteBuf buf, int readable) throws Exception { int readerIndex = buf.readerIndex(); int localFlushedAmount; if (buf.nioBufferCount() == 1) { if (buf.hasMemoryAddress()) { localFlushedAmount = Native.writeAddress(fd, buf.memoryAddress(), readerIndex, buf.writerIndex()); } else { ByteBuffer nioBuf = buf.internalNioBuffer(readerIndex, readable); localFlushedAmount = Native.write(fd, nioBuf, nioBuf.position(), nioBuf.limit()); } } else { // backed by more then one buffer, do a gathering write... ByteBuffer[] nioBufs = buf.nioBuffers(); localFlushedAmount = (int) Native.writev(fd, nioBufs, 0, nioBufs.length); } if (localFlushedAmount > 0) { buf.readerIndex(readerIndex + localFlushedAmount); } return localFlushedAmount; }
private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException { ByteBuf newDirectIn = null; try { int readerIndex = in.readerIndex(); int readableBytes = in.readableBytes(); // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is // called. final ByteBuffer[] in0; if (in.isDirect() || !wantsDirectBuffer) { // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed // ByteBuf // to calculate the count) we will just assume a CompositeByteBuf contains more then 1 // ByteBuf. // The worst that can happen is that we allocate an extra ByteBuffer[] in // CompositeByteBuf.nioBuffers() // which is better then walking the composed ByteBuf in most cases. if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) { in0 = singleBuffer; // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object // allocation // to a minimum. in0[0] = in.internalNioBuffer(readerIndex, readableBytes); } else { in0 = in.nioBuffers(); } } else { // We could even go further here and check if its a CompositeByteBuf and if so try to // decompose it and // only replace the ByteBuffer that are not direct. At the moment we just will replace the // whole // CompositeByteBuf to keep the complexity to a minimum newDirectIn = alloc.directBuffer(readableBytes); newDirectIn.writeBytes(in, readerIndex, readableBytes); in0 = singleBuffer; in0[0] = newDirectIn.internalNioBuffer(0, readableBytes); } for (; ; ) { ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.wrap(in0, out0); in.skipBytes(result.bytesConsumed()); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: out.ensureWritable(maxPacketBufferSize); break; default: return result; } } } finally { // Null out to allow GC of ByteBuffer singleBuffer[0] = null; if (newDirectIn != null) { newDirectIn.release(); } } }
@Override public ByteBuffer[] nioBuffers(int index, int length) { return buf.nioBuffers(index, length); }
@Override public ByteBuffer[] nioBuffers() { return buf.nioBuffers(); }
@Override public ByteBuffer[] nioBuffers(int var1, int var2) { return a.nioBuffers(var1, var2); }