List of usage examples for io.netty.buffer ByteBuf isDirect
public abstract boolean isDirect();
From source file:org.vertx.java.core.http.impl.VertxHttpHandler.java
License:Open Source License
@Override protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception { if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; ByteBuf buf = content.content(); if (buf != Unpooled.EMPTY_BUFFER && buf.isDirect()) { ByteBuf newBuf = safeBuffer(content, allocator); if (msg instanceof LastHttpContent) { LastHttpContent last = (LastHttpContent) msg; return new AssembledLastHttpContent(newBuf, last.trailingHeaders()); } else { return new DefaultHttpContent(newBuf); }//from www .j a v a 2 s. com } } else if (msg instanceof WebSocketFrame) { ByteBuf payload = safeBuffer((WebSocketFrame) msg, allocator); boolean isFinal = ((WebSocketFrame) msg).isFinalFragment(); org.vertx.java.core.http.WebSocketFrame.FrameType frameType; if (msg instanceof BinaryWebSocketFrame) { frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.BINARY; } else if (msg instanceof CloseWebSocketFrame) { frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.CLOSE; } else if (msg instanceof PingWebSocketFrame) { frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.PING; } else if (msg instanceof PongWebSocketFrame) { frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.PONG; } else if (msg instanceof TextWebSocketFrame) { frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.TEXT; } else if (msg instanceof ContinuationWebSocketFrame) { frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.CONTINUATION; } else { throw new IllegalStateException("Unsupported websocket msg " + msg); } return new DefaultWebSocketFrame(frameType, payload, isFinal); } return msg; }
From source file:uk.co.thinkofdeath.prismarine.network.CipherCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { byte[] data;// ww w . jav a 2 s . com int offset = 0; int dataSize; if (!msg.isDirect()) { data = msg.array(); offset = msg.arrayOffset(); dataSize = msg.readableBytes(); msg.skipBytes(msg.readableBytes()); } else { dataSize = msg.readableBytes(); if (dataBuffer.length < dataSize) { dataBuffer = new byte[dataSize]; } msg.readBytes(dataBuffer, 0, dataSize); data = dataBuffer; } int size = cipherEncrypt.getOutputSize(msg.readableBytes()); if (encryptBuffer.length < size) { encryptBuffer = new byte[size]; } int count = cipherEncrypt.update(data, offset, dataSize, encryptBuffer); out.writeBytes(encryptBuffer, 0, count); }
From source file:uk.co.thinkofdeath.prismarine.network.CipherCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { byte[] data;/*ww w . j a va2s . co m*/ int offset = 0; int dataSize = in.readableBytes(); if (!in.isDirect()) { data = in.array(); offset = in.arrayOffset(); in.skipBytes(in.readableBytes()); } else { if (deDataBuffer.length < dataSize) { deDataBuffer = new byte[dataSize]; } in.readBytes(deDataBuffer, 0, dataSize); data = deDataBuffer; } int size = cipherDecrypt.getOutputSize(dataSize); ByteBuf buf = ctx.alloc().heapBuffer(size); buf.writerIndex(cipherDecrypt.update(data, offset, dataSize, buf.array(), buf.arrayOffset())); out.add(buf); }
From source file:uk.co.thinkofdeath.prismarine.network.CompressionCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { MCByteBuf buf = new MCByteBuf(out); if (msg.readableBytes() < threshold) { buf.writeVarInt(0);/*w w w . j av a 2s. c om*/ buf.writeBytes(msg); } else { CompressionInfo ci = info.get(); byte[] data; int offset = 0; int dataSize; // Handle both direct and heap buffers // heap buffers are faster in this case as they // do not require a copy if (!msg.isDirect()) { data = msg.array(); offset = msg.arrayOffset(); dataSize = msg.readableBytes(); msg.skipBytes(msg.readableBytes()); } else { dataSize = msg.readableBytes(); if (ci.dataBuffer.length < dataSize) { ci.dataBuffer = new byte[dataSize]; } msg.readBytes(ci.dataBuffer, 0, dataSize); data = ci.dataBuffer; } buf.writeVarInt(dataSize); ci.deflater.setInput(data, offset, dataSize); ci.deflater.finish(); while (!ci.deflater.finished()) { int count = ci.deflater.deflate(ci.compBuffer); out.writeBytes(ci.compBuffer, 0, count); } ci.deflater.reset(); } }