public void TobyteArrayTest() { sbyte[] e = new sbyte[] { 55 }; BigInt a = new BigInt(55); Assert.IsTrue(ByteArrayUtil.AreEqual(a.ToByteArray(), e)); }
public override byte[] ToUtf8ByteArray() { if (buffer == null) { return(emptyBytes); } return(ByteArrayUtil.FastCloneWithResize(buffer, offset)); }
public override void WriteTrue() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 4); buffer[offset + 0] = (byte)'t'; buffer[offset + 1] = (byte)'r'; buffer[offset + 2] = (byte)'u'; buffer[offset + 3] = (byte)'e'; offset += 4; }
public override void WriteNull() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 4); buffer[offset + 0] = (byte)'n'; buffer[offset + 1] = (byte)'u'; buffer[offset + 2] = (byte)'l'; buffer[offset + 3] = (byte)'l'; offset += 4; }
public void AddString(byte[] str) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, str.Length); for (int i = 0; i < str.Length; i++) { buffer[offset + i] = str[i]; } offset += str.Length; }
public void AddSubstring(byte[] str, int start, int length) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, length); for (int i = 0; i < length; i++) { buffer[offset + i] = str[start + i]; } offset += length; }
public void AddPadding(byte c, int count) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, count); for (int i = 0; i < count; i++) { buffer[offset + i] = c; } offset += count; }
public override void WriteFalse() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 5); buffer[offset + 0] = (byte)'f'; buffer[offset + 1] = (byte)'a'; buffer[offset + 2] = (byte)'l'; buffer[offset + 3] = (byte)'s'; buffer[offset + 4] = (byte)'e'; offset += 5; }
public bool IsTranscoderEncode(byte[] bytes) { if (bytes == null || bytes.Length < 2) { return(false); } byte[] b = new byte[2]; Array.Copy(bytes, 0, b, 0, b.Length); if (ByteArrayUtil.bytesToShort(b) == MAGIC_NUMBER) { return(true); } return(false); }
static int FillFromStream(Stream input, ref byte[] buffer) { int length = 0; int read; while ((read = input.Read(buffer, length, buffer.Length - length)) > 0) { length += read; if (length == buffer.Length) { ByteArrayUtil.FastResize(ref buffer, length * 2); } } return(length); }
private int Window(BigInt x, int i, int[] nbs, int[] nzs, int w) { // TODO Auto-generated method stub int j, r; nbs[0] = 1; nzs[0] = 0; sbyte[] xa = x.ToByteArray(); //check for leading 0 bit if (!ByteArrayUtil.GetBitByDegree(i, xa)) { return(0); } //adjust window if not enough bits left if (i - w + 1 < 0) { w = i + 1; } r = 1; for (j = i - 1; j > i - w; j--) { nbs[0]++; r *= 2; if (ByteArrayUtil.GetBitByDegree(j, xa)) { r += 1; } if (r % 4 == 0) { r /= 4; nbs[0] -= 2; nzs[0] = 2; break; } } if (r % 2 == 0) { r /= 2; nzs[0] = 1; nbs[0]--; } return(r); }
private Point SimpleMultiply(Point p, BigInt k) { if (!(this.field.IsValidElement(p.X)) || !(this.field.IsValidElement(p.Y)) ) { throw new ArgumentException("The input point must be taken over the field."); } if (p.IsInfinity()) { return(p); } if (k.Equals(BigInt.ZERO)) { return(Point.INFINITY); } if (k.Equals(BigInt.ONE)) { return(p); } if (k.Signum() == -1) { k = k.Abs(); p = this.Negate(p); } sbyte[] ba = k.ToByteArray(); int degree = ByteArrayUtil.DegreeOf(ba) - 1; Point x = p; for (int i = degree; i >= 0; i--) { x = this.Dbl(x); if (ByteArrayUtil.GetBitByDegree(i, ba)) { x = this.Add(p, x); } } return(x); }
public void EncodeGamePackage(object messageBody) { try { // clear send buffer ResetSendBuffer(); // int messageId = 0; if (!m_MessageMapTypeToId.TryGetValue(messageBody.GetType(), out messageId)) { Debug.LogError("Can't encode message " + messageId); return; } // push message id Array.Copy(ByteArrayUtil.intToBytes(messageId), 0, m_SendBuffer, m_SendBufferSize, 4); m_SendBufferSize += 4; // push prefix m_SendBufferSize += 4; //push header (default length = 0,body = null) Array.Copy(ByteArrayUtil.shortToByteArray(0), 0, m_SendBuffer, m_SendBufferSize, 2); m_SendBufferSize += 2; // push message body length TBase Message = messageBody as TBase; byte[] tmpSendBody = ThriftSerialize.Serialize(Message); Array.Copy(ByteArrayUtil.intToBytes(tmpSendBody.Length), 0, m_SendBuffer, m_SendBufferSize, 4); m_SendBufferSize += 4; Array.Copy(tmpSendBody, 0, m_SendBuffer, m_SendBufferSize, tmpSendBody.Length); m_SendBufferSize += tmpSendBody.Length; Debug.Log("Send msg:" + messageBody.ToString()); } catch (Exception e) { Debug.LogError("Error on encode game package "); Debug.LogException(e); } }
public static T Deserialize <T>(Stream stream, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = JsonSerializer.DefaultResolver; } #if NETSTANDARD && !NET45 var ms = stream as MemoryStream; if (ms != null) { ArraySegment <byte> buf2; if (ms.TryGetBuffer(out buf2)) { // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf2.Array, buf2.Offset).GetCurrentJsonToken(); if (token == JsonToken.Number) { var buf3 = new byte[buf2.Count]; Buffer.BlockCopy(buf2.Array, buf2.Offset, buf3, 0, buf3.Length); return(Deserialize <T>(buf3, 0, resolver)); } return(Deserialize <T>(buf2.Array, buf2.Offset, resolver)); } } #endif { var buf = MemoryPool.GetBuffer(); var len = FillFromStream(stream, ref buf); // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf).GetCurrentJsonToken(); if (token == JsonToken.Number) { buf = ByteArrayUtil.FastCloneWithResize(buf, len); } return(Deserialize <T>(buf, resolver)); } }
public override void WriteBoolean(bool value) { if (value) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 4); buffer[offset + 0] = (byte)'t'; buffer[offset + 1] = (byte)'r'; buffer[offset + 2] = (byte)'u'; buffer[offset + 3] = (byte)'e'; offset += 4; } else { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 5); buffer[offset + 0] = (byte)'f'; buffer[offset + 1] = (byte)'a'; buffer[offset + 2] = (byte)'l'; buffer[offset + 3] = (byte)'s'; buffer[offset + 4] = (byte)'e'; offset += 5; } }
public static async System.Threading.Tasks.Task <T> DeserializeAsync <T>(Stream stream, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = JsonSerializer.DefaultResolver; } var buffer = BufferPool.Default.Rent(); var buf = buffer; try { int length = 0; int read; while ((read = await stream.ReadAsync(buf, length, buf.Length - length).ConfigureAwait(false)) > 0) { length += read; if (length == buf.Length) { ByteArrayUtil.FastResize(ref buf, length * 2); } } // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf).GetCurrentJsonToken(); if (token == JsonToken.Number) { buf = ByteArrayUtil.FastCloneWithResize(buf, length); } return(Deserialize <T>(buf, resolver)); } finally { BufferPool.Default.Return(buffer); } }
public Span <byte> GetBuffer(int offset, int appendLength) { ByteArrayUtil.EnsureCapacity(ref this.buffer, offset, appendLength); return(new Span <byte>(this.buffer, offset, appendLength)); }
public void AddStringSlow(string str) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, StringEncoding.UTF8.GetMaxByteCount(str.Length)); offset += StringEncoding.UTF8.GetBytes(str, 0, str.Length, buffer, offset); }
public override void WriteBeginArray() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1); buffer[offset++] = (byte)'['; }
public override void WriteEndObject() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1); buffer[offset++] = (byte)'}'; }
public override void WriteNameSeparator() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1); buffer[offset++] = (byte)':'; }
public void AddCharacter(byte str) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1); buffer[offset++] = str; }
public override void WriteQuotation() { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1); buffer[offset++] = (byte)'\"'; }
private int DecodeGamePackage(int index, int size) { m_bIsWaitingPkgComplete = false; int initIndex = index; if (IsOutOfSize(index, size, 4)) { m_bIsWaitingPkgComplete = true; return(initIndex); } //decode message id int messageId = ByteArrayUtil.bytesToInt(m_DecodingBuffer.ToArray(), index); index += 4; if (IsOutOfSize(index, size, 4)) { m_bIsWaitingPkgComplete = true; return(initIndex); } //skip header index += 4; if (IsOutOfSize(index, size, 2)) { m_bIsWaitingPkgComplete = true; return(initIndex); } short headerLength = ByteArrayUtil.bytesToShort(m_DecodingBuffer.ToArray(), index); index += 2; if (IsOutOfSize(index, size, headerLength)) { m_bIsWaitingPkgComplete = true; return(initIndex); } //skip header index += headerLength; if (IsOutOfSize(index, size, 4)) { m_bIsWaitingPkgComplete = true; return(initIndex); } //get message length int messageLength = ByteArrayUtil.bytesToInt(m_DecodingBuffer.ToArray(), index); index += 4; if (IsOutOfSize(index, size, messageLength)) { m_bIsWaitingPkgComplete = true; return(initIndex); } byte[] messageBody = new byte[messageLength]; Array.Copy(m_DecodingBuffer.ToArray(), index, messageBody, 0, messageLength); //update index index += messageLength; TBase message = null; Type tmpType; if (!m_MessageMapIdToType.TryGetValue(messageId, out tmpType)) { Debug.LogError("Can't decode message " + messageId); //return -1; return(index); } message = Activator.CreateInstance(tmpType) as TBase; ThriftSerialize.DeSerialize(message, messageBody); //broad cast MessageDispatcher.Instance.BroadcastMessage(new MessageObject(messageId, message)); Debug.Log("Rec msg:" + message.ToString()); return(index); }
public override void WriteString(string value) { if (value == null) { WriteNull(); return; } // single-path escape // nonescaped-ensure var startoffset = offset; var max = StringEncoding.UTF8.GetMaxByteCount(value.Length) + 2; ByteArrayUtil.EnsureCapacity(ref buffer, startoffset, max); var from = 0; //var to = value.Length; buffer[offset++] = (byte)'\"'; // for JIT Optimization, for-loop i < str.Length for (int i = 0; i < value.Length; i++) { byte escapeChar = default(byte); switch (value[i]) { case '"': escapeChar = (byte)'"'; break; case '\\': escapeChar = (byte)'\\'; break; case '\b': escapeChar = (byte)'b'; break; case '\f': escapeChar = (byte)'f'; break; case '\n': escapeChar = (byte)'n'; break; case '\r': escapeChar = (byte)'r'; break; case '\t': escapeChar = (byte)'t'; break; // use switch jumptable case (char)0: case (char)1: case (char)2: case (char)3: case (char)4: case (char)5: case (char)6: case (char)7: case (char)11: case (char)14: case (char)15: case (char)16: case (char)17: case (char)18: case (char)19: case (char)20: case (char)21: case (char)22: case (char)23: case (char)24: case (char)25: case (char)26: case (char)27: case (char)28: case (char)29: case (char)30: case (char)31: case (char)32: case (char)33: case (char)35: case (char)36: case (char)37: case (char)38: case (char)39: case (char)40: case (char)41: case (char)42: case (char)43: case (char)44: case (char)45: case (char)46: case (char)47: case (char)48: case (char)49: case (char)50: case (char)51: case (char)52: case (char)53: case (char)54: case (char)55: case (char)56: case (char)57: case (char)58: case (char)59: case (char)60: case (char)61: case (char)62: case (char)63: case (char)64: case (char)65: case (char)66: case (char)67: case (char)68: case (char)69: case (char)70: case (char)71: case (char)72: case (char)73: case (char)74: case (char)75: case (char)76: case (char)77: case (char)78: case (char)79: case (char)80: case (char)81: case (char)82: case (char)83: case (char)84: case (char)85: case (char)86: case (char)87: case (char)88: case (char)89: case (char)90: case (char)91: default: continue; } max += 2; ByteArrayUtil.EnsureCapacity(ref buffer, startoffset, max); // check +escape capacity offset += StringEncoding.UTF8.GetBytes(value, from, i - from, buffer, offset); from = i + 1; buffer[offset++] = (byte)'\\'; buffer[offset++] = escapeChar; } if (from != value.Length) { offset += StringEncoding.UTF8.GetBytes(value, from, value.Length - from, buffer, offset); } buffer[offset++] = (byte)'\"'; }
public void Parse_FormatInvalid_ShouldThrow(string str) { Assert.Throws <FormatException>(() => ByteArrayUtil.Parse(str)); }
public void Parse(string str) { var data = new byte[] { 0x01, 0xff }; CollectionAssert.AreEqual(data, ByteArrayUtil.Parse(str)); }
public override void EnsureCapacity(int offset, int appendLength) { ByteArrayUtil.EnsureCapacity(ref this.buffer, offset, appendLength); }
public override void WriteRaw(byte rawValue) { ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1); buffer[offset++] = rawValue; }
private void ReadStringSegmentCore(out byte[] resultBytes, out int resultOffset, out int resultLength) { // SkipWhiteSpace is already called from IsNull byte[] builder = null; var builderOffset = 0; char[] codePointStringBuffer = null; var codePointStringOffet = 0; if (this.bytes[this.offset] != '\"') { throw this.CreateParsingException("String Begin Token"); } this.offset++; var from = this.offset; // eliminate array-bound check for (int i = this.offset; i < this.bytes.Length; i++) { byte escapeCharacter = 0; switch (this.bytes[i]) { case (byte)'\\': // escape character switch ((char)this.bytes[i + 1]) { case '"': case '\\': case '/': escapeCharacter = this.bytes[i + 1]; goto COPY; case 'b': escapeCharacter = (byte)'\b'; goto COPY; case 'f': escapeCharacter = (byte)'\f'; goto COPY; case 'n': escapeCharacter = (byte)'\n'; goto COPY; case 'r': escapeCharacter = (byte)'\r'; goto COPY; case 't': escapeCharacter = (byte)'\t'; goto COPY; case 'u': if (codePointStringBuffer == null) { codePointStringBuffer = StringBuilderCache.GetCodePointStringBuffer(); } if (codePointStringOffet == 0) { if (builder == null) { builder = StringBuilderCache.GetBuffer(); } var copyCount = i - from; ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, copyCount + 1); // require + 1 Buffer.BlockCopy(this.bytes, from, builder, builderOffset, copyCount); builderOffset += copyCount; } if (codePointStringBuffer.Length == codePointStringOffet) { Array.Resize(ref codePointStringBuffer, codePointStringBuffer.Length * 2); } var a = (char)this.bytes[i + 2]; var b = (char)this.bytes[i + 3]; var c = (char)this.bytes[i + 4]; var d = (char)this.bytes[i + 5]; var codepoint = GetCodePoint(a, b, c, d); codePointStringBuffer[codePointStringOffet++] = (char)codepoint; i += 5; this.offset += 6; from = this.offset; continue; default: throw this.CreateParsingExceptionMessage("Bad JSON escape."); } case (byte)'"': // endtoken this.offset++; goto END; default: // string if (codePointStringOffet != 0) { if (builder == null) { builder = StringBuilderCache.GetBuffer(); } ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, StringEncoding.UTF8.GetMaxByteCount(codePointStringOffet)); builderOffset += StringEncoding.UTF8.GetBytes(codePointStringBuffer, 0, codePointStringOffet, builder, builderOffset); codePointStringOffet = 0; } this.offset++; continue; } COPY: { if (builder == null) { builder = StringBuilderCache.GetBuffer(); } if (codePointStringOffet != 0) { ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, StringEncoding.UTF8.GetMaxByteCount(codePointStringOffet)); builderOffset += StringEncoding.UTF8.GetBytes(codePointStringBuffer, 0, codePointStringOffet, builder, builderOffset); codePointStringOffet = 0; } var copyCount = i - from; ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, copyCount + 1); // require + 1! Buffer.BlockCopy(this.bytes, from, builder, builderOffset, copyCount); builderOffset += copyCount; builder[builderOffset++] = escapeCharacter; i += 1; this.offset += 2; from = this.offset; } } resultLength = 0; resultBytes = null; resultOffset = 0; throw this.CreateParsingException("String End Token"); END: if (builderOffset == 0 && codePointStringOffet == 0) // no escape { resultBytes = this.bytes; resultOffset = from; resultLength = this.offset - 1 - from; // skip last quote } else { if (builder == null) { builder = StringBuilderCache.GetBuffer(); } if (codePointStringOffet != 0) { ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, StringEncoding.UTF8.GetMaxByteCount(codePointStringOffet)); builderOffset += StringEncoding.UTF8.GetBytes(codePointStringBuffer, 0, codePointStringOffet, builder, builderOffset); codePointStringOffet = 0; } var copyCount = this.offset - from - 1; ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, copyCount); Buffer.BlockCopy(this.bytes, from, builder, builderOffset, copyCount); builderOffset += copyCount; resultBytes = builder; resultOffset = 0; resultLength = builderOffset; } }