Example usage for io.netty.util AsciiString array

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

Introduction

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

Prototype

public byte[] array() 

Source Link

Document

This gives direct access to the underlying storage 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;//from w w w. j a  va 2 s.c  o  m
    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:com.linecorp.armeria.server.http.tomcat.TomcatService.java

License:Apache License

private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;//  ww w  .  ja v  a 2 s  .c  o m
    }

    for (Entry<AsciiString, String> e : headers) {
        final AsciiString k = e.getKey();
        final String v = e.getValue();

        if (k.isEmpty() || k.byteAt(0) == ':') {
            continue;
        }

        final MessageBytes cValue = cHeaders.addValue(k.array(), k.arrayOffset(), k.length());
        final byte[] valueBytes = v.getBytes(StandardCharsets.US_ASCII);
        cValue.setBytes(valueBytes, 0, valueBytes.length);
    }
}

From source file:com.linecorp.armeria.server.http.tomcat.TomcatServiceInvocationHandler.java

License:Apache License

private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;/*from  ww w .  ja v a  2  s.c  o m*/
    }

    for (Iterator<Entry<CharSequence, CharSequence>> i = headers.iteratorCharSequence(); i.hasNext();) {
        final Entry<CharSequence, CharSequence> e = i.next();
        final CharSequence k = e.getKey();
        final CharSequence v = e.getValue();

        final MessageBytes cValue;
        if (k instanceof AsciiString) {
            final AsciiString ak = (AsciiString) k;
            cValue = cHeaders.addValue(ak.array(), ak.arrayOffset(), ak.length());
        } else {
            cValue = cHeaders.addValue(k.toString());
        }

        if (v instanceof AsciiString) {
            final AsciiString av = (AsciiString) v;
            cValue.setBytes(av.array(), av.arrayOffset(), av.length());
        } else {
            final byte[] valueBytes = v.toString().getBytes(StandardCharsets.US_ASCII);
            cValue.setBytes(valueBytes, 0, valueBytes.length);
        }
    }
}

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

License:Apache License

/**
 * Compare two {@link CharSequence} objects without leaking timing information.
 * <p>//  w  ww  .  j  a  v a2s.c om
 * The {@code int} return type is intentional and is designed to allow cascading of constant time operations:
 * <pre>
 *     String s1 = "foo";
 *     String s2 = "foo";
 *     String s3 = "foo";
 *     String s4 = "goo";
 *     boolean equals = (equalsConstantTime(s1, s2) & equalsConstantTime(s3, s4)) != 0;
 * </pre>
 * @param s1 the first value.
 * @param s2 the second value.
 * @return {@code 0} if not equal. {@code 1} if equal.
 */
static int equalsConstantTime(CharSequence s1, CharSequence s2) {
    if (s1 instanceof AsciiString && s2 instanceof AsciiString) {
        if (s1.length() != s2.length()) {
            return 0;
        }
        AsciiString s1Ascii = (AsciiString) s1;
        AsciiString s2Ascii = (AsciiString) s2;
        return PlatformDependent.equalsConstantTime(s1Ascii.array(), s1Ascii.arrayOffset(), s2Ascii.array(),
                s2Ascii.arrayOffset(), s1.length());
    }

    return ConstantTimeUtils.equalsConstantTime(s1, s2);
}

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  w  w.j a v a2 s  .c o m*/
    // Slow path - copy.
    return seq.toString().getBytes(UTF_8);
}

From source file:ratpack.session.store.internal.AsciiStringByteBufRedisCodec.java

License:Apache License

@Override
public ByteBuffer encodeKey(AsciiString key) {
    return ByteBuffer.wrap(key.array());
}