Search in sources :

Example 6 with Encoder

use of java.util.Base64.Encoder in project vert.x by eclipse.

the class Http2ServerTest method testQueuePushPromise.

@Test
public void testQueuePushPromise() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    int numPushes = 10;
    Set<String> pushSent = new HashSet<>();
    server.requestHandler(req -> {
        req.response().setChunked(true).write("abc");
        for (int i = 0; i < numPushes; i++) {
            int val = i;
            String path = "/wibble" + val;
            req.response().push(HttpMethod.GET, path, ar -> {
                assertTrue(ar.succeeded());
                assertSame(ctx, Vertx.currentContext());
                pushSent.add(path);
                vertx.setTimer(10, id -> {
                    ar.result().end("wibble-" + val);
                });
            });
        }
    });
    startServer(ctx);
    TestClient client = new TestClient();
    client.settings.maxConcurrentStreams(3);
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        Http2ConnectionEncoder encoder = request.encoder;
        encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
        request.decoder.frameListener(new Http2FrameAdapter() {

            int count = numPushes;

            Set<String> pushReceived = new HashSet<>();

            @Override
            public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
                pushReceived.add(headers.path().toString());
            }

            @Override
            public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
                if (count-- == 0) {
                    vertx.runOnContext(v -> {
                        assertEquals(numPushes, pushSent.size());
                        assertEquals(pushReceived, pushSent);
                        testComplete();
                    });
                }
                return super.onDataRead(ctx, streamId, data, padding, endOfStream);
            }
        });
    });
    fut.sync();
    await();
}
Also used : Context(io.vertx.core.Context) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFuture(io.netty.channel.ChannelFuture) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Context(io.vertx.core.Context) Unpooled(io.netty.buffer.Unpooled) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Map(java.util.Map) ApplicationProtocolNegotiationHandler(io.netty.handler.ssl.ApplicationProtocolNegotiationHandler) ReadStream(io.vertx.core.streams.ReadStream) AbstractHttp2ConnectionHandlerBuilder(io.netty.handler.codec.http2.AbstractHttp2ConnectionHandlerBuilder) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) StreamResetException(io.vertx.core.http.StreamResetException) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelInitializer(io.netty.channel.ChannelInitializer) Http2Flags(io.netty.handler.codec.http2.Http2Flags) Set(java.util.Set) ChannelPipeline(io.netty.channel.ChannelPipeline) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) Future(io.vertx.core.Future) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) Base64(java.util.Base64) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) SslHandler(io.netty.handler.ssl.SslHandler) Http2Headers(io.netty.handler.codec.http2.Http2Headers) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Http2Error(io.netty.handler.codec.http2.Http2Error) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) Trust(io.vertx.test.core.tls.Trust) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Http2EventAdapter(io.netty.handler.codec.http2.Http2EventAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ByteBuf(io.netty.buffer.ByteBuf) WriteStream(io.vertx.core.streams.WriteStream) Http2Stream(io.netty.handler.codec.http2.Http2Stream) BiConsumer(java.util.function.BiConsumer) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) ClosedChannelException(java.nio.channels.ClosedChannelException) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) ApplicationProtocolNames(io.netty.handler.ssl.ApplicationProtocolNames) Test(org.junit.Test) IOException(java.io.IOException) SSLHelper(io.vertx.core.net.impl.SSLHelper) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Http2Settings(io.netty.handler.codec.http2.Http2Settings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) AtomicLong(java.util.concurrent.atomic.AtomicLong) Http2Connection(io.netty.handler.codec.http2.Http2Connection) HttpMethod(io.vertx.core.http.HttpMethod) HttpUtils(io.vertx.core.http.impl.HttpUtils) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with Encoder

use of java.util.Base64.Encoder in project sonarqube by SonarSource.

the class UuidGeneratorImplTest method generate_from_FixedBase_concurrent_test.

@Test
public void generate_from_FixedBase_concurrent_test() throws InterruptedException {
    UuidGenerator.WithFixedBase withFixedBase = underTest.withFixedBase();
    int rounds = 500;
    List<byte[]> uuids1 = new ArrayList<>(rounds);
    List<byte[]> uuids2 = new ArrayList<>(rounds);
    AtomicInteger cnt = new AtomicInteger();
    Thread t1 = new Thread(() -> {
        for (int i = 0; i < rounds; i++) {
            uuids1.add(withFixedBase.generate(cnt.getAndIncrement()));
        }
    });
    Thread t2 = new Thread(() -> {
        for (int i = 0; i < rounds; i++) {
            uuids2.add(withFixedBase.generate(cnt.getAndIncrement()));
        }
    });
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    Base64.Encoder encoder = Base64.getEncoder();
    Set<String> uuids = new HashSet<>(rounds * 2);
    uuids1.forEach(bytes -> uuids.add(encoder.encodeToString(bytes)));
    uuids2.forEach(bytes -> uuids.add(encoder.encodeToString(bytes)));
    assertThat(uuids).hasSize(rounds * 2);
}
Also used : Base64(java.util.Base64) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with Encoder

use of java.util.Base64.Encoder in project j2objc by google.

the class Base64Test method testRoundTrip_wrap_url.

public void testRoundTrip_wrap_url() throws Exception {
    Encoder encoder = Base64.getUrlEncoder();
    Decoder decoder = Base64.getUrlDecoder();
    checkRoundTrip_wrapInputStream(encoder, decoder);
}
Also used : Encoder(java.util.Base64.Encoder) Decoder(java.util.Base64.Decoder)

Example 9 with Encoder

use of java.util.Base64.Encoder in project j2objc by google.

the class Base64Test method testRoundtrip_wrap_mime.

public void testRoundtrip_wrap_mime() throws Exception {
    Encoder encoder = Base64.getMimeEncoder();
    Decoder decoder = Base64.getMimeDecoder();
    checkRoundTrip_wrapInputStream(encoder, decoder);
}
Also used : Encoder(java.util.Base64.Encoder) Decoder(java.util.Base64.Decoder)

Example 10 with Encoder

use of java.util.Base64.Encoder in project j2objc by google.

the class Base64Test method checkWrapOutputStreamConsistentWithEncode.

private static void checkWrapOutputStreamConsistentWithEncode(Encoder encoder) throws Exception {
    final Random random = new Random(32176L);
    // one large write(byte[]) of the whole input
    WriteStrategy allAtOnce = (bytes, out) -> out.write(bytes);
    checkWrapOutputStreamConsistentWithEncode(encoder, allAtOnce);
    // many calls to write(int)
    WriteStrategy byteWise = (bytes, out) -> {
        for (byte b : bytes) {
            out.write(b);
        }
    };
    checkWrapOutputStreamConsistentWithEncode(encoder, byteWise);
    // intermixed sequences of write(int) with
    // write(byte[],int,int) of various lengths.
    WriteStrategy mixed = (bytes, out) -> {
        int[] writeLengths = { -10, -5, -1, 0, 1, 1, 2, 2, 3, 10, 100 };
        int p = 0;
        while (p < bytes.length) {
            int l = writeLengths[random.nextInt(writeLengths.length)];
            l = Math.min(l, bytes.length - p);
            if (l >= 0) {
                out.write(bytes, p, l);
                p += l;
            } else {
                l = Math.min(-l, bytes.length - p);
                for (int i = 0; i < l; ++i) {
                    out.write(bytes[p + i]);
                }
                p += l;
            }
        }
    };
    checkWrapOutputStreamConsistentWithEncode(encoder, mixed);
}
Also used : OutputStream(java.io.OutputStream) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Encoder(java.util.Base64.Encoder) Set(java.util.Set) IOException(java.io.IOException) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) Base64(java.util.Base64) Decoder(java.util.Base64.Decoder) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) TestCase(junit.framework.TestCase) Collections(java.util.Collections) Arrays.copyOfRange(java.util.Arrays.copyOfRange) LinkedHashSet(java.util.LinkedHashSet) InputStream(java.io.InputStream) Random(java.util.Random)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)11 ByteBuf (io.netty.buffer.ByteBuf)11 Unpooled (io.netty.buffer.Unpooled)11 Channel (io.netty.channel.Channel)11 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)11 ChannelFuture (io.netty.channel.ChannelFuture)11 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 ChannelInitializer (io.netty.channel.ChannelInitializer)11 ChannelPipeline (io.netty.channel.ChannelPipeline)11 EventLoopGroup (io.netty.channel.EventLoopGroup)11 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)11 AbstractHttp2ConnectionHandlerBuilder (io.netty.handler.codec.http2.AbstractHttp2ConnectionHandlerBuilder)11 DefaultHttp2Connection (io.netty.handler.codec.http2.DefaultHttp2Connection)11 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)11 Http2Connection (io.netty.handler.codec.http2.Http2Connection)11 Http2ConnectionDecoder (io.netty.handler.codec.http2.Http2ConnectionDecoder)11 Http2ConnectionEncoder (io.netty.handler.codec.http2.Http2ConnectionEncoder)11 Http2ConnectionHandler (io.netty.handler.codec.http2.Http2ConnectionHandler)11