Example usage for com.fasterxml.jackson.core Base64Variant encodeBase64Chunk

List of usage examples for com.fasterxml.jackson.core Base64Variant encodeBase64Chunk

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core Base64Variant encodeBase64Chunk.

Prototype

public int encodeBase64Chunk(int b24, byte[] buffer, int ptr) 

Source Link

Document

Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 bytes (ascii), stored in given result buffer.

Usage

From source file:com.bazaarvoice.jackson.rison.RisonGenerator.java

protected void _writeBinary(Base64Variant b64variant, byte[] input, int inputPtr, final int inputEnd)
        throws IOException, JsonGenerationException {
    // Encoding is by chunks of 3 input, 4 output chars, so:
    int safeInputEnd = inputEnd - 3;
    // Let's also reserve room for possible 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();//from  w w  w.j  av a2 s . com
        }
        // First, mash 3 bytes into lsb of 32-bit int
        int b24 = ((int) input[inputPtr++]) << 8;
        b24 |= ((int) input[inputPtr++]) & 0xFF;
        b24 = (b24 << 8) | (((int) input[inputPtr++]) & 0xFF);
        _outputTail = b64variant.encodeBase64Chunk(b24, _outputBuffer, _outputTail);
        if (--chunksBeforeLF <= 0) {
            // note: RISON does not escape newlines
            _outputBuffer[_outputTail++] = '\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]) & 0xFF) << 8;
        }
        _outputTail = b64variant.encodeBase64Partial(b24, inputLeft, _outputBuffer, _outputTail);
    }
}