ulysses-you commented on a change in pull request #1395:
URL: https://github.com/apache/incubator-kyuubi/pull/1395#discussion_r750816636



##########
File path: 
kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLRichByteBuf.scala
##########
@@ -0,0 +1,328 @@
+/*
+ * 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 org.apache.kyuubi.server.mysql
+
+import java.nio.charset.StandardCharsets
+
+import io.netty.buffer.ByteBuf
+
+// https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol
+object MySQLRichByteBuf {
+
+  private def charset = StandardCharsets.UTF_8
+
+  implicit class Implicit(self: ByteBuf) {
+
+    /**
+     * Read 1 byte fixed length integer from byte buffers.
+     *
+     * @return 1 byte fixed length integer
+     */
+    def readInt1: Int = self.readUnsignedByte
+
+    /**
+     * Write 1 byte fixed length integer to byte buffers.
+     *
+     * @param value 1 byte fixed length integer
+     */
+    def writeInt1(value: Int): ByteBuf = self.writeByte(value)
+
+    /**
+     * Read 2 byte fixed length integer from byte buffers.
+     *
+     * @return 2 byte fixed length integer
+     */
+    def readInt2: Int = self.readUnsignedShortLE
+
+    /**
+     * Write 2 byte fixed length integer to byte buffers.
+     *
+     * @param value 2 byte fixed length integer
+     */
+    def writeInt2(value: Int): ByteBuf = self.writeShortLE(value)
+
+    /**
+     * Read 3 byte fixed length integer from byte buffers.
+     *
+     * @return 3 byte fixed length integer
+     */
+    def readInt3: Int = self.readUnsignedMediumLE
+
+    /**
+     * Write 3 byte fixed length integer to byte buffers.
+     *
+     * @param value 3 byte fixed length integer
+     */
+    def writeInt3(value: Int): ByteBuf = self.writeMediumLE(value)
+
+    /**
+     * Read 4 byte fixed length integer from byte buffers.
+     *
+     * @return 4 byte fixed length integer
+     */
+    def readInt4: Int = self.readIntLE
+
+    /**
+     * Write 4 byte fixed length integer to byte buffers.
+     *
+     * @param value 4 byte fixed length integer
+     */
+    def writeInt4(value: Int): ByteBuf = self.writeIntLE(value)
+
+    /**
+     * Read 6 byte fixed length integer from byte buffers.
+     *
+     * @return 6 byte fixed length integer
+     */
+    def readInt6: Long = {
+      var result = 0L
+      var i = 0
+      while (i < 6) {
+        result |= (0xff & self.readByte).toLong << (8 * i)
+        i = i + 1
+      }
+      result
+    }
+
+    /**
+     * Write 6 byte fixed length integer to byte buffers.
+     *
+     * @param value 6 byte fixed length integer
+     */
+    def writeInt6(value: Long): ByteBuf = throw new 
UnsupportedOperationException
+
+    /**
+     * Read 8 byte fixed length integer from byte buffers.
+     *
+     * @return 8 byte fixed length integer
+     */
+    def readInt8: Long = self.readLongLE
+
+    /**
+     * Write 8 byte fixed length integer to byte buffers.
+     *
+     * @param value 8 byte fixed length integer
+     */
+    def writeInt8(value: Long): ByteBuf = self.writeLongLE(value)
+
+    /**
+     * Read lenenc integer from byte buffers.
+     *
+     * @return lenenc integer
+     */
+    def readIntLenenc: Long = {
+      val firstByte = readInt1
+      if (firstByte < 0xfb) return firstByte
+      if (0xfb == firstByte) return 0
+      if (0xfc == firstByte) return readInt2
+      if (0xfd == firstByte) return readInt3
+      self.readLongLE
+    }
+
+    /**
+     * Write lenenc integer to byte buffers.
+     *
+     * @param value lenenc integer
+     */
+    def writeIntLenenc(value: Long): ByteBuf = {
+      if (value < 0xfb) {
+        self.writeByte(value.toInt)
+      } else if (value < (1 << 16)) {
+        self.writeByte(0xfc)
+        self.writeShortLE(value.toInt)
+      } else if (value < (1 << 24)) {
+        self.writeByte(0xfd)
+        self.writeMediumLE(value.toInt)
+      } else {
+        self.writeByte(0xfe)
+        self.writeLongLE(value)
+      }
+    }
+
+    /**
+     * Read fixed length long from byte buffers.
+     *
+     * @param length length read from byte buffers
+     * @return fixed length long
+     */
+    def readLong(length: Int): Long = {
+      var result = 0

Review comment:
       0L




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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to