io.netty.util.internal.EmptyArrays Java Examples

The following examples show how to use io.netty.util.internal.EmptyArrays. 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: AsciiString.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the characters in this string to a character array.
 *
 * @return a character array containing the characters of this string.
 */
public char[] toCharArray(int start, int end) {
    int length = end - start;
    if (length == 0) {
        return EmptyArrays.EMPTY_CHARS;
    }

    if (isOutOfBounds(start, length, length())) {
        throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= srcIdx + length("
                        + length + ") <= srcLen(" + length() + ')');
    }

    final char[] buffer = new char[length];
    for (int i = 0, j = start + arrayOffset(); i < length; i++, j++) {
        buffer[i] = b2c(value[j]);
    }
    return buffer;
}
 
Example #2
Source File: WebSocketServerHandshaker.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor specifying the destination web socket location
 *
 * @param version
 *            the protocol version
 * @param uri
 *            URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
 *            sent to this URL.
 * @param subprotocols
 *            CSV of supported protocols. Null if sub protocols not supported.
 * @param maxFramePayloadLength
 *            Maximum length of a frame's payload
 */
protected WebSocketServerHandshaker(
        WebSocketVersion version, String uri, String subprotocols,
        int maxFramePayloadLength) {
    this.version = version;
    this.uri = uri;
    if (subprotocols != null) {
        String[] subprotocolArray = subprotocols.split(",");
        for (int i = 0; i < subprotocolArray.length; i++) {
            subprotocolArray[i] = subprotocolArray[i].trim();
        }
        this.subprotocols = subprotocolArray;
    } else {
        this.subprotocols = EmptyArrays.EMPTY_STRINGS;
    }
    this.maxFramePayloadLength = maxFramePayloadLength;
}
 
Example #3
Source File: ThreadPerChannelEventLoopGroup.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new {@link ThreadPerChannelEventLoopGroup}.
 *
 * @param maxChannels       the maximum number of channels to handle with this instance. Once you try to register
 *                          a new {@link Channel} and the maximum is exceed it will throw an
 *                          {@link ChannelException} on the {@link #register(Channel)} and
 *                          {@link #register(Channel, ChannelPromise)} method.
 *                          Use {@code 0} to use no limit
 * @param threadFactory     the {@link ThreadFactory} used to create new {@link Thread} instances that handle the
 *                          registered {@link Channel}s
 * @param args              arguments which will passed to each {@link #newChild(Object...)} call.
 */
protected ThreadPerChannelEventLoopGroup(int maxChannels, ThreadFactory threadFactory, Object... args) {
    if (maxChannels < 0) {
        throw new IllegalArgumentException(String.format(
                "maxChannels: %d (expected: >= 0)", maxChannels));
    }
    if (threadFactory == null) {
        throw new NullPointerException("threadFactory");
    }

    if (args == null) {
        childArgs = EmptyArrays.EMPTY_OBJECTS;
    } else {
        childArgs = args.clone();
    }

    this.maxChannels = maxChannels;
    this.threadFactory = threadFactory;

    tooManyChannels = new ChannelException("too many channels (max: " + maxChannels + ')');
    tooManyChannels.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
}
 
Example #4
Source File: ReferenceCountedOpenSslEngine.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public final String[] getEnabledCipherSuites() {
    final String[] enabled;
    synchronized (this) {
        if (!isDestroyed()) {
            enabled = SSL.getCiphers(ssl);
        } else {
            return EmptyArrays.EMPTY_STRINGS;
        }
    }
    if (enabled == null) {
        return EmptyArrays.EMPTY_STRINGS;
    } else {
        synchronized (this) {
            for (int i = 0; i < enabled.length; i++) {
                String mapped = toJavaCipherSuite(enabled[i]);
                if (mapped != null) {
                    enabled[i] = mapped;
                }
            }
        }
        return enabled;
    }
}
 
Example #5
Source File: ThreadPerChannelEventLoopGroup.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new {@link ThreadPerChannelEventLoopGroup}.
 *
 * @param maxChannels       the maximum number of channels to handle with this instance. Once you try to register
 *                          a new {@link Channel} and the maximum is exceed it will throw an
 *                          {@link ChannelException} on the {@link #register(Channel)} and
 *                          {@link #register(ChannelPromise)} method.
 *                          Use {@code 0} to use no limit
 * @param executor          the {@link Executor} used to create new {@link Thread} instances that handle the
 *                          registered {@link Channel}s
 * @param args              arguments which will passed to each {@link #newChild(Object...)} call.
 */
protected ThreadPerChannelEventLoopGroup(int maxChannels, Executor executor, Object... args) {
    if (maxChannels < 0) {
        throw new IllegalArgumentException(String.format(
                "maxChannels: %d (expected: >= 0)", maxChannels));
    }
    if (executor == null) {
        throw new NullPointerException("executor");
    }

    if (args == null) {
        childArgs = EmptyArrays.EMPTY_OBJECTS;
    } else {
        childArgs = args.clone();
    }

    this.maxChannels = maxChannels;
    this.executor = executor;

    tooManyChannels = ThrowableUtil.unknownStackTrace(
            new ChannelException("too many channels (max: " + maxChannels + ')'),
            ThreadPerChannelEventLoopGroup.class, "nextChild()");
}
 
Example #6
Source File: WebSocketServerHandshaker.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor specifying the destination web socket location
 *
 * @param version
 *            the protocol version
 * @param uri
 *            URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
 *            sent to this URL.
 * @param subprotocols
 *            CSV of supported protocols. Null if sub protocols not supported.
 * @param maxFramePayloadLength
 *            Maximum length of a frame's payload
 */
protected WebSocketServerHandshaker(
        WebSocketVersion version, String uri, String subprotocols,
        int maxFramePayloadLength) {
    this.version = version;
    this.uri = uri;
    if (subprotocols != null) {
        String[] subprotocolArray = StringUtil.split(subprotocols, ',');
        for (int i = 0; i < subprotocolArray.length; i++) {
            subprotocolArray[i] = subprotocolArray[i].trim();
        }
        this.subprotocols = subprotocolArray;
    } else {
        this.subprotocols = EmptyArrays.EMPTY_STRINGS;
    }
    this.maxFramePayloadLength = maxFramePayloadLength;
}
 
Example #7
Source File: ReferenceCountedOpenSslEngine.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getValueNames() {
    Map<String, Object> values = this.values;
    if (values == null || values.isEmpty()) {
        return EmptyArrays.EMPTY_STRINGS;
    }
    return values.keySet().toArray(new String[values.size()]);
}
 
Example #8
Source File: ZlibTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGZIPCompressOnly() throws Exception {
    testGZIPCompressOnly0(null); // Do not write anything; just finish the stream.
    testGZIPCompressOnly0(EmptyArrays.EMPTY_BYTES); // Write an empty array.
    testGZIPCompressOnly0(BYTES_SMALL);
    testGZIPCompressOnly0(BYTES_LARGE);
}
 
Example #9
Source File: OpenSslEngine.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getValueNames() {
    Map<String, Object> values = this.values;
    if (values == null || values.isEmpty()) {
        return EmptyArrays.EMPTY_STRINGS;
    }
    return values.keySet().toArray(new String[values.size()]);
}
 
Example #10
Source File: OpenSslEngine.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getEnabledProtocols() {
    List<String> enabled = new ArrayList<String>();
    // Seems like there is no way to explict disable SSLv2Hello in openssl so it is always enabled
    enabled.add(PROTOCOL_SSL_V2_HELLO);
    int opts = SSL.getOptions(ssl);
    if ((opts & SSL.SSL_OP_NO_TLSv1) == 0) {
        enabled.add(PROTOCOL_TLS_V1);
    }
    if ((opts & SSL.SSL_OP_NO_TLSv1_1) == 0) {
        enabled.add(PROTOCOL_TLS_V1_1);
    }
    if ((opts & SSL.SSL_OP_NO_TLSv1_2) == 0) {
        enabled.add(PROTOCOL_TLS_V1_2);
    }
    if ((opts & SSL.SSL_OP_NO_SSLv2) == 0) {
        enabled.add(PROTOCOL_SSL_V2);
    }
    if ((opts & SSL.SSL_OP_NO_SSLv3) == 0) {
        enabled.add(PROTOCOL_SSL_V3);
    }
    int size = enabled.size();
    if (size == 0) {
        return EmptyArrays.EMPTY_STRINGS;
    } else {
        return enabled.toArray(new String[size]);
    }
}
 
Example #11
Source File: OpenSslEngine.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getEnabledCipherSuites() {
    String[] enabled = SSL.getCiphers(ssl);
    if (enabled == null) {
        return EmptyArrays.EMPTY_STRINGS;
    } else {
        for (int i = 0; i < enabled.length; i++) {
            String mapped = toJavaCipherSuite(enabled[i]);
            if (mapped != null) {
                enabled[i] = mapped;
            }
        }
        return enabled;
    }
}
 
Example #12
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 #13
Source File: CompositeByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
    checkIndex(index, length);
    if (length == 0) {
        return in.read(EmptyArrays.EMPTY_BYTES);
    }

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

    do {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.capacity() - (index - adjustment));
        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;
}
 
Example #14
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 #15
Source File: FixedCompositeByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer[] nioBuffers(int index, int length) {
    checkIndex(index, length);
    if (length == 0) {
        return EmptyArrays.EMPTY_BYTE_BUFFERS;
    }

    RecyclableArrayList array = RecyclableArrayList.newInstance(buffers.length);
    try {
        Component c = findComponent(index);
        int i = c.index;
        int adjustment = c.offset;
        ByteBuf s = c.buf;
        for (;;) {
            int localLength = Math.min(length, s.readableBytes() - (index - adjustment));
            switch (s.nioBufferCount()) {
                case 0:
                    throw new UnsupportedOperationException();
                case 1:
                    array.add(s.nioBuffer(index - adjustment, localLength));
                    break;
                default:
                    Collections.addAll(array, s.nioBuffers(index - adjustment, localLength));
            }

            index += localLength;
            length -= localLength;
            adjustment += s.readableBytes();
            if (length <= 0) {
                break;
            }
            s = buffer(++i);
        }

        return array.toArray(new ByteBuffer[array.size()]);
    } finally {
        array.recycle();
    }
}
 
Example #16
Source File: SSLEngineTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
protected void testEnablingAnAlreadyDisabledSslProtocol(String[] protocols1, String[] protocols2) throws Exception {
    SSLEngine sslEngine = null;
    try {
        File serverKeyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
        File serverCrtFile = new File(getClass().getResource("test.crt").getFile());
        serverSslCtx = SslContextBuilder.forServer(serverCrtFile, serverKeyFile)
           .sslProvider(sslServerProvider())
           .sslContextProvider(serverSslContextProvider())
           .build();

        sslEngine = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

        // Disable all protocols
        sslEngine.setEnabledProtocols(EmptyArrays.EMPTY_STRINGS);

        // The only protocol that should be enabled is SSLv2Hello
        String[] enabledProtocols = sslEngine.getEnabledProtocols();
        assertArrayEquals(protocols1, enabledProtocols);

        // Enable a protocol that is currently disabled
        sslEngine.setEnabledProtocols(new String[]{ PROTOCOL_TLS_V1_2 });

        // The protocol that was just enabled should be returned
        enabledProtocols = sslEngine.getEnabledProtocols();
        assertEquals(protocols2.length, enabledProtocols.length);
        assertArrayEquals(protocols2, enabledProtocols);
    } finally {
        if (sslEngine != null) {
            sslEngine.closeInbound();
            sslEngine.closeOutbound();
            cleanupServerSslEngine(sslEngine);
        }
    }
}
 
Example #17
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 #18
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 #19
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 #20
Source File: SslContext.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static KeyManagerFactory buildKeyManagerFactory(X509Certificate[] certChainFile,
                                                String keyAlgorithm, PrivateKey key,
                                                String keyPassword, KeyManagerFactory kmf)
        throws KeyStoreException, NoSuchAlgorithmException, IOException,
        CertificateException, UnrecoverableKeyException {
    char[] keyPasswordChars = keyPassword == null ? EmptyArrays.EMPTY_CHARS : keyPassword.toCharArray();
    KeyStore ks = buildKeyStore(certChainFile, key, keyPasswordChars);
    // Set up key manager factory to use our key store
    if (kmf == null) {
        kmf = KeyManagerFactory.getInstance(keyAlgorithm);
    }
    kmf.init(ks, keyPasswordChars);

    return kmf;
}
 
Example #21
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 #22
Source File: ZlibTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testGZIPCompressOnly() throws Exception {
    testGZIPCompressOnly0(null); // Do not write anything; just finish the stream.
    testGZIPCompressOnly0(EmptyArrays.EMPTY_BYTES); // Write an empty array.
    testGZIPCompressOnly0(BYTES_SMALL);
    testGZIPCompressOnly0(BYTES_LARGE);
}
 
Example #23
Source File: ProxyHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void test() throws Exception {
    final SuccessTestHandler testHandler = new SuccessTestHandler();
    Bootstrap b = new Bootstrap();
    b.group(group);
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.AUTO_READ, ThreadLocalRandom.current().nextBoolean());
    b.resolver(NoopAddressResolverGroup.INSTANCE);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(clientHandlers);
            p.addLast(new LineBasedFrameDecoder(64));
            p.addLast(testHandler);
        }
    });

    boolean finished = b.connect(destination).channel().closeFuture().await(10, TimeUnit.SECONDS);

    logger.debug("Received messages: {}", testHandler.received);

    if (testHandler.exceptions.isEmpty()) {
        logger.debug("No recorded exceptions on the client side.");
    } else {
        for (Throwable t : testHandler.exceptions) {
            logger.debug("Recorded exception on the client side: {}", t);
        }
    }

    assertProxyHandlers(true);

    assertThat(testHandler.received.toArray(), is(new Object[] { "0", "1", "2", "3" }));
    assertThat(testHandler.exceptions.toArray(), is(EmptyArrays.EMPTY_OBJECTS));
    assertThat(testHandler.eventCount, is(expectedEventCount));
    assertThat(finished, is(true));
}
 
Example #24
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 #25
Source File: InsecureExtendedTrustManager.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public X509Certificate[] getAcceptedIssuers() {
    return EmptyArrays.EMPTY_X509_CERTIFICATES;
}
 
Example #26
Source File: FingerprintTrustManagerFactorySHA256.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public X509Certificate[] getAcceptedIssuers() {
    return EmptyArrays.EMPTY_X509_CERTIFICATES;
}
 
Example #27
Source File: InsecureExtendedTrustManager.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public X509Certificate[] getAcceptedIssuers() {
    return EmptyArrays.EMPTY_X509_CERTIFICATES;
}
 
Example #28
Source File: InsecureExtendedTrustManager.java    From CapturePacket with MIT License 4 votes vote down vote up
@Override
public X509Certificate[] getAcceptedIssuers() {
    return EmptyArrays.EMPTY_X509_CERTIFICATES;
}
 
Example #29
Source File: InsecureTrustManagerFactory.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public X509Certificate[] getAcceptedIssuers() {
    return EmptyArrays.EMPTY_X509_CERTIFICATES;
}
 
Example #30
Source File: FingerprintTrustManagerFactory.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public X509Certificate[] getAcceptedIssuers() {
    return EmptyArrays.EMPTY_X509_CERTIFICATES;
}