Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1.     public class CustomVarInt32BinaryStorage : IBinaryStorage<CustomVar<int>>
  2.     {
  3.         public void WriteValue(IWriter writer, CustomVar<int> val, out int writtenBytes)
  4.         {
  5.             writtenBytes = 0;
  6.             var value = val.Value;
  7.  
  8.             if (value >= 0 && value <= sbyte.MaxValue)
  9.             {
  10.                 writer.WriteValue((byte)value);
  11.                 return;
  12.             }
  13.  
  14.             while (value != 0)
  15.             {
  16.                 var b = value & sbyte.MaxValue;
  17.                 value = value >> 7;
  18.  
  19.                 if (value > 0)
  20.                     b |= 128;
  21.  
  22.                 writer.WriteValue((byte)b);
  23.             }
  24.         }
  25.  
  26.         public CustomVar<int> ReadValue(IReader reader, out int bytesRead)
  27.         {
  28.             bytesRead = 0;
  29.             var result = 0;
  30.            
  31.             for (var offset = 0; offset < 32; offset += 7)
  32.             {
  33.                 var readByte = reader.ReadValue<byte>();
  34.                 var hasNext = (readByte & 128) == 128;
  35.  
  36.                 if (offset > 0)
  37.                     result += ((readByte & sbyte.MaxValue) << offset);
  38.                 else
  39.                     result += (readByte & sbyte.MaxValue);
  40.  
  41.                 if (result > short.MaxValue)
  42.                     result -= 65536;
  43.  
  44.                 if (!hasNext)
  45.                     return new CustomVar<int>(result);
  46.             }
  47.             throw new InternalBufferOverflowException();
  48.         }
  49.     }
  50.  
  51.             // astron
  52.             WriterCache<int>.SingleWriteCustom = (dst, value) // dst = byte*
  53.             {
  54.                 if (value >= 0 && value <= sbyte.MaxValue)
  55.                 {
  56.                     WriterCache<byte>.SingleWriteValue(dst, (byte)value);
  57.                     return;
  58.                 }
  59.  
  60.                 while (value != 0)
  61.                 {
  62.                     var b = value & sbyte.MaxValue;
  63.                     value = (int)((uint)value >> 7);
  64.  
  65.                     if (value > 0)
  66.                         b |= 128;
  67.  
  68.                     WriterCache<byte>.SingleWriteValue(dst, (byte)b);
  69.                     dst++;
  70.                 }
  71.             };
  72.  
  73.             ReaderCache<int>.SingleReadCustom = (b) => // b = byte*
  74.             {
  75.                 var result = 0;
  76.  
  77.                 for (var offset = 0; offset < 32; offset += 7, b++)
  78.                 {
  79.                     var readByte = ReaderCache<byte>.SingleReadValue(b);
  80.                     var hasNext = (readByte & 128) == 128;
  81.  
  82.                     if (offset > 0)
  83.                         result += (readByte & sbyte.MaxValue) << offset;
  84.                     else
  85.                         result += readByte & sbyte.MaxValue;
  86.  
  87.                     if (!hasNext)
  88.                         return result;
  89.                 }
  90.                 throw new ArgumentOutOfRangeException();
  91.             };
  92.  
  93.       // D2
  94.       public function writeVarInt(value:int) : void
  95.       {
  96.          var b:* = 0;
  97.          var ba:ByteArray = new ByteArray();
  98.          if(value >= 0 && value <= MASK_01111111)
  99.          {
  100.             ba.writeByte(value);
  101.             this._data.writeBytes(ba);
  102.             return;
  103.          }
  104.          var c:int = value;
  105.          for(var buffer:ByteArray = new ByteArray(); c != 0; )
  106.          {
  107.             buffer.writeByte(c & MASK_01111111);
  108.             buffer.position = buffer.length - 1;
  109.             b = int(buffer.readByte());
  110.             c = c >>> CHUNCK_BIT_SIZE;
  111.             if(c > 0)
  112.             {
  113.                b = b | MASK_10000000;
  114.             }
  115.             ba.writeByte(b);
  116.          }
  117.          this._data.writeBytes(ba);
  118.       }
  119.       public function readVarInt() : int
  120.       {
  121.          var b:int = 0;
  122.          var value:int = 0;
  123.          var offset:int = 0;
  124.          for(var hasNext:* = false; offset < INT_SIZE; )
  125.          {
  126.             b = this._data.readByte();
  127.             hasNext = (b & MASK_10000000) == MASK_10000000;
  128.             if(offset > 0)
  129.             {
  130.                value = value + ((b & MASK_01111111) << offset);
  131.             }
  132.             else
  133.             {
  134.                value = value + (b & MASK_01111111);
  135.             }
  136.             offset = offset + CHUNCK_BIT_SIZE;
  137.             if(!hasNext)
  138.             {
  139.                return value;
  140.             }
  141.          }
  142.          throw new Error("Too much data");
  143.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement