/// <summary> /// Will quote given JSON String value using standard quoting, encode /// results as UTF-8, and return result as a byte array. /// </summary> public byte[] quoteAsUTF8(string text) { com.fasterxml.jackson.core.util.ByteArrayBuilder bb = _bytes; if (bb == null) { // no allocator; can add if we must, shouldn't need to _bytes = bb = new com.fasterxml.jackson.core.util.ByteArrayBuilder(null); } int inputPtr = 0; int inputEnd = text.Length; int outputPtr = 0; byte[] outputBuffer = bb.resetAndGetFirstSegment(); while (inputPtr < inputEnd) { int[] escCodes = com.fasterxml.jackson.core.io.CharTypes.get7BitOutputEscapes(); // ASCII and escapes while (true) { int ch = text[inputPtr]; if (ch > unchecked((int)(0x7F)) || escCodes[ch] != 0) { goto inner_loop_break; } if (outputPtr >= outputBuffer.Length) { outputBuffer = bb.finishCurrentSegment(); outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)ch); if (++inputPtr >= inputEnd) { goto main_break; } inner_loop_continue: ; } inner_loop_break: ; if (outputPtr >= outputBuffer.Length) { outputBuffer = bb.finishCurrentSegment(); outputPtr = 0; } // Ok, so what did we hit? int ch_1 = (int)text[inputPtr++]; if (ch_1 <= unchecked((int)(0x7F))) { // needs quoting int escape = escCodes[ch_1]; // ctrl-char, 6-byte escape... outputPtr = _appendByte(ch_1, escape, bb, outputPtr); outputBuffer = bb.getCurrentSegment(); goto main_continue; } if (ch_1 <= unchecked((int)(0x7FF))) { // fine, just needs 2 byte output outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0xc0)) | (ch_1 >> 6) )); ch_1 = (unchecked((int)(0x80)) | (ch_1 & unchecked((int)(0x3f)))); } else { // 3 or 4 bytes // Surrogates? if (ch_1 < SURR1_FIRST || ch_1 > SURR2_LAST) { // nope outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0xe0)) | (ch_1 >> 12 ))); if (outputPtr >= outputBuffer.Length) { outputBuffer = bb.finishCurrentSegment(); outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | ((ch_1 >> 6 ) & unchecked((int)(0x3f))))); ch_1 = (unchecked((int)(0x80)) | (ch_1 & unchecked((int)(0x3f)))); } else { // yes, surrogate pair if (ch_1 > SURR1_LAST) { // must be from first range _illegal(ch_1); } // and if so, followed by another from next range if (inputPtr >= inputEnd) { _illegal(ch_1); } ch_1 = _convert(ch_1, text[inputPtr++]); if (ch_1 > unchecked((int)(0x10FFFF))) { // illegal, as per RFC 4627 _illegal(ch_1); } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0xf0)) | (ch_1 >> 18 ))); if (outputPtr >= outputBuffer.Length) { outputBuffer = bb.finishCurrentSegment(); outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | ((ch_1 >> 12 ) & unchecked((int)(0x3f))))); if (outputPtr >= outputBuffer.Length) { outputBuffer = bb.finishCurrentSegment(); outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | ((ch_1 >> 6 ) & unchecked((int)(0x3f))))); ch_1 = (unchecked((int)(0x80)) | (ch_1 & unchecked((int)(0x3f)))); } } if (outputPtr >= outputBuffer.Length) { outputBuffer = bb.finishCurrentSegment(); outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)ch_1); main_continue: ; } main_break: ; return _bytes.completeAndCoalesce(outputPtr); }
/// <summary> /// Will encode given String as UTF-8 (without any quoting), return /// resulting byte array. /// </summary> public byte[] encodeAsUTF8(string text) { com.fasterxml.jackson.core.util.ByteArrayBuilder byteBuilder = _bytes; if (byteBuilder == null) { // no allocator; can add if we must, shouldn't need to _bytes = byteBuilder = new com.fasterxml.jackson.core.util.ByteArrayBuilder(null); } int inputPtr = 0; int inputEnd = text.Length; int outputPtr = 0; byte[] outputBuffer = byteBuilder.resetAndGetFirstSegment(); int outputEnd = outputBuffer.Length; while (inputPtr < inputEnd) { int c = text[inputPtr++]; // first tight loop for ascii while (c <= unchecked((int)(0x7F))) { if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.Length; outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)c); if (inputPtr >= inputEnd) { goto main_loop_break; } c = text[inputPtr++]; } // then multi-byte... if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.Length; outputPtr = 0; } if (c < unchecked((int)(0x800))) { // 2-byte outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0xc0)) | (c >> 6))); } else { // 3 or 4 bytes // Surrogates? if (c < SURR1_FIRST || c > SURR2_LAST) { // nope outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0xe0)) | (c >> 12))); if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.Length; outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | ((c >> 6) & unchecked((int)(0x3f))))); } else { // yes, surrogate pair if (c > SURR1_LAST) { // must be from first range _illegal(c); } // and if so, followed by another from next range if (inputPtr >= inputEnd) { _illegal(c); } c = _convert(c, text[inputPtr++]); if (c > unchecked((int)(0x10FFFF))) { // illegal, as per RFC 4627 _illegal(c); } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0xf0)) | (c >> 18))); if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.Length; outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | ((c >> 12) & unchecked((int)(0x3f))))); if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.Length; outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | ((c >> 6) & unchecked((int)(0x3f))))); } } if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.Length; outputPtr = 0; } outputBuffer[outputPtr++] = unchecked((byte)(unchecked((int)(0x80)) | (c & unchecked( (int)(0x3f))))); main_loop_continue: ; } main_loop_break: ; return _bytes.completeAndCoalesce(outputPtr); }
/// <summary> /// Convenience method for decoding contents of a Base64-encoded String, /// using this variant's settings. /// </summary> /// <param name="input"/> /// <since>2.2.3</since> /// <exception cref="System.ArgumentException">if input is not valid base64 encoded data /// </exception> public byte[] decode(string input) { com.fasterxml.jackson.core.util.ByteArrayBuilder b = new com.fasterxml.jackson.core.util.ByteArrayBuilder (); decode(input, b); return b.toByteArray(); }