List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:de.gandev.modjn.entity.func.response.ReadCoilsResponse.java
License:Apache License
@Override public void decode(ByteBuf data) { byteCount = data.readUnsignedByte(); byte[] coils = new byte[byteCount]; data.readBytes(coils);/*from ww w. ja va 2 s . c o m*/ coilStatus = BitSet.valueOf(coils); }
From source file:de.gandev.modjn.entity.func.response.ReadDiscreteInputsResponse.java
License:Apache License
@Override public void decode(ByteBuf data) { byteCount = data.readUnsignedByte(); byte[] inputs = new byte[byteCount]; data.readBytes(inputs);/* w ww .j a v a 2s . co m*/ inputStatus = BitSet.valueOf(inputs); }
From source file:de.gandev.modjn.entity.func.response.ReadHoldingRegistersResponse.java
License:Apache License
@Override public void decode(ByteBuf data) { byteCount = data.readUnsignedByte(); registers = new int[byteCount / 2]; for (int i = 0; i < registers.length; i++) { registers[i] = data.readUnsignedShort(); }/* w w w . j av a 2 s . c om*/ }
From source file:de.gandev.modjn.entity.func.response.ReadInputRegistersResponse.java
License:Apache License
@Override public void decode(ByteBuf data) { byteCount = data.readUnsignedByte(); inputRegisters = new int[byteCount / 2]; for (int i = 0; i < inputRegisters.length; i++) { inputRegisters[i] = data.readUnsignedShort(); }/*from www . j a v a 2s .c o m*/ }
From source file:divconq.ctp.cmd.ProgressCommand.java
License:Open Source License
@Override public boolean decode(ByteBuf in) { if (in.readableBytes() < 1) return false; this.amount = in.readUnsignedByte(); return true;//from w w w. j a v a2 s. co m }
From source file:divconq.ctp.CtpAdapter.java
License:Open Source License
public boolean decode(ByteBuf buf) throws Exception { OperationContext.set(this.context); // get the command to continue to decode itself if (this.current != null) return this.current.decode(buf); if (buf.readableBytes() < 1) return false; int cmdtype = buf.readUnsignedByte(); this.current = this.mapper.map(cmdtype); // TODO handle error/close? if (this.current == null) return false; // get the command to decode itself return this.current.decode(buf); }
From source file:divconq.ctp.f.BlockCommand.java
License:Open Source License
@Override public boolean decode(ByteBuf in) { while (this.state != State.DONE) { switch (this.state) { case BLOCK_TYPE: { if (in.readableBytes() < 1) return false; this.blocktype = in.readUnsignedByte(); this.eof = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_EOF) != 0); this.skipHeaders = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_HEADER) == 0); this.skipPayload = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_CONTENT) == 0); // completely done, exit the loop and decode if (this.skipHeaders && this.skipPayload) { this.state = State.DONE; break; }//from w w w . j a v a2s.c o m // to skip headers, go back to loop if (this.skipHeaders) { this.state = State.STREAM_OFFSET; break; } this.state = State.HEADER_ATTR; // deliberate fall through } case HEADER_ATTR: { if (in.readableBytes() < 2) return false; this.currattr = in.readShort(); // done with headers, go back to loop to skip down to payload if (this.currattr == CtpConstants.CTP_F_ATTR_END) { if (this.skipPayload) this.state = State.DONE; else this.state = State.STREAM_OFFSET; break; } this.state = State.HEADER_SIZE; // deliberate fall through } case HEADER_SIZE: { if (in.readableBytes() < 2) return false; this.currasize = in.readShort(); // an empty attribute is like a flag - present but no data // go on to next header if (this.currasize == 0) { this.headers.put(this.currattr, new byte[0]); this.currattr = 0; this.state = State.HEADER_ATTR; break; } this.state = State.HEADER_VALUE; // deliberate fall through } case HEADER_VALUE: { if (in.readableBytes() < this.currasize) return false; byte[] val = new byte[this.currasize]; in.readBytes(val); this.headers.put(this.currattr, val); this.currattr = 0; this.currasize = 0; this.state = State.HEADER_ATTR; break; } case STREAM_OFFSET: { if (in.readableBytes() < 8) return false; this.streamOffset = in.readLong(); this.state = State.PAYLOAD_SIZE; // deliberate fall through } case PAYLOAD_SIZE: { if (in.readableBytes() < 3) return false; this.paysize = in.readMedium(); this.state = State.PAYLOAD; // deliberate fall through } case PAYLOAD: { // return here, without any state reset, means we need more before we can decide what to do if (in.readableBytes() < this.paysize) return false; // add Data only if there are some bytes, otherwise skip buffer allocation if (this.paysize > 0) { ByteBuf bb = in.readSlice(this.paysize); bb.retain(); this.data = bb; } this.state = State.DONE; // deliberate fall through } case DONE: { break; } } } return true; }
From source file:divconq.ctp.stream.UngzipStream.java
License:Open Source License
protected void inflate(ByteBuf in) { switch (this.gzipState) { case HEADER_START: if (in.readableBytes() < 10) return; // read magic numbers int magic0 = in.readByte(); int magic1 = in.readByte(); if (magic0 != 31) { OperationContext.get().getTaskRun().kill("Input is not in the GZIP format"); return; }//w ww . ja v a2 s.c o m this.crc.update(magic0); this.crc.update(magic1); int method = in.readUnsignedByte(); if (method != Deflater.DEFLATED) { OperationContext.get().getTaskRun() .kill("Unsupported compression method " + method + " in the GZIP header"); return; } this.crc.update(method); this.flags = in.readUnsignedByte(); this.crc.update(this.flags); if ((this.flags & FRESERVED) != 0) { OperationContext.get().getTaskRun().kill("Reserved flags are set in the GZIP header"); return; } // mtime (int) this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readUnsignedByte()); // extra flags this.crc.update(in.readUnsignedByte()); // operating system this.gzipState = GzipState.FLG_READ; case FLG_READ: if ((this.flags & FEXTRA) != 0) { if (in.readableBytes() < 2) return; int xlen1 = in.readUnsignedByte(); int xlen2 = in.readUnsignedByte(); this.crc.update(xlen1); this.crc.update(xlen2); this.xlen |= xlen1 << 8 | xlen2; } this.gzipState = GzipState.XLEN_READ; case XLEN_READ: if (this.xlen != -1) { if (in.readableBytes() < xlen) return; byte[] xtra = new byte[xlen]; in.readBytes(xtra); this.crc.update(xtra); } this.gzipState = GzipState.SKIP_FNAME; case SKIP_FNAME: if ((this.flags & FNAME) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.SKIP_COMMENT; case SKIP_COMMENT: if ((this.flags & FCOMMENT) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.PROCESS_FHCRC; case PROCESS_FHCRC: if ((this.flags & FHCRC) != 0) { if (in.readableBytes() < 4) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } } this.crc.reset(); this.gzipState = GzipState.PRROCESS_CONTENT; case PRROCESS_CONTENT: int readableBytes = in.readableBytes(); if (readableBytes < 1) return; if (in.hasArray()) { this.inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes); } else { byte[] array = new byte[readableBytes]; in.getBytes(in.readerIndex(), array); this.inflater.setInput(array); } int maxOutputLength = this.inflater.getRemaining() << 1; ByteBuf decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); boolean readFooter = false; byte[] outArray = decompressed.array(); try { while (!this.inflater.needsInput()) { int writerIndex = decompressed.writerIndex(); int outIndex = decompressed.arrayOffset() + writerIndex; int length = decompressed.writableBytes(); if (length == 0) { // completely filled the buffer allocate a new one and start to fill it this.outlist.add(decompressed); decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); outArray = decompressed.array(); continue; } int outputLength = this.inflater.inflate(outArray, outIndex, length); if (outputLength > 0) { decompressed.writerIndex(writerIndex + outputLength); this.crc.update(outArray, outIndex, outputLength); } else { if (this.inflater.needsDictionary()) { if (this.dictionary == null) { OperationContext.get().getTaskRun().kill( "decompression failure, unable to set dictionary as non was specified"); return; } this.inflater.setDictionary(this.dictionary); } } if (this.inflater.finished()) { readFooter = true; break; } } in.skipBytes(readableBytes - this.inflater.getRemaining()); } catch (DataFormatException x) { OperationContext.get().getTaskRun().kill("decompression failure: " + x); return; } finally { if (decompressed.isReadable()) { this.outlist.add(decompressed); } else { decompressed.release(); } } if (!readFooter) break; this.gzipState = GzipState.PROCESS_FOOTER; case PROCESS_FOOTER: if (in.readableBytes() < 8) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = this.crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } // read ISIZE and verify int dataLength = 0; for (int i = 0; i < 4; ++i) dataLength |= in.readUnsignedByte() << i * 8; int readLength = this.inflater.getTotalOut(); if (dataLength != readLength) { OperationContext.get().getTaskRun() .kill("Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength); return; } this.gzipState = GzipState.DONE; case DONE: break; } }
From source file:gps.GpsPacket.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { int size = buf.readUnsignedByte(); dataList = new ArrayList<>(size); for (int i = 0; i < size; i++) { dataList.add(readPlayerData(buf)); }//from w w w . j a v a 2 s. c om }
From source file:gps.GpsPacket.java
License:Open Source License
private PlayerData readPlayerData(ByteBuf buf) { char[] chars = new char[buf.readUnsignedByte()]; for (int i = 0; i < chars.length; i++) { chars[i] = buf.readChar();//from ww w .ja va 2 s .c om } String username = new String(chars); BlockPos pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt()); int dimension = buf.readInt(); return new PlayerData(username, dimension, pos); }