List of usage examples for io.netty.buffer ByteBuf capacity
public abstract int capacity();
From source file:ru.calypso.ogar.server.net.PacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception { ByteBuf buf = frame.content().order(ByteOrder.LITTLE_ENDIAN); if (buf.capacity() < 1) { // Discard empty messages return;//from w w w .j a v a 2 s . c om } buf.resetReaderIndex(); int packetId = buf.readUnsignedByte(); Packet packet = PacketRegistry.CLIENT2SERVER.constructPacket(packetId); if (packet == null) { _log.info("Unknown packet ID: " + packetId + ", user disconected!"); ctx.disconnect(); return; } Log.logDebug("Received packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") from " + ctx.channel().remoteAddress()); packet.readData(buf); out.add(packet); }
From source file:sas.systems.imflux.test.packet.rtcp.SdesChunkItemsTest.java
License:Apache License
@Test public void testEncodeNull() throws Exception { ByteBuf buffer = SdesChunkItems.encode(SdesChunkItems.NULL_ITEM); assertEquals(1, buffer.capacity()); assertEquals(0x00, buffer.array()[0]); }
From source file:uk.co.thinkofdeath.thinkcraft.bukkit.world.ChunkManager.java
License:Apache License
private void gzipChunk(ChunkSnapshot chunk, ByteBuf out) { int mask = 0; int count = 0; for (int i = 0; i < 16; i++) { if (!chunk.isSectionEmpty(i)) { mask |= 1 << i;//from w w w .ja v a2 s . c o m count++; } } ByteBuf data = allocator.buffer(16 * 16 * 16 * 4 * count + 3 + 256); data.writeByte(1); // The chunk exists data.writeShort(mask); int offset = 0; int blockDataOffset = 16 * 16 * 16 * 2 * count; int skyDataOffset = blockDataOffset + 16 * 16 * 16 * count; for (int i = 0; i < 16; i++) { if (!chunk.isSectionEmpty(i)) { for (int oy = 0; oy < 16; oy++) { for (int oz = 0; oz < 16; oz++) { for (int ox = 0; ox < 16; ox++) { int y = oy + (i << 4); int id = chunk.getBlockTypeId(ox, y, oz); int dValue = chunk.getBlockData(ox, y, oz); data.setShort((offset << 1) + 3, (id << 4) | dValue); data.setByte(blockDataOffset + offset + 3, chunk.getBlockEmittedLight(ox, y, oz)); data.setByte(skyDataOffset + offset + 3, chunk.getBlockSkyLight(ox, y, oz)); offset++; } } } } } for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { data.setByte(skyDataOffset + offset + 3 + x + z * 16, ThinkBiome.bukkitToId(chunk.getBiome(x, z))); } } data.writerIndex(data.capacity()); try { GZIPOutputStream gzip = new GZIPOutputStream(new ByteBufOutputStream(out)); byte[] bytes = new byte[data.readableBytes()]; data.readBytes(bytes); gzip.write(bytes); gzip.close(); } catch (IOException e) { throw new RuntimeException(); } finally { data.release(); } }
From source file:vn.com.onesoft.bigfox.server.io.core.channel.websocket.WebSocketServerHandler.java
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return;//from w w w . j av a2s . c o m } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (frame instanceof BinaryWebSocketFrame) { BinaryWebSocketFrame bFrame = (BinaryWebSocketFrame) frame; ByteBuf buf = bFrame.content(); int length = buf.capacity(); byte[] data = new byte[length]; buf.readBytes(data); try { //HuongNS // data = BFCompressManager.getInstance().decompress(data); // data = BFEncryptManager.crypt(ctx.channel(), data); mf.onMessage(ctx.channel(), data); //Thc thi yu cu t Client } catch (Exception ex) { ctx.channel().close(); // BFLogger.getInstance().error("handleWebSocketFrame - Exception"); BFLogger.getInstance().error(ex.getMessage(), ex); } } }