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:dorkbox.network.pipeline.ByteBufInput.java

License:Apache License

private void readUtf8(int charCount) {
    ByteBuf buffer = byteBuf;
    char[] chars = this.inputChars; // will be the correct size.
    // Try to read 7 bit ASCII chars.
    int charIndex = 0;
    int count = charCount;
    int b;/*w w  w. ja  v a 2s  .  c om*/
    while (charIndex < count) {
        b = buffer.readByte();
        if (b < 0) {
            buffer.readerIndex(buffer.readerIndex() - 1);
            break;
        }
        chars[charIndex++] = (char) b;
    }
    // If buffer didn't hold all chars or any were not ASCII, use slow path for remainder.
    if (charIndex < charCount) {
        readUtf8_slow(charCount, charIndex);
    }
}

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

License:Apache License

private void readUtf8_slow(int charCount, int charIndex) {
    ByteBuf buffer = byteBuf;
    char[] chars = this.inputChars;
    while (charIndex < charCount) {
        int b = buffer.readByte() & 0xFF;
        switch (b >> 4) {
        case 0://  www  . ja  v a 2s.  c om
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            chars[charIndex] = (char) b;
            break;
        case 12:
        case 13:
            chars[charIndex] = (char) ((b & 0x1F) << 6 | buffer.readByte() & 0x3F);
            break;
        case 14:
            chars[charIndex] = (char) ((b & 0x0F) << 12 | (buffer.readByte() & 0x3F) << 6
                    | buffer.readByte() & 0x3F);
            break;
        }
        charIndex++;
    }
}

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

License:Apache License

private String readAscii() {
    ByteBuf buffer = byteBuf;

    int start = buffer.readerIndex() - 1;
    int b;//from   w ww.j a  v a  2s . co  m
    do {
        b = buffer.readByte();
    } while ((b & 0x80) == 0);
    int i = buffer.readerIndex() - 1;
    buffer.setByte(i, buffer.getByte(i) & 0x7F); // Mask end of ascii bit.

    int capp = buffer.readerIndex() - start;

    byte[] ba = new byte[capp];
    buffer.getBytes(start, ba);

    @SuppressWarnings("deprecation")
    String value = new String(ba, 0, 0, capp);

    buffer.setByte(i, buffer.getByte(i) | 0x80);
    return value;
}

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  ww. j ava  2 s.  c om
 */
@Override
public StringBuilder readStringBuilder() {
    ByteBuf buffer = byteBuf;

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

    StringBuilder builder = new StringBuilder(charCount);
    builder.append(inputChars, 0, charCount);
    return builder;
}

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

License:Apache License

/**
 * Reads a 1-9 byte long./*from   w w w  .  j  a va2  s  . co  m*/
 */
@Override
public long readLong(boolean optimizePositive) throws KryoException {
    ByteBuf buffer = byteBuf;
    int b = buffer.readByte();
    long 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 |= (long) (b & 0x7F) << 28;
                    if ((b & 0x80) != 0) {
                        b = buffer.readByte();
                        result |= (long) (b & 0x7F) << 35;
                        if ((b & 0x80) != 0) {
                            b = buffer.readByte();
                            result |= (long) (b & 0x7F) << 42;
                            if ((b & 0x80) != 0) {
                                b = buffer.readByte();
                                result |= (long) (b & 0x7F) << 49;
                                if ((b & 0x80) != 0) {
                                    b = buffer.readByte();
                                    result |= (long) b << 56;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (!optimizePositive) {
        result = result >>> 1 ^ -(result & 1);
    }
    return result;
}

From source file:dorkbox.network.pipeline.discovery.BroadcastServer.java

License:Apache License

/**
 * @return true if the broadcast was responded to, false if it was not a broadcast (and there was no response)
 *//*from  w ww  . j a  va  2s . c  o m*/
public boolean isDiscoveryRequest(final Channel channel, ByteBuf byteBuf, final InetSocketAddress localAddress,
        InetSocketAddress remoteAddress) {
    if (byteBuf.readableBytes() == 1) {
        // this is a BROADCAST discovery event. Don't read the byte unless it is...
        if (byteBuf.getByte(0) == MagicBytes.broadcastID) {
            byteBuf.readByte(); // read the byte to consume it (now that we verified it is a broadcast byte)

            // absolutely MUST send packet > 0 across, otherwise netty will think it failed to write to the socket, and keep trying.
            // (this bug was fixed by netty, however we are keeping this code)
            ByteBuf directBuffer = channel.alloc().ioBuffer(bufferSize);
            directBuffer.writeByte(MagicBytes.broadcastResponseID);

            // now output the port information for TCP/UDP so the broadcast client knows which port to connect to
            // either it will be TCP or UDP, or BOTH

            int enabledFlag = 0;
            if (tcpPort > 0) {
                enabledFlag |= MagicBytes.HAS_TCP;
            }

            if (udpPort > 0) {
                enabledFlag |= MagicBytes.HAS_UDP;
            }

            directBuffer.writeByte(enabledFlag);

            // TCP is always first
            if (tcpPort > 0) {
                directBuffer.writeShort(tcpPort);
            }

            if (udpPort > 0) {
                directBuffer.writeShort(udpPort);
            }

            channel.writeAndFlush(new DatagramPacket(directBuffer, remoteAddress, localAddress));

            logger.info("Responded to host discovery from [{}]", EndPoint.getHostDetails(remoteAddress));

            byteBuf.release();
            return true;
        }
    }

    return false;
}

From source file:dorkbox.network.pipeline.discovery.BroadcastServer.java

License:Apache License

/**
 * @return true if this is a broadcast response, false if it was not a broadcast response
 *///  ww  w .j  a  va2s .  c  om
public static boolean isDiscoveryResponse(ByteBuf byteBuf, final InetAddress remoteAddress,
        final Channel channel) {
    if (byteBuf.readableBytes() <= MagicBytes.maxPacketSize) {
        // this is a BROADCAST discovery RESPONSE event. Don't read the byte unless it is...
        if (byteBuf.getByte(0) == MagicBytes.broadcastResponseID) {
            byteBuf.readByte(); // read the byte to consume it (now that we verified it is a broadcast byte)

            // either it will be TCP or UDP, or BOTH
            int typeID = byteBuf.readByte();

            int tcpPort = 0;
            int udpPort = 0;

            // TCP is always first
            if ((typeID & MagicBytes.HAS_TCP) == MagicBytes.HAS_TCP) {
                tcpPort = byteBuf.readUnsignedShort();
            }

            if ((typeID & MagicBytes.HAS_UDP) == MagicBytes.HAS_UDP) {
                udpPort = byteBuf.readUnsignedShort();
            }

            channel.attr(ClientDiscoverHostHandler.STATE)
                    .set(new BroadcastResponse(remoteAddress, tcpPort, udpPort));

            byteBuf.release();
            return true;
        }
    }

    return false;
}

From source file:dorkbox.util.bytes.OptimizeUtilsByteBuf.java

License:Apache License

/**
 * FROM KRYO//from  w ww.  ja v a  2  s. c  om
 * <p>
 * look at buffer, and see if we can read the length of the int off of it. (from the reader index)
 *
 * @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
 */
public static int canReadInt(ByteBuf buffer) {
    int startIndex = buffer.readerIndex();
    try {
        int remaining = buffer.readableBytes();
        for (int offset = 0, count = 1; offset < 32 && remaining > 0; offset += 7, remaining--, count++) {
            int b = buffer.readByte();
            if ((b & 0x80) == 0) {
                return count;
            }
        }
        return 0;
    } finally {
        buffer.readerIndex(startIndex);
    }
}

From source file:dorkbox.util.bytes.OptimizeUtilsByteBuf.java

License:Apache License

/**
 * FROM KRYO/*from   w  ww  .  ja v a 2s. com*/
 * <p>
 * Reads an int from the buffer that was optimized.
 *
 * @param optimizePositive
 *                 If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
 *                 bytes). This ultimately means that it will use fewer bytes for positive numbers.
 *
 * @return the number of bytes written.
 */
public static int readInt(ByteBuf buffer, boolean optimizePositive) {
    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.util.bytes.OptimizeUtilsByteBuf.java

License:Apache License

/**
 * FROM KRYO// w  w w.ja  v  a  2 s  .c o m
 * <p>
 * Reads a 1-9 byte long.
 *
 * @param optimizePositive
 *                 If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
 *                 bytes). This ultimately means that it will use fewer bytes for positive numbers.
 */
public static long readLong(ByteBuf buffer, boolean optimizePositive) {
    int b = buffer.readByte();
    long 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 |= (long) (b & 0x7F) << 28;
                    if ((b & 0x80) != 0) {
                        b = buffer.readByte();
                        result |= (long) (b & 0x7F) << 35;
                        if ((b & 0x80) != 0) {
                            b = buffer.readByte();
                            result |= (long) (b & 0x7F) << 42;
                            if ((b & 0x80) != 0) {
                                b = buffer.readByte();
                                result |= (long) (b & 0x7F) << 49;
                                if ((b & 0x80) != 0) {
                                    b = buffer.readByte();
                                    result |= (long) b << 56;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (!optimizePositive) {
        result = result >>> 1 ^ -(result & 1);
    }
    return result;
}