zipkin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-zipkin] adriancole commented on a change in pull request #2589: Makes SpanBytesDecoder work on ByteBuffer
Date Tue, 14 May 2019 06:58:01 GMT
adriancole commented on a change in pull request #2589: Makes SpanBytesDecoder work on ByteBuffer
URL: https://github.com/apache/incubator-zipkin/pull/2589#discussion_r283649766
 
 

 ##########
 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();
+    }
+
+    @Override long readLongLe() {
+      require(8);
+      return (readByteUnsafe() & 0xffL)
+        | (readByteUnsafe() & 0xffL) << 8
+        | (readByteUnsafe() & 0xffL) << 16
+        | (readByteUnsafe() & 0xffL) << 24
+        | (readByteUnsafe() & 0xffL) << 32
+        | (readByteUnsafe() & 0xffL) << 40
+        | (readByteUnsafe() & 0xffL) << 48
+        | (readByteUnsafe() & 0xffL) << 56;
+    }
+
+    @Override public int pos() {
+      return buf.position();
+    }
+
+    @Override public int read(byte[] dst, int offset, int length) {
+      int toRead = checkReadArguments(dst, offset, length);
+      if (toRead == 0) return 0;
+      buf.get(dst, offset, toRead);
+      return toRead;
+    }
+
+    @Override public long skip(long maxCount) {
+      return skip(buf, maxCount);
+    }
+
+    @Override public int available() {
+      return buf.remaining();
+    }
+
+    static long skip(ByteBuffer buf, long maxCount) {
+      // avoid java.lang.NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;
+      // bytes.position(bytes.position() + count);
+      long i = 0;
+      for (; i < maxCount && buf.hasRemaining(); i++) {
+        buf.get();
+      }
+      return i;
+    }
+  }
+
+  static final class Array extends ReadBuffer {
+    final byte[] buf;
+    int pos; // visible for testing
+
+    Array(byte[] buf, int pos) {
+      this.buf = buf;
+      this.pos = pos;
+    }
+
+    @Override final byte readByteUnsafe() {
+      return buf[pos++];
+    }
+
+    @Override final byte[] readBytes(int length) {
+      require(length);
+      byte[] result = new byte[length];
+      System.arraycopy(buf, pos, result, 0, length);
+      pos += length;
+      return result;
+    }
+
+    @Override public int read(byte[] dst, int offset, int length) {
+      int toRead = checkReadArguments(dst, offset, length);
+      if (toRead == 0) return 0;
+      System.arraycopy(buf, pos, dst, 0, toRead);
+      pos += toRead;
+      return toRead;
+    }
+
+    @Override boolean tryReadAscii(char[] destination, int length) {
+      for (int i = 0; i < length; i++) {
+        byte b = buf[pos + i];
+        if ((b & 0x80) != 0) return false;  // Not 7-bit ASCII character
+        destination[i] = (char) b;
+      }
+      pos += length;
+      return true;
+    }
+
+    @Override final String doReadUtf8(int length) {
+      String result = new String(buf, pos, length, UTF_8);
 
 Review comment:
   ditto

----------------------------------------------------------------
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

Mime
View raw message