Example usage for io.netty.buffer ByteBuf isReadable

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

Introduction

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

Prototype

public abstract boolean isReadable(int size);

Source Link

Document

Returns true if and only if this buffer contains equal to or more than the specified number of elements.

Usage

From source file:org.jfxvnc.net.rfb.codec.decoder.rect.CopyRectDecoder.java

License:Apache License

@Override
public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (!in.isReadable(4)) {
        return false;
    }// w w w  .ja v a  2 s  .c o  m
    int sryX = in.readShort();
    int srcY = in.readShort();

    out.add(new CopyImageRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), sryX, srcY));
    return true;
}

From source file:org.jfxvnc.net.rfb.codec.decoder.rect.ZlibRectDecoder.java

License:Apache License

@Override
public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!initialized) {
        if (!in.isReadable(4)) {
            return false;
        }// w  ww  .j av a 2  s  .co  m
        capacity = (int) in.readUnsignedInt();
        initialized = true;
    }
    return super.decode(ctx, in, out);
}

From source file:org.jfxvnc.net.rfb.codec.decoder.ServerCutTextDecoder.java

License:Apache License

@Override
public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (!in.isReadable(8)) {
        return false;
    }/*  w  w  w .  j  a  v  a 2s. c  om*/

    in.markReaderIndex();

    in.skipBytes(4);
    int length = in.readInt();

    if (!in.isReadable(length)) {
        in.resetReaderIndex();
        return false;
    }

    byte[] text = new byte[length];
    in.readBytes(text);
    out.add(new ServerCutTextEvent(new String(text, StandardCharsets.ISO_8859_1)));
    return true;
}

From source file:org.jfxvnc.net.rfb.codec.security.vncauth.VncAuthDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!in.isReadable(16)) {
        return;/*  www .ja  v a2 s. com*/
    }
    byte[] challenge = new byte[16];
    in.readBytes(challenge);
    out.add(new VncAuthSecurityMessage(challenge));
}

From source file:org.l2junity.loginserver.network.client.crypt.Crypt.java

License:Open Source License

@Override
public void encrypt(ByteBuf buf) {
    // Ensure that byte order is little endian because we set new packet size in first 2 bytes
    if (buf.order() != ByteOrder.LITTLE_ENDIAN) {
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
    }/*from   w  ww .j  ava2 s  .  co m*/

    // Checksum & XOR Key or Checksum only
    buf.writeZero(_static ? 8 : 4);

    // Padding
    buf.writeZero(8 - (buf.readableBytes() % 8));

    if (_static) {
        _static = false;

        int key = Rnd.nextInt();
        buf.skipBytes(4); // The first 4 bytes are ignored
        while (buf.readerIndex() < (buf.writerIndex() - 8)) {
            int data = buf.readInt();
            key += data;
            data ^= key;
            buf.setInt(buf.readerIndex() - 4, data);
        }
        buf.setInt(buf.readerIndex(), key);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            STATIC_BLOWFISH_ENGINE.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    } else {
        int checksum = 0;
        while (buf.isReadable(8)) {
            checksum ^= buf.readInt();
        }
        buf.setInt(buf.readerIndex(), checksum);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            _blowfishEngine.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    }
}

From source file:org.l2junity.loginserver.network.client.crypt.Crypt.java

License:Open Source License

@Override
public void decrypt(ByteBuf buf) {
    // Packet size must be multiple of 8
    if ((buf.readableBytes() % 8) != 0) {
        buf.clear();/*w ww .j  a v a 2 s.  c o  m*/
        return;
    }

    final byte[] block = new byte[8];
    while (buf.isReadable(8)) {
        buf.readBytes(block);
        _blowfishEngine.decryptBlock(block, 0);
        buf.setBytes(buf.readerIndex() - block.length, block);
    }

    // verify checksum also dont forget!
}

From source file:org.opendaylight.bgp.concepts.RouteDistinguisherUtil.java

License:Open Source License

/**
 * Parses three types of route distinguisher from given ByteBuf.
 *
 * @param buffer/*  w  w w . j av a2  s. c o  m*/
 * @return RouteDistinguisher
 */
public static RouteDistinguisher parseRouteDistinguisher(final ByteBuf buffer) {
    Preconditions.checkState(buffer != null && buffer.isReadable(RD_LENGTH),
            "Cannot read Route Distinguisher from provided buffer.");
    final int type = buffer.readUnsignedShort();
    final RD_TYPE rdType = RD_TYPE.valueOf(type);
    final StringBuilder routeDistiguisher = new StringBuilder();
    switch (rdType) {
    case AS_2BYTE:
        routeDistiguisher.append(type);
        routeDistiguisher.append(SEPARATOR);
        routeDistiguisher.append(buffer.readUnsignedShort());
        routeDistiguisher.append(SEPARATOR);
        routeDistiguisher.append(buffer.readUnsignedInt());
        return new RouteDistinguisher(new RdTwoOctetAs(routeDistiguisher.toString()));
    case IPV4:
        routeDistiguisher.append(Ipv4Util.addressForByteBuf(buffer).getValue());
        routeDistiguisher.append(SEPARATOR);
        routeDistiguisher.append(buffer.readUnsignedShort());
        return new RouteDistinguisher(new RdIpv4(routeDistiguisher.toString()));
    case AS_4BYTE:
        routeDistiguisher.append(buffer.readUnsignedInt());
        routeDistiguisher.append(SEPARATOR);
        routeDistiguisher.append(buffer.readUnsignedShort());
        return new RouteDistinguisher(new RdAs(routeDistiguisher.toString()));
    default:
        // now that this RD type is not supported, we want to read the remain 6 bytes
        // in order to get the byte index correct
        for (int i = 0; i < 6; i++) {
            routeDistiguisher.append("0x").append(Integer.toHexString(buffer.readByte() & 0xFF)).append(" ");
        }
        LOG.debug("Invalid Route Distinguisher: type={}, rawRouteDistinguisherValue={}", type,
                routeDistiguisher.toString());
        throw new IllegalArgumentException("Invalid Route Distinguisher type " + type);
    }
}

From source file:org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.java

License:Open Source License

/**
 * Reads Path Identifier (4 bytes) from buffer.
 *
 * @param buffer Input buffer./*w  w w .  j a v a2s  .  c o m*/
 * @return Decoded PathId.
 */
public static PathId readPathId(final ByteBuf buffer) {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(ByteBufWriteUtil.INT_BYTES_LENGTH));
    return new PathId(buffer.readUnsignedInt());
}