/* /********************************************************** /* Internal methods, low-level writing, base64 encoded /********************************************************** */ /// <exception cref="System.IO.IOException"/> /// <exception cref="com.fasterxml.jackson.core.JsonGenerationException"/> protected internal void _writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant , byte[] input, int inputPtr, int inputEnd) { // Encoding is by chunks of 3 input, 4 output chars, so: int safeInputEnd = inputEnd - 3; // Let's also reserve room for possible (and quoted) lf char each round int safeOutputEnd = _outputEnd - 6; int chunksBeforeLF = b64variant.getMaxLineLength() >> 2; // Ok, first we loop through all full triplets of data: while (inputPtr <= safeInputEnd) { if (_outputTail > safeOutputEnd) { // need to flush _flushBuffer(); } // First, mash 3 bytes into lsb of 32-bit int int b24 = ((int)input[inputPtr++]) << 8; b24 |= ((int)input[inputPtr++]) & unchecked((int)(0xFF)); b24 = (b24 << 8) | (((int)input[inputPtr++]) & unchecked((int)(0xFF))); _outputTail = b64variant.encodeBase64Chunk(b24, _outputBuffer, _outputTail); if (--chunksBeforeLF <= 0) { // note: must quote in JSON value _outputBuffer[_outputTail++] = (byte)('\\'); _outputBuffer[_outputTail++] = (byte)('n'); chunksBeforeLF = b64variant.getMaxLineLength() >> 2; } } // And then we may have 1 or 2 leftover bytes to encode int inputLeft = inputEnd - inputPtr; // 0, 1 or 2 if (inputLeft > 0) { // yes, but do we have room for output? if (_outputTail > safeOutputEnd) { // don't really need 6 bytes but... _flushBuffer(); } int b24 = ((int)input[inputPtr++]) << 16; if (inputLeft == 2) { b24 |= (((int)input[inputPtr++]) & unchecked((int)(0xFF))) << 8; } _outputTail = b64variant.encodeBase64Partial(b24, inputLeft, _outputBuffer, _outputTail ); } }
// write method when length is unknown /// <exception cref="System.IO.IOException"/> /// <exception cref="com.fasterxml.jackson.core.JsonGenerationException"/> protected internal int _writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant , Sharpen.InputStream data, byte[] readBuffer) { int inputPtr = 0; int inputEnd = 0; int lastFullOffset = -3; int bytesDone = 0; // Let's also reserve room for possible (and quoted) LF char each round int safeOutputEnd = _outputEnd - 6; int chunksBeforeLF = b64variant.getMaxLineLength() >> 2; // Ok, first we loop through all full triplets of data: while (true) { if (inputPtr > lastFullOffset) { // need to load more inputEnd = _readMore(data, readBuffer, inputPtr, inputEnd, readBuffer.Length); inputPtr = 0; if (inputEnd < 3) { // required to try to read to have at least 3 bytes break; } lastFullOffset = inputEnd - 3; } if (_outputTail > safeOutputEnd) { // need to flush _flushBuffer(); } // First, mash 3 bytes into lsb of 32-bit int int b24 = ((int)readBuffer[inputPtr++]) << 8; b24 |= ((int)readBuffer[inputPtr++]) & unchecked((int)(0xFF)); b24 = (b24 << 8) | (((int)readBuffer[inputPtr++]) & unchecked((int)(0xFF))); bytesDone += 3; _outputTail = b64variant.encodeBase64Chunk(b24, _outputBuffer, _outputTail); if (--chunksBeforeLF <= 0) { _outputBuffer[_outputTail++] = (byte)('\\'); _outputBuffer[_outputTail++] = (byte)('n'); chunksBeforeLF = b64variant.getMaxLineLength() >> 2; } } // And then we may have 1 or 2 leftover bytes to encode if (inputPtr < inputEnd) { // yes, but do we have room for output? if (_outputTail > safeOutputEnd) { // don't really need 6 bytes but... _flushBuffer(); } int b24 = ((int)readBuffer[inputPtr++]) << 16; int amount = 1; if (inputPtr < inputEnd) { b24 |= (((int)readBuffer[inputPtr]) & unchecked((int)(0xFF))) << 8; amount = 2; } bytesDone += amount; _outputTail = b64variant.encodeBase64Partial(b24, amount, _outputBuffer, _outputTail ); } return bytesDone; }
// write-method called when length is definitely known /// <exception cref="System.IO.IOException"/> /// <exception cref="com.fasterxml.jackson.core.JsonGenerationException"/> protected internal int _writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant , Sharpen.InputStream data, byte[] readBuffer, int bytesLeft) { int inputPtr = 0; int inputEnd = 0; int lastFullOffset = -3; // Let's also reserve room for possible (and quoted) lf char each round int safeOutputEnd = _outputEnd - 6; int chunksBeforeLF = b64variant.getMaxLineLength() >> 2; while (bytesLeft > 2) { // main loop for full triplets if (inputPtr > lastFullOffset) { inputEnd = _readMore(data, readBuffer, inputPtr, inputEnd, bytesLeft); inputPtr = 0; if (inputEnd < 3) { // required to try to read to have at least 3 bytes break; } lastFullOffset = inputEnd - 3; } if (_outputTail > safeOutputEnd) { // need to flush _flushBuffer(); } int b24 = ((int)readBuffer[inputPtr++]) << 8; b24 |= ((int)readBuffer[inputPtr++]) & unchecked((int)(0xFF)); b24 = (b24 << 8) | (((int)readBuffer[inputPtr++]) & unchecked((int)(0xFF))); bytesLeft -= 3; _outputTail = b64variant.encodeBase64Chunk(b24, _outputBuffer, _outputTail); if (--chunksBeforeLF <= 0) { _outputBuffer[_outputTail++] = '\\'; _outputBuffer[_outputTail++] = 'n'; chunksBeforeLF = b64variant.getMaxLineLength() >> 2; } } // And then we may have 1 or 2 leftover bytes to encode if (bytesLeft > 0) { inputEnd = _readMore(data, readBuffer, inputPtr, inputEnd, bytesLeft); inputPtr = 0; if (inputEnd > 0) { // yes, but do we have room for output? if (_outputTail > safeOutputEnd) { // don't really need 6 bytes but... _flushBuffer(); } int b24 = ((int)readBuffer[inputPtr++]) << 16; int amount; if (inputPtr < inputEnd) { b24 |= (((int)readBuffer[inputPtr]) & unchecked((int)(0xFF))) << 8; amount = 2; } else { amount = 1; } _outputTail = b64variant.encodeBase64Partial(b24, amount, _outputBuffer, _outputTail ); bytesLeft -= amount; } } return bytesLeft; }