Example usage for io.netty.util.internal EmptyArrays EMPTY_BYTES

List of usage examples for io.netty.util.internal EmptyArrays EMPTY_BYTES

Introduction

In this page you can find the example usage for io.netty.util.internal EmptyArrays EMPTY_BYTES.

Prototype

null EMPTY_BYTES

To view the source code for io.netty.util.internal EmptyArrays EMPTY_BYTES.

Click Source Link

Usage

From source file:divconq.http.multipart.AbstractDiskHttpData.java

License:Apache License

@Override
public byte[] get() throws IOException {
    if (file == null) {
        return EmptyArrays.EMPTY_BYTES;
    }//from   ww  w  . ja  v a 2s  . c o  m
    return readFrom(file);
}

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

License:Apache License

@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
    checkIndex(index, length);//  w  ww .  j  a  va 2  s.co  m
    if (length == 0) {
        return in.read(EmptyArrays.EMPTY_BYTES);
    }

    int i = findIndex(index);
    int readBytes = 0;

    do {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.writableBytes());
        int localReadBytes = s.setBytes(index - adjustment, in, localLength);
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        }

        if (localReadBytes == localLength) {
            index += localLength;
            length -= localLength;
            readBytes += localLength;
            i++;
        } else {
            index += localReadBytes;
            length -= localReadBytes;
            readBytes += localReadBytes;
        }
    } while (length > 0);

    return readBytes;
}

From source file:shi.cs.MqttEncoder.java

License:Apache License

private static ByteBuf encodeConnectMessage(ByteBufAllocator byteBufAllocator, MqttConnectMessage message) {
    int payloadBufferSize = 0;

    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    MqttConnectVariableHeader variableHeader = message.variableHeader();
    MqttConnectPayload payload = message.payload();
    MqttVersion mqttVersion = MqttVersion.fromProtocolNameAndLevel(variableHeader.name(),
            (byte) variableHeader.version());

    // Client id/*from w ww.j  a  v  a2 s  . c om*/
    String clientIdentifier = payload.clientIdentifier();
    if (!isValidClientId(mqttVersion, clientIdentifier)) {
        throw new MqttIdentifierRejectedException("invalid clientIdentifier: " + clientIdentifier);
    }
    byte[] clientIdentifierBytes = encodeStringUtf8(clientIdentifier);
    payloadBufferSize += 2 + clientIdentifierBytes.length;

    // Will topic and message
    String willTopic = payload.willTopic();
    byte[] willTopicBytes = willTopic != null ? encodeStringUtf8(willTopic) : EmptyArrays.EMPTY_BYTES;
    String willMessage = payload.willMessage();
    byte[] willMessageBytes = willMessage != null ? encodeStringUtf8(willMessage) : EmptyArrays.EMPTY_BYTES;
    if (variableHeader.isWillFlag()) {
        payloadBufferSize += 2 + willTopicBytes.length;
        payloadBufferSize += 2 + willMessageBytes.length;
    }

    String userName = payload.userName();
    byte[] userNameBytes = userName != null ? encodeStringUtf8(userName) : EmptyArrays.EMPTY_BYTES;
    if (variableHeader.hasUserName()) {
        payloadBufferSize += 2 + userNameBytes.length;
    }

    String password = payload.password();
    byte[] passwordBytes = password != null ? encodeStringUtf8(password) : EmptyArrays.EMPTY_BYTES;
    if (variableHeader.hasPassword()) {
        payloadBufferSize += 2 + passwordBytes.length;
    }

    // Fixed header
    byte[] protocolNameBytes = mqttVersion.protocolNameBytes();
    int variableHeaderBufferSize = 2 + protocolNameBytes.length + 4;
    int variablePartSize = variableHeaderBufferSize + payloadBufferSize;
    int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize);
    ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variablePartSize);
    buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
    writeVariableLengthInt(buf, variablePartSize);

    buf.writeShort(protocolNameBytes.length);
    buf.writeBytes(protocolNameBytes);

    buf.writeByte(variableHeader.version());
    buf.writeByte(getConnVariableHeaderFlag(variableHeader));
    buf.writeShort(variableHeader.keepAliveTimeSeconds());

    // Payload
    buf.writeShort(clientIdentifierBytes.length);
    buf.writeBytes(clientIdentifierBytes, 0, clientIdentifierBytes.length);
    if (variableHeader.isWillFlag()) {
        buf.writeShort(willTopicBytes.length);
        buf.writeBytes(willTopicBytes, 0, willTopicBytes.length);
        buf.writeShort(willMessageBytes.length);
        buf.writeBytes(willMessageBytes, 0, willMessageBytes.length);
    }
    if (variableHeader.hasUserName()) {
        buf.writeShort(userNameBytes.length);
        buf.writeBytes(userNameBytes, 0, userNameBytes.length);
    }
    if (variableHeader.hasPassword()) {
        buf.writeShort(passwordBytes.length);
        buf.writeBytes(passwordBytes, 0, passwordBytes.length);
    }
    return buf;
}