Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomVarInt32BinaryStorage : IBinaryStorage<CustomVar<int>>
- {
- public void WriteValue(IWriter writer, CustomVar<int> val, out int writtenBytes)
- {
- writtenBytes = 0;
- var value = val.Value;
- if (value >= 0 && value <= sbyte.MaxValue)
- {
- writer.WriteValue((byte)value);
- return;
- }
- while (value != 0)
- {
- var b = value & sbyte.MaxValue;
- value = value >> 7;
- if (value > 0)
- b |= 128;
- writer.WriteValue((byte)b);
- }
- }
- public CustomVar<int> ReadValue(IReader reader, out int bytesRead)
- {
- bytesRead = 0;
- var result = 0;
- for (var offset = 0; offset < 32; offset += 7)
- {
- var readByte = reader.ReadValue<byte>();
- var hasNext = (readByte & 128) == 128;
- if (offset > 0)
- result += ((readByte & sbyte.MaxValue) << offset);
- else
- result += (readByte & sbyte.MaxValue);
- if (result > short.MaxValue)
- result -= 65536;
- if (!hasNext)
- return new CustomVar<int>(result);
- }
- throw new InternalBufferOverflowException();
- }
- }
- // astron
- WriterCache<int>.SingleWriteCustom = (dst, value) // dst = byte*
- {
- if (value >= 0 && value <= sbyte.MaxValue)
- {
- WriterCache<byte>.SingleWriteValue(dst, (byte)value);
- return;
- }
- while (value != 0)
- {
- var b = value & sbyte.MaxValue;
- value = (int)((uint)value >> 7);
- if (value > 0)
- b |= 128;
- WriterCache<byte>.SingleWriteValue(dst, (byte)b);
- dst++;
- }
- };
- ReaderCache<int>.SingleReadCustom = (b) => // b = byte*
- {
- var result = 0;
- for (var offset = 0; offset < 32; offset += 7, b++)
- {
- var readByte = ReaderCache<byte>.SingleReadValue(b);
- var hasNext = (readByte & 128) == 128;
- if (offset > 0)
- result += (readByte & sbyte.MaxValue) << offset;
- else
- result += readByte & sbyte.MaxValue;
- if (!hasNext)
- return result;
- }
- throw new ArgumentOutOfRangeException();
- };
- // D2
- public function writeVarInt(value:int) : void
- {
- var b:* = 0;
- var ba:ByteArray = new ByteArray();
- if(value >= 0 && value <= MASK_01111111)
- {
- ba.writeByte(value);
- this._data.writeBytes(ba);
- return;
- }
- var c:int = value;
- for(var buffer:ByteArray = new ByteArray(); c != 0; )
- {
- buffer.writeByte(c & MASK_01111111);
- buffer.position = buffer.length - 1;
- b = int(buffer.readByte());
- c = c >>> CHUNCK_BIT_SIZE;
- if(c > 0)
- {
- b = b | MASK_10000000;
- }
- ba.writeByte(b);
- }
- this._data.writeBytes(ba);
- }
- public function readVarInt() : int
- {
- var b:int = 0;
- var value:int = 0;
- var offset:int = 0;
- for(var hasNext:* = false; offset < INT_SIZE; )
- {
- b = this._data.readByte();
- hasNext = (b & MASK_10000000) == MASK_10000000;
- if(offset > 0)
- {
- value = value + ((b & MASK_01111111) << offset);
- }
- else
- {
- value = value + (b & MASK_01111111);
- }
- offset = offset + CHUNCK_BIT_SIZE;
- if(!hasNext)
- {
- return value;
- }
- }
- throw new Error("Too much data");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement