List of usage examples for io.netty.buffer ByteBuf getUnsignedByte
public abstract short getUnsignedByte(int index);
From source file:net.sourceforge.entrainer.socket.PortUnificationHandler.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // Will use the first five bytes to detect a protocol. if (in.readableBytes() < 5) { return;/* w w w . j av a 2 s. co m*/ } final int magic1 = in.getUnsignedByte(in.readerIndex()); final int magic2 = in.getUnsignedByte(in.readerIndex() + 1); if (isHttp(magic1, magic2)) { switchToWebSockets(ctx); } else { switchToJava(ctx); } }
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 ww w. ja v a 2 s . c om*/ * * 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:org.dcache.xrootd.protocol.messages.DirListRequest.java
License:Open Source License
public DirListRequest(ByteBuf buffer) { super(buffer, kXR_dirlist); options = buffer.getUnsignedByte(19); }
From source file:org.dcache.xrootd.protocol.messages.GenericReadRequestMessage.java
License:Open Source License
public GenericReadRequestMessage(ByteBuf buffer, int requestId) { super(buffer, requestId); int alen = buffer.getInt(20); if (alen <= 8) { pathid = -1;//from w w w . ja v a2 s. c om readList = new EmbeddedReadRequest[0]; } else { int prefix = 0; if (alen % 16 == 0) { pathid = -1; } else if (alen % 16 != 8) { pathid = -1; LOGGER.warn("invalid readv request: data doesn't start with 8 byte prefix (pathid)"); } else { pathid = buffer.getUnsignedByte(24); prefix = 8; } int numberOfListEntries = (alen - prefix) / 16; readList = new EmbeddedReadRequest[numberOfListEntries]; for (int i = 0; i < numberOfListEntries; i++) { int j = 24 + prefix + i * 16; readList[i] = new EmbeddedReadRequest(buffer.getInt(j), buffer.getInt(j + 4), buffer.getLong(j + 8)); } } }
From source file:org.dcache.xrootd.protocol.messages.LoginRequest.java
License:Open Source License
public LoginRequest(ByteBuf buffer) { super(buffer, kXR_login); int pos = buffer.indexOf(8, 16, (byte) 0); // User name is padded with '\0' if (pos > -1) { username = buffer.toString(8, pos - 8, US_ASCII); } else {/*from w ww . ja v a2s .c om*/ username = buffer.toString(8, 8, US_ASCII); } pid = buffer.getInt(4); capver = buffer.getUnsignedByte(18); role = buffer.getUnsignedByte(19); int tlen = buffer.getInt(20); token = buffer.toString(24, tlen, US_ASCII); }
From source file:org.dcache.xrootd.protocol.messages.StatRequest.java
License:Open Source License
public StatRequest(ByteBuf buffer) { super(buffer, kXR_stat); options = buffer.getUnsignedByte(4); fhandle = buffer.getInt(16); }
From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUDecoder.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception { if (logger.isTraceEnabled()) { logger.trace("decode - bytes: {}", ByteBufUtil.hexDump(in)); }//from w ww. ja v a 2 s. c o m if (in.readableBytes() < Constants.APCI_MIN_LENGTH) { return; } final byte start = in.getByte(in.readerIndex() + 0); if (start != Constants.START_BYTE) { throw new DecoderException( String.format("ACPI must start with 0x%02x but did with 0x%02x", Constants.START_BYTE, start)); } final short len = in.getUnsignedByte(in.readerIndex() + 1); if (len > Constants.APCI_MAX_DATA_LENGTH) { throw new DecoderException(String.format("APCI has a maximum length of %s bytes, but we received %s", Constants.APCI_MAX_DATA_LENGTH, len)); } if (in.readableBytes() < len + 2) { return; } // we have the full InformationTransfer // skip start & len in.skipBytes(2); // read control fields final ByteBuf controlFields = in.readSlice(4); final ByteBuf data; if (len > 4) { data = Unpooled.copiedBuffer(in.readSlice(len - 4)).order(ByteOrder.LITTLE_ENDIAN); } else { data = null; } // now add the PDU out.add(decode(controlFields, data)); }
From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.ASDUAddress.java
License:Open Source License
public int[] toArray() { final ByteBuf buf = Unpooled.buffer(2); buf.writeShort(this.address); return new int[] { buf.getUnsignedByte(0), buf.getUnsignedByte(1) }; }
From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.InformationObjectAddress.java
License:Open Source License
public int[] toArray() { final ByteBuf buf = Unpooled.buffer(4); buf.writeMedium(this.address); return new int[] { buf.getUnsignedByte(0), buf.getUnsignedByte(1), buf.getUnsignedByte(2) }; }
From source file:org.eclipse.scada.protocol.iec60870.asdu.types.ASDUAddress.java
License:Open Source License
@Override public String toString() { final ByteBuf buf = Unpooled.buffer(2); buf.writeShort(this.address); return isBroadcast() ? "[BCAST]" : String.format("[%d-%d # %d]", buf.getUnsignedByte(0), buf.getUnsignedByte(1), this.address); }