From commits-return-1320-archive-asf-public=cust-asf.ponee.io@zipkin.apache.org Tue May 14 06:47:54 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 1CEA018065D for ; Tue, 14 May 2019 08:47:54 +0200 (CEST) Received: (qmail 81967 invoked by uid 500); 14 May 2019 06:47:53 -0000 Mailing-List: contact commits-help@zipkin.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zipkin.apache.org Delivered-To: mailing list commits@zipkin.apache.org Received: (qmail 81957 invoked by uid 99); 14 May 2019 06:47:53 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 May 2019 06:47:53 +0000 From: GitBox To: commits@zipkin.apache.org Subject: [GitHub] [incubator-zipkin] adriancole commented on a change in pull request #2589: Makes SpanBytesDecoder work on ByteBuffer Message-ID: <155781647338.12663.16867385136529466771.gitbox@gitbox.apache.org> Date: Tue, 14 May 2019 06:47:53 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit adriancole commented on a change in pull request #2589: Makes SpanBytesDecoder work on ByteBuffer URL: https://github.com/apache/incubator-zipkin/pull/2589#discussion_r283646667 ########## File path: zipkin/src/main/java/zipkin2/internal/ReadBuffer.java ########## @@ -0,0 +1,379 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package zipkin2.internal; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +import static zipkin2.internal.HexCodec.HEX_DIGITS; +import static zipkin2.internal.JsonCodec.UTF_8; + +/** Read operations do bounds checks, as typically more errors occur reading than writing. */ +public abstract class ReadBuffer extends InputStream { + /** Do not use the buffer passed here after, as it may be manipulated directly. */ + public static ReadBuffer wrapUnsafe(ByteBuffer buffer) { + if (buffer.hasArray()) { + int pos = buffer.position(); + return wrap(buffer.array(), buffer.arrayOffset() + pos); + } + return new ReadBuffer.Buff(buffer); + } + + public static ReadBuffer wrap(byte[] bytes, int pos) { + return new ReadBuffer.Array(bytes, pos); + } + + static final class Buff extends ReadBuffer { + final ByteBuffer buf; // visible for testing + + Buff(ByteBuffer buf) { + this.buf = buf; + } + + @Override final byte readByteUnsafe() { + return buf.get(); + } + + @Override final byte[] readBytes(int length) { + require(length); + byte[] copy = new byte[length]; + buf.get(copy); + return copy; + } + + @Override boolean tryReadAscii(char[] destination, int length) { + buf.mark(); + for (int i = 0; i < length; i++) { + byte b = buf.get(); + if ((b & 0x80) != 0) { + buf.reset(); + return false; // Not 7-bit ASCII character + } + destination[i] = (char) b; + } + return true; + } + + @Override final String doReadUtf8(int length) { + return new String(readBytes(length), UTF_8); + } + + @Override short readShort() { + require(2); + return buf.getShort(); + } + + @Override int readInt() { + require(4); + return buf.getInt(); + } + + @Override long readLong() { + require(8); + return buf.getLong(); Review comment: nope you are right ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services