List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract ByteBuf readerIndex(int readerIndex);
From source file:com.friz.lobby.network.codec.LoginDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { if (!buf.isReadable()) return;// ww w . ja va 2 s.com int type = buf.readUnsignedByte(); int size = buf.readUnsignedShort(); if (!buf.isReadable(size)) return; int major = buf.readInt(); int minor = buf.readInt(); int rsaSize = buf.readUnsignedShort(); byte[] rsa = new byte[rsaSize]; buf.readBytes(rsa); ByteBuf rsaBuf = Unpooled.wrappedBuffer( new BigInteger(rsa).modPow(Constants.LOGIN_EXPONENT, Constants.LOGIN_MODULUS).toByteArray()); int rsaMagic = rsaBuf.readUnsignedByte(); int[] key = new int[4]; for (int i = 0; i < key.length; i++) key[i] = rsaBuf.readInt(); int block = rsaBuf.readUnsignedByte(); if (block == 1 || block == 3) { int code = rsaBuf.readUnsignedMedium(); rsaBuf.readerIndex(rsaBuf.readerIndex() + 1); System.out.println(new GoogleAuthenticator().authorize("OE2ZSYF6T7N2R5CG", code)); } else if (block == 0) { int trusted = rsaBuf.readInt(); } else if (block == 2) { rsaBuf.readerIndex(rsaBuf.readerIndex() + 4); } String password = BufferUtils.getString(rsaBuf); long serverKey = rsaBuf.readLong(); long clientKey = rsaBuf.readLong(); byte[] xtea = new byte[buf.readableBytes()]; buf.readBytes(xtea); ByteBuf xteaBuf = Unpooled.wrappedBuffer(new XTEA(xtea).decrypt(key).toByteArray()); String username = ""; boolean asString = xteaBuf.readBoolean(); if (asString) username = BufferUtils.getString(xteaBuf); else username = BufferUtils.getBase37(xteaBuf); int game = xteaBuf.readUnsignedByte(); int lang = xteaBuf.readUnsignedByte(); int display = xteaBuf.readUnsignedByte(); int width = xteaBuf.readUnsignedShort(); int height = xteaBuf.readUnsignedShort(); int multisample = xteaBuf.readByte(); byte[] uid = new byte[24]; for (int i = 0; i < uid.length; i++) uid[i] = xteaBuf.readByte(); String token = BufferUtils.getString(xteaBuf); int prefSize = xteaBuf.readUnsignedByte(); int prefVersion = xteaBuf.readUnsignedByte(); int aPref = xteaBuf.readUnsignedByte(); int antiAliasing = xteaBuf.readUnsignedByte(); int aPref1 = xteaBuf.readUnsignedByte(); int bloom = xteaBuf.readUnsignedByte(); int brightness = xteaBuf.readUnsignedByte(); int buildArea = xteaBuf.readUnsignedByte(); int aPref2 = xteaBuf.readUnsignedByte(); int flickeringEffects = xteaBuf.readUnsignedByte(); int fog = xteaBuf.readUnsignedByte(); int groundBlending = xteaBuf.readUnsignedByte(); int groundDecoration = xteaBuf.readUnsignedByte(); int idleAnimations = xteaBuf.readUnsignedByte(); int lighting = xteaBuf.readUnsignedByte(); int sceneryShadows = xteaBuf.readUnsignedByte(); int aPref3 = xteaBuf.readUnsignedByte(); int nullPref = xteaBuf.readUnsignedByte(); int orthoMode = xteaBuf.readUnsignedByte(); int particles = xteaBuf.readUnsignedByte(); int removeRoofs = xteaBuf.readUnsignedByte(); int maxScreenSize = xteaBuf.readUnsignedByte(); int skyboxes = xteaBuf.readUnsignedByte(); int mobShadows = xteaBuf.readUnsignedByte(); int textures = xteaBuf.readUnsignedByte(); int desiredToolkit = xteaBuf.readUnsignedByte(); int nullPref1 = xteaBuf.readUnsignedByte(); int water = xteaBuf.readUnsignedByte(); int screenSize = xteaBuf.readUnsignedByte(); int customCursors = xteaBuf.readUnsignedByte(); int graphics = xteaBuf.readUnsignedByte(); int cpu = xteaBuf.readUnsignedByte(); int aPref4 = xteaBuf.readUnsignedByte(); int safeMode = xteaBuf.readUnsignedByte(); int aPref5 = xteaBuf.readUnsignedByte(); int aPref6 = xteaBuf.readUnsignedByte(); int aPref7 = xteaBuf.readUnsignedByte(); int soundEffectsVolume = xteaBuf.readUnsignedByte(); int areaSoundsVolume = xteaBuf.readUnsignedByte(); int voiceOverVolume = xteaBuf.readUnsignedByte(); int musicVolume = xteaBuf.readUnsignedByte(); int themeMusicVolume = xteaBuf.readUnsignedByte(); int steroSound = xteaBuf.readUnsignedByte(); int infoVersion = xteaBuf.readUnsignedByte(); int osType = xteaBuf.readUnsignedByte(); boolean arch64 = xteaBuf.readBoolean(); int versionType = xteaBuf.readUnsignedByte(); int vendorType = xteaBuf.readUnsignedByte(); int jMajor = xteaBuf.readUnsignedByte(); int jMinor = xteaBuf.readUnsignedByte(); int jPatch = xteaBuf.readUnsignedByte(); boolean falseBool = xteaBuf.readBoolean(); int heapSize = xteaBuf.readUnsignedShort(); int pocessorCount = xteaBuf.readUnsignedByte(); int cpuPhyscialMemory = xteaBuf.readUnsignedMedium(); int cpuClock = xteaBuf.readUnsignedShort(); String gpuName = BufferUtils.getJagString(xteaBuf); String aString = BufferUtils.getJagString(xteaBuf); String dxVersion = BufferUtils.getJagString(xteaBuf); String aString1 = BufferUtils.getJagString(xteaBuf); int gpuDriverMonth = xteaBuf.readUnsignedByte(); int gpuDriverYear = xteaBuf.readUnsignedShort(); String cpuType = BufferUtils.getJagString(xteaBuf); String cpuName = BufferUtils.getJagString(xteaBuf); int cpuThreads = xteaBuf.readUnsignedByte(); int anInt = xteaBuf.readUnsignedByte(); int anInt1 = xteaBuf.readInt(); int anInt2 = xteaBuf.readInt(); int anInt3 = xteaBuf.readInt(); int anInt4 = xteaBuf.readInt(); String aString2 = BufferUtils.getJagString(xteaBuf); int anInt5 = xteaBuf.readInt(); String aString3 = BufferUtils.getString(xteaBuf); int affiliate = xteaBuf.readInt(); int anInt6 = xteaBuf.readInt(); String aString4 = BufferUtils.getString(xteaBuf); int anInt7 = xteaBuf.readUnsignedByte(); int[] checksums = new int[(xteaBuf.readableBytes() / 4) + 1]; for (int i = 0; i < checksums.length; i++) { if (i == 32) checksums[i] = -1; else checksums[i] = xteaBuf.readInt(); } final List<Module> modules = new ArrayList<>(); modules.add(new ClientVersionModule(major, minor)); modules.add(new ClientTypeModule(game, lang, display, width, height)); out.add(new LoginRequestEvent(modules)); }
From source file:com.friz.update.network.codec.UpdateDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { if (!buffer.isReadable(6)) return;/*from w w w .ja v a2 s . c o m*/ int opcode = buffer.readUnsignedByte(); if (opcode == 0 || opcode == 1) { int type = buffer.readUnsignedByte(); int file = buffer.readInt(); out.add(new FileRequestEvent(opcode == 1, type, file)); } else if (opcode == 4) { int key = buffer.readUnsignedByte(); buffer.readerIndex(buffer.readerIndex() + 4); out.add(new XorRequestEvent(key)); } else { buffer.readerIndex(buffer.readerIndex() + 5); } }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java
License:Open Source License
@Test public void testReadOctetStringToBytesWhenOctetStringSizeIsPositiveReadsByteBufferCorrectly() throws Exception { byte[] bytesToTransfer = { 0x14, 0x13 }; ByteBuf octetStringBytes = Unpooled.buffer(bytesToTransfer.length + 4); octetStringBytes.writeInt(bytesToTransfer.length); octetStringBytes.writeBytes(bytesToTransfer); ByteBuf readBytes = ChannelBufferUtils.readOctetStringToBytes(octetStringBytes); octetStringBytes.readerIndex(4); assertEquals("Read bytes and given bytes are not the same", octetStringBytes, readBytes); octetStringBytes.release();//from w ww . ja v a 2s .co m readBytes.release(); }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java
License:Open Source License
@Test public void testWriteBytesToOctetStringIsWrittenCorrectly() throws Exception { byte[] payload = { 0x14, 0x16 }; ByteBuf input = Unpooled.copiedBuffer(payload); ByteBuf outputBuffer = Unpooled.buffer(4 + payload.length); ChannelBufferUtils.writeBytesToOctetString(input, outputBuffer); assertEquals("Written length prefix in not the length of the encoded byte array", payload.length, outputBuffer.readInt());//from w w w.j a v a 2 s . co m assertEquals("Written bytes are not the same with those of the input array", input.readerIndex(0), outputBuffer); outputBuffer.release(); input.release(); }
From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java
License:Open Source License
@Test public void testDecodeDatagramDecodesCorrectly() throws Exception { ByteBuf encodedMessage = Unpooled.buffer(); ChannelBufferUtils.writeStringToOctetString("", encodedMessage, Charsets.UTF_8); encodedMessage.writeInt(4444);/*ww w .jav a 2 s. com*/ ChannelBufferUtils.writeStringToOctetString("?", encodedMessage, Charsets.UTF_8); encodedMessage.writeInt(5555); byte[] payload = { 0x14, 0x79 }; ByteBuf payloadBuffer = Unpooled.copiedBuffer(payload); ChannelBufferUtils.writeBytesToOctetString(payloadBuffer, encodedMessage); Datagram datagram = transcoderHelper.decodeDatagram(encodedMessage); assertEquals("Source address is incorrect", "", datagram.getSourceAddress()); assertEquals("Source port is incorrect", 4444, datagram.getSourcePort()); assertEquals("Source address is incorrect", "?", datagram.getDestinationAddress()); assertEquals("Source port is incorrect", 5555, datagram.getDestinationPort()); assertEquals("Data payload is incorrect", payloadBuffer.readerIndex(0), datagram.getUserData()); encodedMessage.release(); payloadBuffer.release(); }
From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java
License:Open Source License
@Test public void testDecodeSmsDecodesCorrectly() throws Exception { ByteBuf encodedMessage = Unpooled.buffer(); UUID uuid = UUID.randomUUID(); ChannelBufferUtils.writeStringToOctetString("from", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("to", encodedMessage, Charsets.UTF_8); byte[] payload = { 0x14, 0x79 }; ByteBuf payloadBuffer = Unpooled.copiedBuffer(payload); ChannelBufferUtils.writeBytesToOctetString(payloadBuffer, encodedMessage); ChannelBufferUtils.writeStringToOctetString("content", encodedMessage, Charsets.UTF_8); encodedMessage.writeInt(0);/* w ww. j av a 2 s . c o m*/ ChannelBufferUtils.writeStringToOctetString("smsc", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("smscNumber", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("foreignId", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("service", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("account", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeUUIDToOctetString(uuid, encodedMessage, Charsets.UTF_8); encodedMessage.writeInt(1); encodedMessage.writeInt(2); encodedMessage.writeInt(3); encodedMessage.writeInt(2); encodedMessage.writeInt(1); encodedMessage.writeInt(6); encodedMessage.writeInt(7); encodedMessage.writeInt(8); ChannelBufferUtils.writeStringToOctetString("dlrUrl", encodedMessage, Charsets.UTF_8); encodedMessage.writeInt(9); encodedMessage.writeInt(10); encodedMessage.writeInt(1); ChannelBufferUtils.writeStringToOctetString("UTF-8", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("boxcid", encodedMessage, Charsets.UTF_8); ChannelBufferUtils.writeStringToOctetString("binfo", encodedMessage, Charsets.UTF_8); encodedMessage.writeInt(12); encodedMessage.writeInt(13); encodedMessage.writeInt(14); encodedMessage.writeInt(15); ChannelBufferUtils.writeStringToOctetString("metaData", encodedMessage, Charsets.UTF_8); Sms sms = transcoderHelper.decodeSms(encodedMessage); assertEquals("The from is incorrect", "from", sms.getSender()); assertEquals("The to is incorrect", "to", sms.getReceiver()); assertEquals("The udhdata is incorrect", payloadBuffer.readerIndex(0), sms.getUdhData()); assertEquals("The message data is incorrect", "content", sms.getMsgData()); assertEquals("The time is incorrect", 0, sms.getTime()); assertEquals("The smsc is incorrect", "smsc", sms.getSmscId()); assertEquals("The smscNumber is incorrect", "smscNumber", sms.getSmscNumber()); assertEquals("The foreignId is incorrect", "foreignId", sms.getForeignId()); assertEquals("The service is incorrect", "service", sms.getService()); assertEquals("The account is incorrect", "account", sms.getAccount()); assertEquals("The id is incorrect", uuid, sms.getId()); assertEquals("The sms type is incorrect", 1, sms.getSmsType().value()); assertEquals("The m class is incorrect", 2, sms.getMessageClass().value()); assertEquals("The mwi is incorrect", 3, sms.getMwi().value()); assertEquals("The coding is incorrect", 2, sms.getCoding().value()); assertEquals("The compress is incorrect", 1, sms.getCompress().value()); assertEquals("The validity is incorrect", 6, sms.getValidity()); assertEquals("The deferred is incorrect", 7, sms.getDeferred()); assertEquals("The dlr mask is incorrect", 8, sms.getDlrMask()); assertEquals("The dlr url is incorrect", "dlrUrl", sms.getDlrUrl()); assertEquals("The pid is incorrect", 9, sms.getPid()); assertEquals("The alt dcs is incorrect", 10, sms.getAltDcs()); assertEquals("The rpi is incorrect", 1, sms.getRpi().value()); assertEquals("The charset is incorrect", Charsets.UTF_8, sms.getCharset()); assertEquals("The box id is incorrect", "boxcid", sms.getBoxId()); assertEquals("The binfo is incorrect", "binfo", sms.getBillingInfo()); assertEquals("The msgLeft is incorrect", 12, sms.getMsgLeft()); assertEquals("The priority is incorrect", 13, sms.getPriority()); assertEquals("The resend try is incorrect", 14, sms.getResendTry()); assertEquals("The resend time is incorrect", 15, sms.getResendTime()); assertEquals("The meta data is incorrect", "metaData", sms.getMetaData()); encodedMessage.release(); payloadBuffer.release(); }
From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java
License:Open Source License
@Test public void testEncodeDatagramEncodesCorrectly() throws Exception { byte[] payload = { 0x56, 0x35 }; ByteBuf payloadBuffer = Unpooled.copiedBuffer(payload); Datagram datagram = new Datagram(); datagram.setDestinationAddress("?"); datagram.setSourceAddress(""); datagram.setDestinationPort(5555);// w w w . ja v a2s . c om datagram.setSourcePort(4444); datagram.setUserData(payloadBuffer); ByteBuf byteBuf = Unpooled.buffer(); transcoderHelper.encodeDatagram(datagram, byteBuf); assertEquals("Source address is incorrect", "", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("Source port is incorrect", 4444, byteBuf.readInt()); assertEquals("Source address is incorrect", "?", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("Source port is incorrect", 5555, byteBuf.readInt()); assertEquals("Data payload is incorrect", payloadBuffer.readerIndex(0), ChannelBufferUtils.readOctetStringToBytes(byteBuf)); byteBuf.release(); payloadBuffer.release(); }
From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java
License:Open Source License
@Test public void testEncodeSmsEncodesCorrectly() throws Exception { byte[] data = { 0x25 }; ByteBuf udh = Unpooled.copiedBuffer(data); Sms sms = new Sms(); sms.setSender("from"); sms.setReceiver("to"); sms.setUdhData(udh);/*from www . ja v a 2 s . c om*/ sms.setMsgData("content"); sms.setTime(0); sms.setSmscId("smsc"); sms.setSmscNumber("smscNumber"); sms.setForeignId("foreignId"); sms.setService("service"); sms.setAccount("account"); sms.setId(UUID.randomUUID()); sms.setSmsType(SmsType.MOBILE_TERMINATED_REPLY); sms.setMessageClass(MessageClass.MC_CLASS2); sms.setMwi(MessageWaitingIndicator.fromValue(3)); sms.setCoding(DataCoding.fromValue(2)); sms.setCompress(Compress.fromValue(1)); sms.setValidity(6); sms.setDeferred(7); sms.setDlrMask(8); sms.setDlrUrl("dlrUrl"); sms.setPid(9); sms.setAltDcs(10); sms.setRpi(ReturnPathIndicator.fromValue(1)); sms.setCharset(Charsets.UTF_8); sms.setBoxId("box"); sms.setBillingInfo("binfo"); sms.setMsgLeft(12); sms.setPriority(13); sms.setResendTry(14); sms.setResendTime(15); sms.setMetaData("metadata"); ByteBuf byteBuf = Unpooled.buffer(); transcoderHelper.encodeSms(sms, byteBuf); assertEquals("The from is incorrect", "from", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The to is incorrect", "to", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The udhdata is incorrect", udh.readerIndex(0), ChannelBufferUtils.readOctetStringToBytes(byteBuf)); assertEquals("The message data is incorrect", "content", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The time is incorrect", 0, byteBuf.readInt()); assertEquals("The smsc is incorrect", "smsc", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The smscNumber is incorrect", "smscNumber", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The foreignId is incorrect", "foreignId", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The service is incorrect", "service", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The account is incorrect", "account", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The id is incorrect", sms.getId(), ChannelBufferUtils.readUUID(byteBuf, Charsets.UTF_8)); assertEquals("The sms type is incorrect", sms.getSmsType(), SmsType.fromValue(byteBuf.readInt())); assertEquals("The m class is incorrect", 2, byteBuf.readInt()); assertEquals("The mwi is incorrect", 3, byteBuf.readInt()); assertEquals("The coding is incorrect", 2, byteBuf.readInt()); assertEquals("The compress is incorrect", 1, byteBuf.readInt()); assertEquals("The validity is incorrect", 6, byteBuf.readInt()); assertEquals("The deferred is incorrect", 7, byteBuf.readInt()); assertEquals("The dlr mask is incorrect", 8, byteBuf.readInt()); assertEquals("The dlr url is incorrect", "dlrUrl", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The pid is incorrect", 9, byteBuf.readInt()); assertEquals("The alt dcs is incorrect", 10, byteBuf.readInt()); assertEquals("The rpi is incorrect", 1, byteBuf.readInt()); assertEquals("The charset is incorrect", "UTF-8", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The box id is incorrect", "box", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The binfo is incorrect", "binfo", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); assertEquals("The msgLeft is incorrect", 12, byteBuf.readInt()); assertEquals("The priority is incorrect", 13, byteBuf.readInt()); assertEquals("The resend try is incorrect", 14, byteBuf.readInt()); assertEquals("The resend time is incorrect", 15, byteBuf.readInt()); assertEquals("The meta data is incorrect", "metadata", ChannelBufferUtils.readOctetStringToString(byteBuf, Charsets.UTF_8)); udh.release(); }
From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java
License:Open Source License
private void decodeSimulatorMessage(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) { long messageId = SimulatorMessageCodec.getMessageId(buffer); AddressLevel dstAddressLevel = AddressLevel .fromInt(SimulatorMessageCodec.getDestinationAddressLevel(buffer)); if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("[%d] %s %s received a message for addressLevel %s", messageId, addressLevel, localAddress, dstAddressLevel)); }//w w w . j a va2 s. co m if (dstAddressLevel == addressLevel) { SimulatorMessage message = SimulatorMessageCodec.decodeSimulatorMessage(buffer); if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("[%d] %s %s will consume %s", messageId, addressLevel, localAddress, message)); } out.add(message); } else { int addressIndex = SimulatorMessageCodec.getChildAddressIndex(buffer, addressLevelValue); ctx.attr(forwardAddressIndex).set(addressIndex); out.add(buffer.duplicate()); buffer.readerIndex(buffer.readableBytes()); buffer.retain(); } }
From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java
License:Open Source License
private void decodeResponse(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) { long messageId = ResponseCodec.getMessageId(buffer); AddressLevel dstAddressLevel = AddressLevel.fromInt(ResponseCodec.getDestinationAddressLevel(buffer)); if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("[%d] %s %s received a response for addressLevel %s", messageId, addressLevel, localAddress, dstAddressLevel)); }//from w ww .j a v a 2 s .co m if (dstAddressLevel == addressLevel || dstAddressLevel.isParentAddressLevel(addressLevel)) { Response response = ResponseCodec.decodeResponse(buffer); if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("[%d] %s %s received %s", response.getMessageId(), addressLevel, localAddress, response)); } if (workerJvmManager != null) { workerJvmManager.updateLastSeenTimestamp(response); } out.add(response); } else { int addressIndex = ResponseCodec.getChildAddressIndex(buffer, addressLevelValue); ctx.attr(forwardAddressIndex).set(addressIndex); out.add(buffer.duplicate()); buffer.readerIndex(buffer.readableBytes()); buffer.retain(); } }