/// <summary> /// Read in a JSON string containing base-64 encoded data and decode it. /// </summary> private Byte[] ReadJSONBase64() { var b = ReadJSONString(false); var len = b.Length; var off = 0; var size = 0; // reduce len to ignore fill bytes while ((len > 0) && (b[len - 1] == '=')) { --len; } // read & decode full byte triplets = 4 source bytes while (len > 4) { // Decode 4 bytes at a time TBase64Utils.decode(b, off, 4, b, size); // NB: decoded in place off += 4; len -= 4; size += 3; } // Don't decode if we hit the end or got a single leftover byte (invalid // base64 but legal for skip of regular string type) if (len > 1) { // Decode remainder TBase64Utils.decode(b, off, len, b, size); // NB: decoded in place size += len - 1; } // Sadly we must copy the byte[] (any way around this?) var result = new Byte[size]; Array.Copy(b, 0, result, 0, size); return(result); }
/// <summary> /// Write out contents of byte array b as a JSON string with base-64 encoded /// data /// </summary> private void WriteJSONBase64(Byte[] b) { context.Write(); Transport.Write(QUOTE); var len = b.Length; var off = 0; while (len >= 3) { // Encode 3 bytes at a time TBase64Utils.encode(b, off, 3, tempBuffer, 0); Transport.Write(tempBuffer, 0, 4); off += 3; len -= 3; } if (len > 0) { // Encode remainder TBase64Utils.encode(b, off, len, tempBuffer, 0); Transport.Write(tempBuffer, 0, len + 1); } Transport.Write(QUOTE); }