/// <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(); trans.Write(QUOTE); int len = b.Length; int off = 0; while (len >= 3) { // Encode 3 bytes at a time TBase64Utils.encode(b, off, 3, tempBuffer, 0); trans.Write(tempBuffer, 0, 4); off += 3; len -= 3; } if (len > 0) { // Encode remainder TBase64Utils.encode(b, off, len, tempBuffer, 0); trans.Write(tempBuffer, 0, len + 1); } trans.Write(QUOTE); }
/// <summary> /// Read in a JSON string containing base-64 encoded data and decode it. /// </summary> private byte[] ReadJSONBase64() { byte[] b = ReadJSONString(false); int len = b.Length; int off = 0; int 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?) byte[] 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(); trans.Write(QUOTE); int len = b.Length; int off = 0; // Ignore padding int bound = len >= 2 ? len - 2 : 0; for (int i = len - 1; i >= bound && b[i] == '='; --i) { --len; } while (len >= 3) { // Encode 3 bytes at a time TBase64Utils.encode(b, off, 3, tempBuffer, 0); trans.Write(tempBuffer, 0, 4); off += 3; len -= 3; } if (len > 0) { // Encode remainder TBase64Utils.encode(b, off, len, tempBuffer, 0); trans.Write(tempBuffer, 0, len + 1); } trans.Write(QUOTE); }
/// <summary> /// Write out contents of byte array b as a JSON string with base-64 encoded /// data /// </summary> private async Task WriteJsonBase64Async(byte[] bytes, CancellationToken cancellationToken) { await Context.WriteConditionalDelimiterAsync(cancellationToken); await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken); var len = bytes.Length; var off = 0; while (len >= 3) { // Encode 3 bytes at a time TBase64Utils.Encode(bytes, off, 3, _tempBuffer, 0); await Trans.WriteAsync(_tempBuffer, 0, 4, cancellationToken); off += 3; len -= 3; } if (len > 0) { // Encode remainder TBase64Utils.Encode(bytes, off, len, _tempBuffer, 0); await Trans.WriteAsync(_tempBuffer, 0, len + 1, cancellationToken); } await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken); }
private void WriteJSONBase64(byte[] b) { this.context.Write(); this.trans.Write(TJSONProtocol.QUOTE); int length = (int)b.Length; int num = 0; while (length >= 3) { TBase64Utils.encode(b, num, 3, this.tempBuffer, 0); this.trans.Write(this.tempBuffer, 0, 4); num = num + 3; length = length - 3; } if (length > 0) { TBase64Utils.encode(b, num, length, this.tempBuffer, 0); this.trans.Write(this.tempBuffer, 0, length + 1); } this.trans.Write(TJSONProtocol.QUOTE); }
/// <summary> /// Read in a JSON string containing base-64 encoded data and decode it. /// </summary> private async ValueTask <byte[]> ReadJsonBase64Async(CancellationToken cancellationToken) { var b = await ReadJsonStringAsync(false, cancellationToken); 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 exType) 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); }
private byte[] ReadJSONBase64() { byte[] numArray = this.ReadJSONString(false); int length = (int)numArray.Length; int num = 0; int num1 = 0; while (length >= 4) { TBase64Utils.decode(numArray, num, 4, numArray, num1); num = num + 4; length = length - 4; num1 = num1 + 3; } if (length > 1) { TBase64Utils.decode(numArray, num, length, numArray, num1); num1 = num1 + (length - 1); } byte[] numArray1 = new byte[num1]; Array.Copy(numArray, 0, numArray1, 0, num1); return(numArray1); }