Example usage for io.netty.buffer ByteBuf capacity

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

Introduction

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

Prototype

public abstract int capacity();

Source Link

Document

Returns the number of bytes (octets) this buffer can contain.

Usage

From source file:net.malisis.switches.tileentity.SwitchTileEntity.java

License:Open Source License

@Override
public void readFromNBT(NBTTagCompound tag) {
    super.readFromNBT(tag);
    ByteBuf bytes = Unpooled.copiedBuffer(tag.getByteArray("linkedPos"));
    for (int i = 0; i < bytes.capacity() / 8; i++)
        linkedPos.add(BlockPos.fromLong(bytes.readLong()));
}

From source file:net.marfgamer.jraknet.client.RakNetClient.java

License:Open Source License

/**
 * Sends a raw message to the specified address. Be careful when using this
 * method, because if it is used incorrectly it could break server sessions
 * entirely! If you are wanting to send a message to a session, you are
 * probably looking for the/*from  ww  w .  j  av  a 2 s .c o  m*/
 * {@link net.marfgamer.jraknet.session.RakNetSession#sendMessage(net.marfgamer.jraknet.protocol.Reliability, net.marfgamer.jraknet.Packet)
 * sendMessage} method.
 * 
 * @param buf
 *            the buffer to send.
 * @param address
 *            the address to send the buffer to.
 */
public final void sendNettyMessage(ByteBuf buf, InetSocketAddress address) {
    channel.writeAndFlush(new DatagramPacket(buf, address));
    RakNetLogger.debug(this, "Sent netty message with size of " + buf.capacity() + " bytes ("
            + (buf.capacity() * 8) + " bits) to " + address);
}

From source file:net.marfgamer.jraknet.server.RakNetServer.java

License:Open Source License

/**
 * Sends a raw message to the specified address. Be careful when using this
 * method, because if it is used incorrectly it could break server sessions
 * entirely! If you are wanting to send a message to a session, you are
 * probably looking for the// w ww.jav a 2s  .  co  m
 * {@link net.marfgamer.jraknet.session.RakNetSession#sendMessage(net.marfgamer.jraknet.protocol.Reliability, net.marfgamer.jraknet.Packet)
 * sendMessage} method.
 * 
 * @param buf
 *            the buffer to send.
 * @param address
 *            the address to send the buffer to.
 */
public final void sendNettyMessage(ByteBuf buf, InetSocketAddress address) {
    channel.writeAndFlush(new DatagramPacket(buf, address));
    RakNetLogger.debug(this, "Sent netty message with size of " + buf.capacity() + " bytes ("
            + (buf.capacity() * 8) + ") to " + address);
}

From source file:net.marfgamer.jraknet.session.RakNetSession.java

License:Open Source License

/**
 * Sends a raw message/*from   w  ww.jav  a2  s.  c  om*/
 * 
 * @param buf
 *            the buffer to send.
 */
public final void sendRawMessage(ByteBuf buf) {
    channel.writeAndFlush(new DatagramPacket(buf, this.address));
    RakNetLogger.debug(loggerName,
            "Sent raw buffer with size of " + buf.capacity() + " bytes (" + (buf.capacity() * 8) + " bits)");
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

@Override
public AlternativeCompositeByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
    checkDstIndex(index, length, dstIndex, dst.capacity());
    if (length == 0) {
        return this;
    }/*from   w  w w . j a  v  a 2  s. co m*/

    int i = findIndex(index);
    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.readableBytes() - (index - adjustment));
        s.getBytes(index - adjustment, dst, dstIndex, localLength);
        index += localLength;
        dstIndex += localLength;
        length -= localLength;
        i++;
    }
    return this;
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

@Override
public AlternativeCompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
    checkSrcIndex(index, length, srcIndex, src.capacity());
    if (length == 0) {
        return this;
    }//from www. jav a 2s . c o  m

    int i = findIndex(index);
    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.writableBytes());
        s.setBytes(index - adjustment, src, srcIndex, localLength);
        index += localLength;
        srcIndex += localLength;
        length -= localLength;
        i++;
    }
    return this;
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

private void copyTo(int index, int length, int componentId, ByteBuf dst) {
    int dstIndex = 0;
    int i = componentId;

    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;//from  www. j  a v  a  2 s  . c  om
        int adjustment = c.offset;
        int localLength = Math.min(length, s.readableBytes() - (index - adjustment));
        s.getBytes(index - adjustment, dst, dstIndex, localLength);
        index += localLength;
        dstIndex += localLength;
        length -= localLength;
        i++;
    }

    dst.writerIndex(dst.capacity());
}

From source file:netty.syslog.DecoderUtil.java

License:Open Source License

static String readStringToSpace(ByteBuf buffer, boolean checkNull) {
    if (checkNull && peek(buffer) == '-') {
        buffer.readByte();//from  w  w  w. j  a v a2 s  .  co  m
        return null;
    }
    int length = -1;
    for (int i = buffer.readerIndex(); i < buffer.capacity(); i++) {
        if (buffer.getByte(i) == ' ') {
            length = i - buffer.readerIndex();
            break;
        }
    }
    if (length < 0) {
        length = buffer.readableBytes();
    }
    final String s = buffer.toString(buffer.readerIndex(), length, CharsetUtil.UTF_8);
    buffer.skipBytes(length);
    return s;
}

From source file:org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl.java

License:Apache License

protected ByteBuf copyMessageBuffer(ByteBuf buffer, int skipBytes) {

    ByteBuf newNettyBuffer = Unpooled.buffer(buffer.capacity() - PACKET_HEADERS_SIZE - skipBytes);

    int read = buffer.readerIndex();
    int writ = buffer.writerIndex();
    buffer.readerIndex(PACKET_HEADERS_SIZE);

    newNettyBuffer.writeBytes(buffer, buffer.readableBytes() - skipBytes);
    buffer.setIndex(read, writ);//from w w w . ja va  2 s .  com
    newNettyBuffer.setIndex(0, writ - PACKET_HEADERS_SIZE - skipBytes);

    return newNettyBuffer;
}

From source file:org.apache.activemq.artemis.core.protocol.mqtt.MQTTPublishManager.java

License:Apache License

/**
 * Sends a message either on behalf of the client or on behalf of the broker (Will Messages)
 * @param messageId// w  w w  .  j  a  v a  2s. c om
 * @param topic
 * @param qos
 * @param payload
 * @param retain
 * @param internal if true means on behalf of the broker (skips authorisation) and does not return ack.
 * @throws Exception
 */
void sendInternal(int messageId, String topic, int qos, ByteBuf payload, boolean retain, boolean internal)
        throws Exception {
    synchronized (lock) {
        Message serverMessage = MQTTUtil.createServerMessageFromByteBuf(session, topic, retain, qos, payload);

        if (qos > 0) {
            serverMessage.setDurable(MQTTUtil.DURABLE_MESSAGES);
        }

        if (qos < 2 || !state.getPubRec().contains(messageId)) {
            if (qos == 2 && !internal)
                state.getPubRec().add(messageId);

            Transaction tx = session.getServerSession().newTransaction();
            try {
                if (internal) {
                    session.getServer().getPostOffice().route(serverMessage, tx, true);
                } else {
                    session.getServerSession().send(tx, serverMessage, true, false);
                }

                if (retain) {
                    boolean reset = payload instanceof EmptyByteBuf || payload.capacity() == 0;
                    session.getRetainMessageManager().handleRetainedMessage(serverMessage, topic, reset, tx);
                }
                tx.commit();
            } catch (Throwable t) {
                logger.warn(t.getMessage(), t);
                tx.rollback();
                throw t;
            }
            createMessageAck(messageId, qos, internal);
        }
    }
}