HyCodeYourTale
classpublicfinalPriority 3

VarInt

com.hypixel.hytale.math.data.VarInt

12

Methods

12

Public Methods

0

Fields

9

Constructors

Constructors

package-private
VarInt(byte[] bytes)
package-private
VarInt(int value, DataOutput out)

throws IOException

package-private
VarInt(int value, DataOutput out)

throws IOException

package-private
VarInt(int value)
package-private
VarInt(int value)
package-private
VarInt(DataInput in)

throws IOException

package-private
VarInt(DataInput in)

throws IOException

package-private
VarInt(byte[] bytes)
private
VarInt()

Methods

Public Methods (12)

publicstatic
int readSignedVarInt(DataInput in)

throws IOException

publicstatic
int readSignedVarInt(byte[] bytes)
publicstatic
long readSignedVarLong(DataInput in)

throws IOException

publicstatic
int readUnsignedVarInt(DataInput in)

throws IOException

publicstatic
int readUnsignedVarInt(byte[] bytes)
publicstatic
long readUnsignedVarLong(DataInput in)

throws IOException

publicstatic
void writeSignedVarInt(int value, DataOutput out)

throws IOException

publicstatic
byte[] writeSignedVarInt(int value)
publicstatic
void writeSignedVarLong(long value, DataOutput out)

throws IOException

publicstatic
void writeUnsignedVarInt(int value, DataOutput out)

throws IOException

publicstatic
byte[] writeUnsignedVarInt(int value)
publicstatic
void writeUnsignedVarLong(long value, DataOutput out)

throws IOException

Source Code

package com.hypixel.hytale.math.data;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import javax.annotation.Nonnull;

public final class VarInt {
   private VarInt() {
      throw new UnsupportedOperationException("Do not instantiate.");
   }

   public static void writeSignedVarLong(long value, @Nonnull DataOutput out) throws IOException {
      writeUnsignedVarLong(value << 1 ^ value >> 63, out);
   }

   public static void writeUnsignedVarLong(long value, @Nonnull DataOutput out) throws IOException {
      while ((value & -128L) != 0L) {
         out.writeByte((int)value & 127 | 128);
         value >>>= 7;
      }

      out.writeByte((int)value & 127);
   }

   public static void writeSignedVarInt(int value, @Nonnull DataOutput out) throws IOException {
      writeUnsignedVarInt(value << 1 ^ value >> 31, out);
   }

   public static void writeUnsignedVarInt(int value, @Nonnull DataOutput out) throws IOException {
      while ((long)(value & -128) != 0L) {
         out.writeByte(value & 127 | 128);
         value >>>= 7;
      }

      out.writeByte(value & 127);
   }

   public static byte[] writeSignedVarInt(int value) {
      return writeUnsignedVarInt(value << 1 ^ value >> 31);
   }

   public static byte[] writeUnsignedVarInt(int value) {
      byte[] byteArrayList = new byte[10];
      int i = 0;

      while ((long)(value & -128) != 0L) {
         byteArrayList[i++] = (byte)(value & 127 | 128);
         value >>>= 7;
      }

      byteArrayList[i] = (byte)(value & 127);

      byte[] out;
      for (out = new byte[i + 1]; i >= 0; i--) {
         out[i] = byteArrayList[i];
      }

      return out;
   }

   public static long readSignedVarLong(@Nonnull DataInput in) throws IOException {
      long raw = readUnsignedVarLong(in);
      long temp = (raw << 63 >> 63 ^ raw) >> 1;
      return temp ^ raw & -9223372036854775808L;
   }

   public static long readUnsignedVarLong(@Nonnull DataInput in) throws IOException {
      long value = 0L;
      int i = 0;

      long b;
      while (((b = (long)in.readByte()) & 128L) != 0L) {
         value |= (b & 127L) << i;
         i += 7;
         if (i > 63) {
            throw new IllegalArgumentException("Variable length quantity is too long");
         }
      }

      return value | b << i;
   }

   public static int readSignedVarInt(@Nonnull DataInput in) throws IOException {
      int raw = readUnsignedVarInt(in);
      int temp = (raw << 31 >> 31 ^ raw) >> 1;
      return temp ^ raw & -2147483648;
   }

   public static int readUnsignedVarInt(@Nonnull DataInput in) throws IOException {
      int value = 0;
      int i = 0;

      int b;
      while (((b = in.readByte()) & 128) != 0) {
         value |= (b & 127) << i;
         i += 7;
         if (i > 35) {
            throw new IllegalArgumentException("Variable length quantity is too long");
         }
      }

      return value | b << i;
   }

   public static int readSignedVarInt(@Nonnull byte[] bytes) {
      int raw = readUnsignedVarInt(bytes);
      int temp = (raw << 31 >> 31 ^ raw) >> 1;
      return temp ^ raw & -2147483648;
   }

   public static int readUnsignedVarInt(@Nonnull byte[] bytes) {
      int value = 0;
      int i = 0;
      byte rb = -128;

      for (byte b : bytes) {
         rb = b;
         if ((b & 128) == 0) {
            break;
         }

         value |= (b & 127) << i;
         i += 7;
         if (i > 35) {
            throw new IllegalArgumentException("Variable length quantity is too long");
         }
      }

      return value | rb << i;
   }
}