Example usage for io.netty.util AsciiString toByteArray

List of usage examples for io.netty.util AsciiString toByteArray

Introduction

In this page you can find the example usage for io.netty.util AsciiString toByteArray.

Prototype

public byte[] toByteArray() 

Source Link

Document

Converts this string to a byte array.

Usage

From source file:com.linecorp.armeria.server.grpc.GrpcService.java

License:Apache License

private byte[][] convertHeadersToArray(HttpHeaders headers) {
    // The Netty AsciiString class is really just a wrapper around a byte[] and supports
    // arbitrary binary data, not just ASCII.
    byte[][] headerValues = new byte[headers.size() * 2][];
    int i = 0;//  ww w .j a v  a 2  s  .c  om
    for (Map.Entry<AsciiString, String> entry : headers) {
        AsciiString key = entry.getKey();
        headerValues[i++] = key.isEntireArrayUsed() ? key.array() : key.toByteArray();
        headerValues[i++] = entry.getValue().getBytes(StandardCharsets.US_ASCII);
    }
    return TransportFrameUtil.toRawSerializedHeaders(headerValues);
}

From source file:io.grpc.netty.Utils.java

License:Apache License

private static byte[] bytes(CharSequence seq) {
    if (seq instanceof AsciiString) {
        // Fast path - sometimes copy.
        AsciiString str = (AsciiString) seq;
        return str.isEntireArrayUsed() ? str.array() : str.toByteArray();
    }/*from   w  ww  .  j  av a 2  s.  com*/
    // Slow path - copy.
    return seq.toString().getBytes(UTF_8);
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpClientRequest.java

License:Apache License

public Mono<Void> done() {
    AsciiString boundary = generateMultipartBoundary();
    AsciiString delimiter = getDelimiter(boundary);
    AsciiString closeDelimiter = getCloseDelimiter(boundary);

    List<PartHttpClientRequest> parts = this.partConsumers.stream().map(partConsumer -> {
        PartHttpClientRequest part = new PartHttpClientRequest(this.objectMapper);
        partConsumer.accept(part);/*  w ww . j  a v a  2s. co m*/
        return part;
    }).collect(Collectors.toList());

    Long contentLength = parts.stream().mapToLong(part -> delimiter.length() + CRLF.length() + part.getLength())
            .sum() + closeDelimiter.length();

    NettyOutbound intermediateRequest = this.request.chunkedTransfer(false)
            .header(CONTENT_TYPE, BOUNDARY_PREAMBLE.concat(boundary))
            .header(CONTENT_LENGTH, String.valueOf(contentLength));

    for (PartHttpClientRequest part : parts) {
        intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(delimiter.toByteArray()));
        intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(CRLF.toByteArray()));
        intermediateRequest = intermediateRequest.sendObject(part.renderedHeaders);
        intermediateRequest = part.sendPayload(intermediateRequest);
    }

    intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(closeDelimiter.toByteArray()));

    return intermediateRequest.then();
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getCloseDelimiter(ByteBufAllocator allocator, AsciiString boundary) {
    AsciiString s = DOUBLE_DASH.concat(boundary).concat(DOUBLE_DASH);
    return allocator.directBuffer(s.length()).writeBytes(s.toByteArray());
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getDelimiter(ByteBufAllocator allocator, AsciiString boundary) {
    AsciiString s = DOUBLE_DASH.concat(boundary).concat(CRLF);
    return allocator.directBuffer(s.length()).writeBytes(s.toByteArray());
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getHeaders(ByteBufAllocator allocator, HttpHeaders headers) {
    AsciiString s = AsciiString.EMPTY_STRING;

    for (Map.Entry<String, String> entry : headers) {
        s = s.concat(new AsciiString(entry.getKey())).concat(HEADER_DELIMITER).concat(entry.getValue())
                .concat(CRLF);/*  w  w  w .  j  a  va2s .c o m*/
    }

    return allocator.directBuffer(s.length()).writeBytes(s.toByteArray());
}