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

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

Introduction

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

Prototype

public int encodeBase64Partial(int bits, int outputBytes, byte[] buffer, int outPtr) 

Source Link

Document

Method that outputs partial chunk (which only encodes one or two bytes of data).

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 ww  w  .jav  a2s .  c o  m
        }
        // 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);
    }
}