Java Code Examples for io.netty.util.internal.EmptyArrays#EMPTY_BYTES

The following examples show how to use io.netty.util.internal.EmptyArrays#EMPTY_BYTES . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractDiskHttpData.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] get() throws IOException {
    if (file == null) {
        return EmptyArrays.EMPTY_BYTES;
    }
    return readFrom(file);
}
 
Example 2
Source File: ReferenceCountedOpenSslEngine.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getId() {
    synchronized (ReferenceCountedOpenSslEngine.this) {
        if (id == null) {
            return EmptyArrays.EMPTY_BYTES;
        }
        return id.clone();
    }
}
 
Example 3
Source File: CompositeByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] array() {
    switch (components.size()) {
    case 0:
        return EmptyArrays.EMPTY_BYTES;
    case 1:
        return components.get(0).buf.array();
    default:
        throw new UnsupportedOperationException();
    }
}
 
Example 4
Source File: ReadOnlyPooledHeapByteBuf.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
@Override
protected void deallocate() {
    freeArray(array);
    array = EmptyArrays.EMPTY_BYTES;
    parent = null;
    offset = 0;
    RECYCLER.recycleInstance(this);
}
 
Example 5
Source File: AbstractDiskHttpData.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] get() throws IOException {
    if (file == null) {
        return EmptyArrays.EMPTY_BYTES;
    }
    return readFrom(file);
}
 
Example 6
Source File: CloseWebSocketFrame.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static ByteBuf newBinaryData(int statusCode, String reasonText) {
    byte[] reasonBytes = EmptyArrays.EMPTY_BYTES;
    if (reasonText != null) {
        reasonBytes = reasonText.getBytes(CharsetUtil.UTF_8);
    }

    ByteBuf binaryData = Unpooled.buffer(2 + reasonBytes.length);
    binaryData.writeShort(statusCode);
    if (reasonBytes.length > 0) {
        binaryData.writeBytes(reasonBytes);
    }

    binaryData.readerIndex(0);
    return binaryData;
}
 
Example 7
Source File: CompositeByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] array() {
    switch (components.size()) {
    case 0:
        return EmptyArrays.EMPTY_BYTES;
    case 1:
        return components.get(0).buf.array();
    default:
        throw new UnsupportedOperationException();
    }
}
 
Example 8
Source File: MqttEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
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());

    // as MQTT 3.1 & 3.1.1 spec, If the User Name Flag is set to 0, the Password Flag MUST be set to 0
    if (!variableHeader.hasUserName() && variableHeader.hasPassword()) {
        throw new DecoderException("Without a username, the password MUST be not set");
    }

    // Client id
    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;
    byte[] willMessage = payload.willMessageInBytes();
    byte[] willMessageBytes = willMessage != null ? 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;
    }

    byte[] password = payload.passwordInBytes();
    byte[] passwordBytes = password != null ? 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;
}
 
Example 9
Source File: EmptyByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] array() {
    return EmptyArrays.EMPTY_BYTES;
}
 
Example 10
Source File: JZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) {
    if (finished) {
        promise.setSuccess();
        return promise;
    }
    finished = true;

    ByteBuf footer;
    try {
        // Configure input.
        z.next_in = EmptyArrays.EMPTY_BYTES;
        z.next_in_index = 0;
        z.avail_in = 0;

        // Configure output.
        byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header
        z.next_out = out;
        z.next_out_index = 0;
        z.avail_out = out.length;

        // Write the ADLER32 checksum (stream footer).
        int resultCode = z.deflate(JZlib.Z_FINISH);
        if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
            promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode));
            return promise;
        } else if (z.next_out_index != 0) {
            footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index);
        } else {
            footer = Unpooled.EMPTY_BUFFER;
        }
    } finally {
        z.deflateEnd();

        // Deference the external references explicitly to tell the VM that
        // the allocated byte arrays are temporary so that the call stack
        // can be utilized.
        // I'm not sure if the modern VMs do this optimization though.
        z.next_in = null;
        z.next_out = null;
    }
    return ctx.writeAndFlush(footer, promise);
}
 
Example 11
Source File: ReadOnlyPooledHeapByteBuf.java    From spring-boot-protocol with Apache License 2.0 4 votes vote down vote up
private ReadOnlyPooledHeapByteBuf() {
    super(0);
    this.array = EmptyArrays.EMPTY_BYTES;
    this.offset = 0;
}
 
Example 12
Source File: EmptyByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] array() {
    return EmptyArrays.EMPTY_BYTES;
}
 
Example 13
Source File: JZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) {
    if (finished) {
        promise.setSuccess();
        return promise;
    }
    finished = true;

    ByteBuf footer;
    try {
        // Configure input.
        z.next_in = EmptyArrays.EMPTY_BYTES;
        z.next_in_index = 0;
        z.avail_in = 0;

        // Configure output.
        byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header
        z.next_out = out;
        z.next_out_index = 0;
        z.avail_out = out.length;

        // Write the ADLER32 checksum (stream footer).
        int resultCode = z.deflate(JZlib.Z_FINISH);
        if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
            promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode));
            return promise;
        } else if (z.next_out_index != 0) {
            footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index);
        } else {
            footer = Unpooled.EMPTY_BUFFER;
        }
    } finally {
        z.deflateEnd();

        // Deference the external references explicitly to tell the VM that
        // the allocated byte arrays are temporary so that the call stack
        // can be utilized.
        // I'm not sure if the modern VMs do this optimization though.
        z.next_in = null;
        z.next_out = null;
    }
    return ctx.writeAndFlush(footer, promise);
}
 
Example 14
Source File: TemporaryThreadLocals.java    From armeria with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
void clear() {
    byteArray = EmptyArrays.EMPTY_BYTES;
    stringBuilder = inflate(new StringBuilder());
    charArray = EmptyArrays.EMPTY_CHARS;
}
 
Example 15
Source File: MqttEncoder.java    From mithqtt with Apache License 2.0 4 votes vote down vote up
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.protocolName(),
            (byte) variableHeader.protocolLevel());

    // Client id
    String clientId = payload.clientId();
    byte[] clientIdBytes = encodeStringUtf8(clientId);
    payloadBufferSize += 2 + clientIdBytes.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.willFlag()) {
        payloadBufferSize += 2 + willTopicBytes.length;
        payloadBufferSize += 2 + willMessageBytes.length;
    }

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

    String password = payload.password();
    byte[] passwordBytes = password != null ? encodeStringUtf8(password) : EmptyArrays.EMPTY_BYTES;
    if (variableHeader.passwordFlag()) {
        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.protocolLevel());
    buf.writeByte(getConnVariableHeaderFlag(variableHeader));
    buf.writeShort(variableHeader.keepAlive());

    // Payload
    buf.writeShort(clientIdBytes.length);
    buf.writeBytes(clientIdBytes, 0, clientIdBytes.length);
    if (variableHeader.willFlag()) {
        buf.writeShort(willTopicBytes.length);
        buf.writeBytes(willTopicBytes, 0, willTopicBytes.length);
        buf.writeShort(willMessageBytes.length);
        buf.writeBytes(willMessageBytes, 0, willMessageBytes.length);
    }
    if (variableHeader.userNameFlag()) {
        buf.writeShort(userNameBytes.length);
        buf.writeBytes(userNameBytes, 0, userNameBytes.length);
    }
    if (variableHeader.passwordFlag()) {
        buf.writeShort(passwordBytes.length);
        buf.writeBytes(passwordBytes, 0, passwordBytes.length);
    }

    return buf;
}