List of usage examples for io.netty.buffer ByteBuf getUnsignedShort
public abstract int getUnsignedShort(int index);
From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveRequestDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 6) { return;// w ww . ja v a 2s. c o m } int aduLength = in.getUnsignedShort(in.readerIndex() + 4); int wholeLength = 6 + aduLength + (withCheckSum ? 2 : 0); if (in.readableBytes() < wholeLength) { return; } if (withCheckSum) { checkSum(in, wholeLength); } int transectionId = in.readUnsignedShort(); in.readerIndex(in.readerIndex() + 4); int unitId = in.readUnsignedByte(); int functionCode = in.readUnsignedByte(); ModbusFunction function; switch (functionCode) { case 0x01: function = new ReadBooleanRegistersFunction(ModbusRegisterType.COIL); break; case 0x02: function = new ReadBooleanRegistersFunction(ModbusRegisterType.DISCRETE_INPUT); break; case 0x03: function = new ReadWordRegistersFunction(ModbusRegisterType.HOLDING); break; case 0x04: function = new ReadWordRegistersFunction(ModbusRegisterType.INPUT); break; case 0x05: function = new WriteCoilFunction(); break; case 0x06: function = new WriteHoldingFunction(); break; default: throw new UnsupportedFunctionCodeException(); } try { function.decodeRequestData(in); } catch (Throwable ex) { if (ex instanceof DecoderException) { throw ex; } else { throw new DecoderException(ex); } } ModbusRequest request = new ModbusRequest(transectionId, unitId, function); out.add(request); if (withCheckSum) { in.readerIndex(in.readerIndex() + 2); } }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveRequestDecoder.java
License:Open Source License
private void checkSum(ByteBuf in, int wholeLength) { int crcGet = in.getUnsignedShort(in.readerIndex() + wholeLength - 2); int crcCalc = Utils.crc(in, in.readerIndex(), wholeLength - 2); if (crcCalc != crcGet) { throw new DecoderException("wrong crc"); }/*from w w w . ja va2 s. co m*/ }
From source file:net.tomp2p.connection.DefaultSignatureFactory.java
License:Apache License
@Override public PublicKey decodePublicKey(ByteBuf buf) { if (buf.readableBytes() < 2) { return null; }/*from w ww . ja v a 2 s .c om*/ int len = buf.getUnsignedShort(buf.readerIndex()); if (buf.readableBytes() - 2 < len) { return null; } buf.skipBytes(2); if (len <= 0) { return PeerMaker.EMPTY_PUBLICKEY; } byte me[] = new byte[len]; buf.readBytes(me); return decodePublicKey(me); }
From source file:net.tomp2p.connection.DSASignatureFactory.java
License:Apache License
@Override public PublicKey decodePublicKey(ByteBuf buf) { if (buf.readableBytes() < 2) { return null; }// w w w . jav a2s . co m int len = buf.getUnsignedShort(buf.readerIndex()); if (buf.readableBytes() - 2 < len) { return null; } buf.skipBytes(2); if (len <= 0) { return PeerBuilder.EMPTY_PUBLIC_KEY; } byte me[] = new byte[len]; buf.readBytes(me); return decodePublicKey(me); }
From source file:net.tomp2p.storage.Data.java
License:Apache License
/** * Reads the header. Does not modify the buffer positions if header could * not be fully read.//from w w w . j av a 2s . com * * Header format: * <pre> * 1 byte - header * 1 or 4 bytes - length * 4 or 0 bytes - ttl (hasTTL) * 1 or 0 bytes - number of basedon keys (hasBasedOn) * n x 20 bytes - basedon keys (hasBasedOn, number of basedon keys) * 2 or 0 bytes - length of public key (hasPublicKey) * n bytes - public key (hasPublicKey, length of public key) * </pre> * * * @param buf * The buffer to read from * @return The data object, may be partially filled */ public static Data decodeHeader(final ByteBuf buf, final SignatureFactory signatureFactory) { // 2 is the smallest packet size, we could start if we know 1 byte to // decode the header, but we always need // a second byte. Thus, we are waiting for at least 2 bytes. if (buf.readableBytes() < Utils.BYTE_BYTE_SIZE + Utils.BYTE_BYTE_SIZE) { return null; } final int header = buf.getUnsignedByte(buf.readerIndex()); final Data.Type type = Data.type(header); //Data length final int length; final int indexLength = Utils.BYTE_BYTE_SIZE; final int indexTTL; switch (type) { case SMALL: length = buf.getUnsignedByte(buf.readerIndex() + indexLength); indexTTL = indexLength + Utils.BYTE_BYTE_SIZE; break; case LARGE: indexTTL = indexLength + Utils.INTEGER_BYTE_SIZE; if (buf.readableBytes() < indexTTL) { return null; } length = buf.getInt(buf.readerIndex() + indexLength); break; default: throw new IllegalArgumentException("unknown type"); } //TTL final int ttl; final int indexBasedOnNr; if (hasTTL(header)) { indexBasedOnNr = indexTTL + Utils.INTEGER_BYTE_SIZE; if (buf.readableBytes() < indexBasedOnNr) { return null; } ttl = buf.getInt(buf.readerIndex() + indexTTL); } else { ttl = -1; indexBasedOnNr = indexTTL; } //Nr BasedOn + basedon final int numBasedOn; final int indexPublicKeySize; final int indexBasedOn; final Set<Number160> basedOn = new HashSet<Number160>(); if (hasBasedOn(header)) { // get # of based on keys indexBasedOn = indexBasedOnNr + Utils.BYTE_BYTE_SIZE; if (buf.readableBytes() < indexBasedOn) { return null; } numBasedOn = buf.getUnsignedByte(buf.readerIndex() + indexBasedOnNr) + 1; indexPublicKeySize = indexBasedOn + (numBasedOn * Number160.BYTE_ARRAY_SIZE); if (buf.readableBytes() < indexPublicKeySize) { return null; } //get basedon int index = buf.readerIndex() + indexBasedOnNr + Utils.BYTE_BYTE_SIZE; final byte[] me = new byte[Number160.BYTE_ARRAY_SIZE]; for (int i = 0; i < numBasedOn; i++) { buf.getBytes(index, me); index += Number160.BYTE_ARRAY_SIZE; basedOn.add(new Number160(me)); } } else { // no based on keys indexPublicKeySize = indexBasedOnNr; numBasedOn = 0; } //public key and size final int publicKeySize; final int indexPublicKey; final int indexEnd; final PublicKey publicKey; if (hasPublicKey(header)) { indexPublicKey = indexPublicKeySize + Utils.SHORT_BYTE_SIZE; if (buf.readableBytes() < indexPublicKey) { return null; } publicKeySize = buf.getUnsignedShort(buf.readerIndex() + indexPublicKeySize); indexEnd = indexPublicKey + publicKeySize; if (buf.readableBytes() < indexEnd) { return null; } //get public key buf.skipBytes(indexPublicKeySize); publicKey = signatureFactory.decodePublicKey(buf); } else { publicKeySize = 0; indexPublicKey = indexPublicKeySize; buf.skipBytes(indexPublicKey); publicKey = null; } //now we have read the header and the length final Data data = new Data(header, length); data.ttlSeconds = ttl; data.basedOnSet = basedOn; data.publicKey = publicKey; return data; }
From source file:nettyClient4.clientDecoder.java
License:Apache License
/** * byte short// w w w . j a v a2 s. c o m * return the length of pack * @param in * @param actualLengthFieldOffset * @return */ @Deprecated private long getFrameLength(ByteBuf in, int actualLengthFieldOffset) { in = in.order(byteOrder); return in.getUnsignedShort(actualLengthFieldOffset);//return the length }
From source file:nl.thijsalders.spigotproxy.haproxy.HAProxyMessageDecoder.java
License:Apache License
/** * Returns the index in the buffer of the end of header if found. * Returns -1 if no end of header was found in the buffer. *//*from ww w . ja v a 2 s .c o m*/ private static int findEndOfHeader(final ByteBuf buffer) { final int n = buffer.readableBytes(); // per spec, the 15th and 16th bytes contain the address length in bytes if (n < 16) { return -1; } int offset = buffer.readerIndex() + 14; // the total header length will be a fixed 16 byte sequence + the dynamic address information block int totalHeaderBytes = 16 + buffer.getUnsignedShort(offset); // ensure we actually have the full header available if (n >= totalHeaderBytes) { return totalHeaderBytes; } else { return -1; } }
From source file:org.dcache.xrootd.core.AbstractXrootdDecoder.java
License:Open Source License
protected XrootdRequest getRequest(ByteBuf frame) { int requestId = frame.getUnsignedShort(2); switch (requestId) { case kXR_sigver: return new SigverRequest(frame); case kXR_login: return new LoginRequest(frame); case kXR_prepare: return new PrepareRequest(frame); case kXR_open: return new OpenRequest(frame); case kXR_stat: return new StatRequest(frame); case kXR_statx: return new StatxRequest(frame); case kXR_read: return new ReadRequest(frame); case kXR_readv: return new ReadVRequest(frame); case kXR_write: return new WriteRequest(frame); case kXR_sync: return new SyncRequest(frame); case kXR_close: return new CloseRequest(frame); case kXR_protocol: return new ProtocolRequest(frame); case kXR_rm://w ww .j av a 2s. com return new RmRequest(frame); case kXR_rmdir: return new RmDirRequest(frame); case kXR_mkdir: return new MkDirRequest(frame); case kXR_mv: return new MvRequest(frame); case kXR_dirlist: return new DirListRequest(frame); case kXR_auth: return new AuthenticationRequest(frame); case kXR_endsess: return new EndSessionRequest(frame); case kXR_locate: return new LocateRequest(frame); case kXR_query: return new QueryRequest(frame); case kXR_set: return new SetRequest(frame); default: return new UnknownRequest(frame); } }
From source file:org.dcache.xrootd.protocol.messages.AbstractXrootdRequest.java
License:Open Source License
public AbstractXrootdRequest(ByteBuf buffer) { streamId = buffer.getUnsignedShort(0); requestId = buffer.getUnsignedShort(2); }
From source file:org.dcache.xrootd.protocol.messages.MkDirRequest.java
License:Open Source License
public MkDirRequest(ByteBuf buffer) { super(buffer, kXR_mkdir); options = buffer.getByte(4); mode = buffer.getUnsignedShort(18); }