Example usage for io.netty.buffer ByteBuf readByte

List of usage examples for io.netty.buffer ByteBuf readByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readByte.

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:de.sanandrew.mods.turretmod.network.PacketPlayerTurretAction.java

License:Creative Commons License

@Override
public void fromBytes(ByteBuf buf) {
    this.turretId = buf.readInt();
    this.actionId = buf.readByte();
}

From source file:de.sanandrew.mods.turretmod.network.PacketUpdateUgradeSlot.java

License:Creative Commons License

@Override
public void fromBytes(ByteBuf buf) {
    this.turretID = buf.readInt();
    this.slot = buf.readByte();
    this.stack = ByteBufUtils.readItemStack(buf);
}

From source file:discord4j.voice.VoiceSocket.java

License:Open Source License

private static String getNullTerminatedString(ByteBuf buffer, int offset) {
    buffer.skipBytes(offset);//from   w ww.j a  va  2 s. c  o  m
    ByteArrayOutputStream os = new ByteArrayOutputStream(15);
    byte c;
    while ((c = buffer.readByte()) != 0) {
        os.write(c);
    }

    return new String(os.toByteArray(), StandardCharsets.US_ASCII);
}

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;
        }/*from www. j  ava2  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:divconq.lang.chars.Utf8Decoder.java

License:Open Source License

public StringBuilder32 processBytesUntilSpecial(ByteBuf buffer) {
    if (buffer == null)
        return null;

    StringBuilder32 sb = null;// www. j a v a2 s  .co  m

    // continue to build up strings from last call
    if (this.result.size() > 0)
        sb = this.result.get(this.result.size() - 1);
    else {
        sb = new StringBuilder32();
        this.result.add(sb);
    }

    this.lastSpecial = -1;

    try {
        while (buffer.readableBytes() > 0) {
            if (this.readByteNeedMore(buffer.readByte(), true))
                continue;

            if (this.lastSpecial != -1) {
                this.result = new ArrayList<StringBuilder32>();
                return sb;
            }

            sb.append(this.getCharacterAndReset());
        }
    } catch (Exception x) {
        // TODO
    }

    return null;
}

From source file:dorkbox.network.pipeline.ByteBufInput.java

License:Apache License

/**
 * Reads a 1-5 byte int. It is guaranteed that a variable length encoding will be used.
 *///from w  w w  .j av  a 2 s  .co m
@Override
public int readVarInt(boolean optimizePositive) throws KryoException {
    ByteBuf buffer = byteBuf;

    int b = buffer.readByte();
    int result = b & 0x7F;
    if ((b & 0x80) != 0) {
        b = buffer.readByte();
        result |= (b & 0x7F) << 7;
        if ((b & 0x80) != 0) {
            b = buffer.readByte();
            result |= (b & 0x7F) << 14;
            if ((b & 0x80) != 0) {
                b = buffer.readByte();
                result |= (b & 0x7F) << 21;
                if ((b & 0x80) != 0) {
                    b = buffer.readByte();
                    result |= (b & 0x7F) << 28;
                }
            }
        }
    }
    return optimizePositive ? result : result >>> 1 ^ -(result & 1);
}

From source file:dorkbox.network.pipeline.ByteBufInput.java

License:Apache License

/**
 * Returns true if enough bytes are available to read an int with {@link #readInt(boolean)}.
 *//*from ww  w  . j a  v a  2s.co m*/
@Override
public boolean canReadInt() throws KryoException {
    ByteBuf buffer = byteBuf;
    int limit = buffer.writerIndex();

    if (limit - buffer.readerIndex() >= 5) {
        return true;
    }

    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    return true;
}

From source file:dorkbox.network.pipeline.ByteBufInput.java

License:Apache License

/**
 * Returns true if enough bytes are available to read a long with {@link #readLong(boolean)}.
 *///from w ww .j  a v  a  2  s.co m
@Override
public boolean canReadLong() throws KryoException {
    ByteBuf buffer = byteBuf;
    int limit = buffer.writerIndex();

    if (limit - buffer.readerIndex() >= 9) {
        return true;
    }

    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    if ((buffer.readByte() & 0x80) == 0) {
        return true;
    }
    if (buffer.readerIndex() == limit) {
        return false;
    }
    return true;
}

From source file:dorkbox.network.pipeline.ByteBufInput.java

License:Apache License

/**
 * Reads the length and string of UTF8 characters, or null. This can read strings written by {@link Output#writeString(String)}
 * , {@link Output#writeString(CharSequence)}, and {@link Output#writeAscii(String)}.
 *
 * @return May be null.// w  w w .  j ava  2s. com
 */
@Override
public String readString() {
    ByteBuf buffer = byteBuf;

    int b = buffer.readByte();
    if ((b & 0x80) == 0) {
        return readAscii(); // ASCII.
    }
    // Null, empty, or UTF8.
    int charCount = readUtf8Length(b);
    switch (charCount) {
    case 0:
        return null;
    case 1:
        return "";
    }
    charCount--;
    if (inputChars.length < charCount) {
        inputChars = new char[charCount];
    }
    readUtf8(charCount);
    return new String(inputChars, 0, charCount);
}

From source file:dorkbox.network.pipeline.ByteBufInput.java

License:Apache License

private int readUtf8Length(int b) {
    int result = b & 0x3F; // Mask all but first 6 bits.
    if ((b & 0x40) != 0) { // Bit 7 means another byte, bit 8 means UTF8.
        ByteBuf buffer = byteBuf;
        b = buffer.readByte();
        result |= (b & 0x7F) << 6;
        if ((b & 0x80) != 0) {
            b = buffer.readByte();//  w  ww . ja  v  a 2 s . com
            result |= (b & 0x7F) << 13;
            if ((b & 0x80) != 0) {
                b = buffer.readByte();
                result |= (b & 0x7F) << 20;
                if ((b & 0x80) != 0) {
                    b = buffer.readByte();
                    result |= (b & 0x7F) << 27;
                }
            }
        }
    }
    return result;
}