protected DynamicContent(bool binary, int capacity) { if (binary) { bytebuf = BufferUtility.GetByteBuffer(capacity); } else { charbuf = BufferUtility.GetCharBuffer(capacity); } count = 0; }
public override string ToString() { JsonContent cnt = new JsonContent(false, 4096); try { cnt.PutFrom(this); return(cnt.ToString()); } finally { BufferUtility.Return(cnt); } }
public static string ToString <D>(D v, byte proj = 0x0f) where D : IData { JsonContent cnt = new JsonContent(false, 4 * 1024); try { cnt.Put(null, v, proj); return(cnt.ToString()); } finally { BufferUtility.Return(cnt); // return buffer to pool } }
void AddByte(byte b) { // ensure capacity int olen = bytebuf.Length; // old length if (count >= olen) { int nlen = olen * 4; // new length byte[] obuf = bytebuf; bytebuf = BufferUtility.GetByteBuffer(nlen); Array.Copy(obuf, 0, bytebuf, 0, olen); BufferUtility.Return(obuf); } bytebuf[count++] = b; // calculate checksum ulong cs = checksum; cs ^= b; // XOR checksum = cs >> 57 | cs << 7; // circular left shift 7 bit }
public void Add(char c) { if (IsBinary) // byte-oriented { // UTF-8 encoding but without surrogate support if (c < 0x80) { // have at most seven bits AddByte((byte)c); } else if (c < 0x800) { // 2 char, 11 bits AddByte((byte)(0xc0 | (c >> 6))); AddByte((byte)(0x80 | (c & 0x3f))); } else { // 3 char, 16 bits AddByte((byte)(0xe0 | ((c >> 12)))); AddByte((byte)(0x80 | ((c >> 6) & 0x3f))); AddByte((byte)(0x80 | (c & 0x3f))); } } else // char-oriented { // ensure capacity int olen = charbuf.Length; // old length if (count >= olen) { int nlen = olen * 4; // new length char[] obuf = charbuf; charbuf = BufferUtility.GetCharBuffer(nlen); Array.Copy(obuf, 0, charbuf, 0, olen); BufferUtility.Return(obuf); } charbuf[count++] = c; } }