Java Code Examples for io.netty.buffer.ByteBuf#arrayOffset()

The following examples show how to use io.netty.buffer.ByteBuf#arrayOffset() . 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: SpdyHeaderBlockJZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private void setInput(ByteBuf decompressed) {
    int len = decompressed.readableBytes();

    byte[] in;
    int offset;
    if (decompressed.hasArray()) {
        in = decompressed.array();
        offset = decompressed.arrayOffset() + decompressed.readerIndex();
    } else {
        in = new byte[len];
        decompressed.getBytes(decompressed.readerIndex(), in);
        offset = 0;
    }
    z.next_in = in;
    z.next_in_index = offset;
    z.avail_in = len;
}
 
Example 2
Source File: SpdyHeaderBlockJZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void setInput(ByteBuf decompressed) {
    int len = decompressed.readableBytes();

    byte[] in;
    int offset;
    if (decompressed.hasArray()) {
        in = decompressed.array();
        offset = decompressed.arrayOffset() + decompressed.readerIndex();
    } else {
        in = new byte[len];
        decompressed.getBytes(decompressed.readerIndex(), in);
        offset = 0;
    }
    z.next_in = in;
    z.next_in_index = offset;
    z.avail_in = len;
}
 
Example 3
Source File: FrameDecoder.java    From incubator-nemo with Apache License 2.0 5 votes vote down vote up
/**
 * Try to emit the body of the control frame.
 *
 * @param in  the {@link ByteBuf} from which to read data
 * @param out the list to which the body of the control frame is added
 * @return {@code true} if the control frame body was emitted, {@code false} otherwise
 * @throws InvalidProtocolBufferException when failed to parse
 */
private boolean onControlBodyAdded(final ByteBuf in, final List out)
  throws InvalidProtocolBufferException {
  assert (controlBodyBytesToRead > 0);
  assert (dataBodyBytesToRead == 0);
  assert (inputContext == null);

  assert (controlBodyBytesToRead <= Integer.MAX_VALUE);

  if (in.readableBytes() < controlBodyBytesToRead) {
    // cannot read body now
    return false;
  }

  final byte[] bytes;
  final int offset;
  if (in.hasArray()) {
    bytes = in.array();
    offset = in.arrayOffset() + in.readerIndex();
  } else {
    bytes = new byte[(int) controlBodyBytesToRead];
    in.getBytes(in.readerIndex(), bytes, 0, (int) controlBodyBytesToRead);
    offset = 0;
  }
  final ByteTransferContextSetupMessage controlMessage
    = ByteTransferContextSetupMessage.PARSER.parseFrom(bytes, offset, (int) controlBodyBytesToRead);

  out.add(controlMessage);
  in.skipBytes((int) controlBodyBytesToRead);
  controlBodyBytesToRead = 0;
  return true;
}
 
Example 4
Source File: CompressionCodecZLib.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PulsarByteBufAllocator.DEFAULT.heapBuffer(uncompressedLength, uncompressedLength);

    int len = encoded.readableBytes();

    byte[] array;
    int offset;
    if (encoded.hasArray()) {
        array = encoded.array();
        offset = encoded.arrayOffset() + encoded.readerIndex();
    } else {
        array = new byte[len];
        encoded.getBytes(encoded.readerIndex(), array);
        offset = 0;
    }

    int resultLength;
    Inflater inflater = this.inflater.get();
    inflater.reset();
    inflater.setInput(array, offset, len);

    try {
        resultLength = inflater.inflate(uncompressed.array(), uncompressed.arrayOffset(), uncompressedLength);
    } catch (DataFormatException e) {
        throw new IOException(e);
    }

    checkArgument(resultLength == uncompressedLength);

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}
 
Example 5
Source File: CompressionCodecZLib.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PulsarByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    Deflater deflater = this.deflater.get();
    deflater.reset();
    deflater.setInput(array, offset, length);
    while (!deflater.needsInput()) {
        deflate(deflater, compressed);
    }

    return compressed;
}
 
Example 6
Source File: Gzipper.java    From zuul with Apache License 2.0 5 votes vote down vote up
private void write(ByteBuf bb) throws IOException {
    byte[] bytes;
    int offset;
    final int length = bb.readableBytes();
    if (bb.hasArray()) {
        /* avoid memory copy if possible */
        bytes = bb.array();
        offset = bb.arrayOffset();
    } else {
        bytes = new byte[length];
        bb.getBytes(bb.readerIndex(), bytes);
        offset = 0;
    }
    gzos.write(bytes, offset, length);
}
 
Example 7
Source File: ProtobufDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    if (extensionRegistry == null) {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
        }
    } else {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build());
        }
    }
}
 
Example 8
Source File: ByteBufUtils.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public static byte[] getArray(ByteBuf buffer) {
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.writableBytes() == 0) {
        return buffer.array();
    }
    byte[] data = new byte[buffer.readableBytes()];
    buffer.getBytes(buffer.readerIndex(), data);
    return data;
}
 
Example 9
Source File: TByteBufTransport.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public int getBufferPosition() {
    final ByteBuf buf = this.buf;
    if (!buf.hasArray())  {
        return 0;
    } else {
        return buf.arrayOffset() + buf.readerIndex();
    }
}
 
Example 10
Source File: HttpPostBodyUtil.java    From dorado with Apache License 2.0 5 votes vote down vote up
/**
 * @param buffer buffer with a backing byte array
 */
SeekAheadOptimize(ByteBuf buffer) {
    if (!buffer.hasArray()) {
        throw new IllegalArgumentException("buffer hasn't backing byte array");
    }
    this.buffer = buffer;
    bytes = buffer.array();
    readerIndex = buffer.readerIndex();
    origPos = pos = buffer.arrayOffset() + readerIndex;
    limit = buffer.arrayOffset() + buffer.writerIndex();
}
 
Example 11
Source File: ByteBufUtils.java    From Distributed-KV with Apache License 2.0 5 votes vote down vote up
/**
 * ByteBuf内容生成字符串
 * @param buf
 * @return
 */
public static String buf2Str(ByteBuf buf) {
    String str;
    // 处理堆缓冲区
    if(buf.hasArray()) {
        str = new String(buf.array(), buf.arrayOffset() + buf.readerIndex(), buf.readableBytes());
    } else { 
    	// 处理直接缓冲区以及复合缓冲区
        byte[] bytes = new byte[buf.readableBytes()];
        buf.getBytes(buf.readerIndex(), bytes);
        str = new String(bytes, 0, buf.readableBytes());
    }
    return str;
}
 
Example 12
Source File: SpdyHeaderBlockZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private boolean compressInto(ByteBuf compressed) {
    byte[] out = compressed.array();
    int off = compressed.arrayOffset() + compressed.writerIndex();
    int toWrite = compressed.writableBytes();
    int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
    compressed.writerIndex(compressed.writerIndex() + numBytes);
    return numBytes == toWrite;
}
 
Example 13
Source File: ProtobufDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
        throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    if (extensionRegistry == null) {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
        }
    } else {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(
                    array, offset, length, extensionRegistry));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(
                    array, offset, length, extensionRegistry).build());
        }
    }
}
 
Example 14
Source File: HttpPostBodyUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * @param buffer buffer with a backing byte array
 */
SeekAheadOptimize(ByteBuf buffer) {
    if (!buffer.hasArray()) {
        throw new IllegalArgumentException("buffer hasn't backing byte array");
    }
    this.buffer = buffer;
    bytes = buffer.array();
    readerIndex = buffer.readerIndex();
    origPos = pos = buffer.arrayOffset() + readerIndex;
    limit = buffer.arrayOffset() + buffer.writerIndex();
}
 
Example 15
Source File: SimpleNonceManager.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
/**
 * Verify a previously unknown nonce and return the {@link NonceKey} representation for the nonce.
 * <p>
 * Later when a nonce is re-used we can match based on the String alone - the information embedded within the nonce will be
 * cached with it.
 * <p>
 * This stage of the verification simply extracts the prefix and the embedded timestamp and recreates a new hashed and
 * Base64 nonce based on the local secret - if the newly generated nonce matches the supplied one we accept it was created
 * by this nonce manager.
 * <p>
 * This verification does not validate that the timestamp is within a valid time period.
 *
 * @param nonce -
 * @return
 */
private Nonce verifyUnknownNonce(final String nonce, final int nonceCount) {
    byte[] complete;
    int offset;
    int length;
    try {
        ByteBuf decode = FlexBase64.decode(nonce);
        complete = decode.array();
        offset = decode.arrayOffset();
        length = decode.writerIndex() - offset;
    } catch (IOException e) {
        throw MESSAGES.invalidBase64Token(e);
    }

    int timeStampLength = complete[offset + 8];
    // A sanity check to try and verify the sizes we expect from the arrays are correct.
    if (hashLength > 0) {
        int expectedLength = 9 + timeStampLength + hashLength;
        if (length != expectedLength) {
            throw MESSAGES.invalidNonceReceived();
        } else if (timeStampLength + 1 >= length)
            throw MESSAGES.invalidNonceReceived();
    }

    byte[] prefix = new byte[8];
    System.arraycopy(complete, offset, prefix, 0, 8);
    byte[] timeStampBytes = new byte[timeStampLength];
    System.arraycopy(complete, offset + 9, timeStampBytes, 0, timeStampBytes.length);

    String expectedNonce = createNonce(prefix, timeStampBytes);

    if (expectedNonce.equals(nonce)) {
        try {
            long timeStamp = Long.parseLong(new String(timeStampBytes, StandardCharsets.UTF_8));

            return new Nonce(expectedNonce, timeStamp, nonceCount);
        } catch (NumberFormatException dropped) {
        }
    }

    return null;
}
 
Example 16
Source File: JdkZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf uncompressed, ByteBuf out) throws Exception {
    if (finished) {
        out.writeBytes(uncompressed);
        return;
    }

    int len = uncompressed.readableBytes();
    if (len == 0) {
        return;
    }

    int offset;
    byte[] inAry;
    if (uncompressed.hasArray()) {
        // if it is backed by an array we not need to to do a copy at all
        inAry = uncompressed.array();
        offset = uncompressed.arrayOffset() + uncompressed.readerIndex();
        // skip all bytes as we will consume all of them
        uncompressed.skipBytes(len);
    } else {
        inAry = new byte[len];
        uncompressed.readBytes(inAry);
        offset = 0;
    }

    if (writeHeader) {
        writeHeader = false;
        if (wrapper == ZlibWrapper.GZIP) {
            out.writeBytes(gzipHeader);
        }
    }

    if (wrapper == ZlibWrapper.GZIP) {
        crc.update(inAry, offset, len);
    }

    deflater.setInput(inAry, offset, len);
    while (!deflater.needsInput()) {
        deflate(out);
    }
}
 
Example 17
Source File: JdkZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf uncompressed, ByteBuf out) throws Exception {
    if (finished) {
        out.writeBytes(uncompressed);
        return;
    }

    int len = uncompressed.readableBytes();
    if (len == 0) {
        return;
    }

    int offset;
    byte[] inAry;
    if (uncompressed.hasArray()) {
        // if it is backed by an array we not need to to do a copy at all
        inAry = uncompressed.array();
        offset = uncompressed.arrayOffset() + uncompressed.readerIndex();
        // skip all bytes as we will consume all of them
        uncompressed.skipBytes(len);
    } else {
        inAry = new byte[len];
        uncompressed.readBytes(inAry);
        offset = 0;
    }

    if (writeHeader) {
        writeHeader = false;
        if (wrapper == ZlibWrapper.GZIP) {
            out.writeBytes(gzipHeader);
        }
    }

    if (wrapper == ZlibWrapper.GZIP) {
        crc.update(inAry, offset, len);
    }

    deflater.setInput(inAry, offset, len);
    while (!deflater.needsInput()) {
        deflate(out);
    }
}
 
Example 18
Source File: SpdyHeaderBlockJZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private ByteBuf encode(ByteBufAllocator alloc) {
    boolean release = true;
    ByteBuf out = null;
    try {
        int oldNextInIndex = z.next_in_index;
        int oldNextOutIndex = z.next_out_index;

        int maxOutputLength = (int) Math.ceil(z.next_in.length * 1.001) + 12;
        out = alloc.heapBuffer(maxOutputLength);
        z.next_out = out.array();
        z.next_out_index = out.arrayOffset() + out.writerIndex();
        z.avail_out = maxOutputLength;

        int resultCode;
        try {
            resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
        } finally {
            out.skipBytes(z.next_in_index - oldNextInIndex);
        }
        if (resultCode != JZlib.Z_OK) {
            throw new CompressionException("compression failure: " + resultCode);
        }

        int outputLength = z.next_out_index - oldNextOutIndex;
        if (outputLength > 0) {
            out.writerIndex(out.writerIndex() + outputLength);
        }
        release = false;
        return out;
    } finally {
        // 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;
        if (release && out != null) {
            out.release();
        }
    }
}
 
Example 19
Source File: SpdyHeaderBlockJZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private ByteBuf encode(ByteBufAllocator alloc) {
    boolean release = true;
    ByteBuf out = null;
    try {
        int oldNextInIndex = z.next_in_index;
        int oldNextOutIndex = z.next_out_index;

        int maxOutputLength = (int) Math.ceil(z.next_in.length * 1.001) + 12;
        out = alloc.heapBuffer(maxOutputLength);
        z.next_out = out.array();
        z.next_out_index = out.arrayOffset() + out.writerIndex();
        z.avail_out = maxOutputLength;

        int resultCode;
        try {
            resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
        } finally {
            out.skipBytes(z.next_in_index - oldNextInIndex);
        }
        if (resultCode != JZlib.Z_OK) {
            throw new CompressionException("compression failure: " + resultCode);
        }

        int outputLength = z.next_out_index - oldNextOutIndex;
        if (outputLength > 0) {
            out.writerIndex(out.writerIndex() + outputLength);
        }
        release = false;
        return out;
    } finally {
        // 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;
        if (release && out != null) {
            out.release();
        }
    }
}
 
Example 20
Source File: BasicAuthenticationMechanism.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.undertow.server.HttpHandler#handleRequest(io.undertow.server.HttpServerExchange)
 */
@Override
public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange, SecurityContext securityContext) {

    List<String> authHeaders = exchange.getRequestHeaders(AUTHORIZATION);
    if (authHeaders != null) {
        for (String current : authHeaders) {
            if (current.toLowerCase(Locale.ENGLISH).startsWith(LOWERCASE_BASIC_PREFIX)) {

                String base64Challenge = current.substring(PREFIX_LENGTH);
                String plainChallenge = null;
                try {
                    ByteBuf decode = FlexBase64.decode(base64Challenge);

                    Charset charset = this.charset;
                    if(!userAgentCharsets.isEmpty()) {
                        String ua = exchange.getRequestHeader(HttpHeaderNames.USER_AGENT);
                        if(ua != null) {
                            for (Map.Entry<Pattern, Charset> entry : userAgentCharsets.entrySet()) {
                                if(entry.getKey().matcher(ua).find()) {
                                    charset = entry.getValue();
                                    break;
                                }
                            }
                        }
                    }

                    plainChallenge = new String(decode.array(), decode.arrayOffset(), decode.writerIndex(), charset);
                    UndertowLogger.SECURITY_LOGGER.debugf("Found basic auth header %s (decoded using charset %s) in %s", plainChallenge, charset, exchange);
                } catch (IOException e) {
                    UndertowLogger.SECURITY_LOGGER.debugf(e, "Failed to decode basic auth header %s in %s", base64Challenge, exchange);
                }
                int colonPos;
                if (plainChallenge != null && (colonPos = plainChallenge.indexOf(COLON)) > -1) {
                    String userName = plainChallenge.substring(0, colonPos);
                    char[] password = plainChallenge.substring(colonPos + 1).toCharArray();

                    IdentityManager idm = getIdentityManager(securityContext);
                    PasswordCredential credential = new PasswordCredential(password);
                    try {
                        final AuthenticationMechanismOutcome result;
                        Account account = idm.verify(userName, credential);
                        if (account != null) {
                            securityContext.authenticationComplete(account, name, false);
                            result = AuthenticationMechanismOutcome.AUTHENTICATED;
                        } else {
                            securityContext.authenticationFailed(MESSAGES.authenticationFailed(userName), name);
                            result = AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
                        }
                        return result;
                    } finally {
                        clear(password);
                    }
                }

                // By this point we had a header we should have been able to verify but for some reason
                // it was not correctly structured.
                return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
            }
        }
    }

    // No suitable header has been found in this request,
    return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
}