use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class Channel method wrap.
private ByteBuf wrap(ByteBuf src) throws IOException {
SSLEngine engine = getSSLEngine();
ByteBufAllocator alloc = alloc();
ByteBuf out = null;
try {
if (ssl_handshake_finished) {
byte sslWrapExt = this.ssl_wrap_ext;
if (sslWrapExt == 0) {
out = alloc.allocate(guess_wrap_out(src.readableBytes(), 0xff + 1));
} else {
out = alloc.allocate(guess_wrap_out(src.readableBytes(), sslWrapExt & 0xff));
}
final int SSL_PACKET_BUFFER_SIZE = SslContext.SSL_PACKET_BUFFER_SIZE;
for (; ; ) {
SSLEngineResult result = engine.wrap(src.nioReadBuffer(), out.nioWriteBuffer());
Status status = result.getStatus();
sync_buf(src, out);
if (status == Status.CLOSED) {
return out;
} else if (status == Status.BUFFER_OVERFLOW) {
out.expansion(out.capacity() + SSL_PACKET_BUFFER_SIZE);
continue;
} else {
if (src.hasReadableBytes()) {
continue;
}
if (sslWrapExt == 0) {
int srcLen = src.writeIndex();
int outLen = out.readIndex();
int y = ((srcLen + 1) / SSL_PACKET_BUFFER_SIZE) + 1;
int u = ((outLen - srcLen) / y) * 2;
this.ssl_wrap_ext = (byte) u;
}
return out;
}
}
} else {
ByteBuf dst = FastThreadLocal.get().getSslWrapBuf();
for (; ; ) {
dst.clear();
SSLEngineResult result = engine.wrap(src.nioReadBuffer(), dst.nioWriteBuffer());
Status status = result.getStatus();
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
sync_buf(src, dst);
if (status == Status.CLOSED) {
return swap(alloc, dst);
}
if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
if (out != null) {
out.writeBytes(dst);
return out;
}
return swap(alloc, dst);
} else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
if (out == null) {
out = alloc.allocate(256);
}
out.writeBytes(dst);
continue;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
finish_handshake();
if (out != null) {
out.writeBytes(dst);
return out;
}
return swap(alloc, dst);
} else if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING) {
// It is shouldn't here to have "NOT_HANDSHAKING", because of the ssl is closed?
throw SSL_WRAP_CLOSED;
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
run_delegated_tasks(engine);
continue;
}
}
}
} catch (Throwable e) {
release(out);
if (e instanceof IOException) {
throw (IOException) e;
}
throw new IOException(e);
}
}
use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class TestExpansion method arrayPool.
@Test
public void arrayPool() throws Exception {
ByteBufAllocator alloc = TestAllocUtil.heap(1024 * 4);
ByteBuf buf = alloc.allocate(2);
for (int i = 0; i < 10; i++) {
buf.writeBytes(data);
buf.writeByte(a);
alloc.allocate(1);
}
Assert.assertTrue(v(buf));
}
use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class TestExpansion method directPool.
@Test
public void directPool() throws Exception {
ByteBufAllocator alloc = TestAllocUtil.direct(1024 * 4);
ByteBuf buf = alloc.allocate(2);
for (int i = 0; i < 10; i++) {
buf.writeBytes(data);
buf.writeByte(a);
alloc.allocate(1);
}
Assert.assertTrue(v(buf));
}
use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class TestDuplicatedHeapByteBuf method testDirectP.
@Test
public void testDirectP() throws Exception {
ByteBufAllocator a = TestAllocUtil.direct();
ByteBuf buf = a.allocate(16);
buf.writeBytes(data.getBytes());
ByteBuf buf2 = buf.duplicate();
v(buf2);
}
use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class TestDuplicatedHeapByteBuf method testHeapP.
@Test
public void testHeapP() throws Exception {
ByteBufAllocator a = TestAllocUtil.heap();
ByteBuf buf = a.allocate(16);
buf.writeBytes(data.getBytes());
ByteBuf buf2 = buf.duplicate();
v(buf2);
}
Aggregations