Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Jackson JSON-processor.
   *
   * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
   *
   * Licensed under the License specified in file LICENSE, included with
   * the source code and binary code bundles.
   * You may not use this file except in compliance with the License.
   *
   * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.fasterxml.jackson.core;
 
Abstract base class used to define specific details of which variant of Base64 encoding/decoding is to be used. Although there is somewhat standard basic version (so-called "MIME Base64"), other variants exists, see Base64 Wikipedia entry for details.

Author(s):
Tatu Saloranta
 
 public final class Base64Variant
     implements java.io.Serializable
 {
     // We'll only serialize name
     private static final long serialVersionUID = 1L;

    
Placeholder used by "no padding" variant, to be used when a character value is needed.
 
     final static char PADDING_CHAR_NONE = '\0';

    
Marker used to denote ascii characters that do not correspond to a 6-bit value (in this variant), and is not used as a padding character.
 
     public final static int BASE64_VALUE_INVALID = -1;

    
Marker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).
 
     public final static int BASE64_VALUE_PADDING = -2;
 
     /*
     /**********************************************************
     /* Encoding/decoding tables
     /**********************************************************
      */

    
Decoding table used for base 64 decoding.
 
     private final transient int[] _asciiToBase64 = new int[128];

    
Encoding table used for base 64 decoding when output is done as characters.
 
     private final transient char[] _base64ToAsciiC = new char[64];

    
Alternative encoding table used for base 64 decoding when output is done as ascii bytes.
 
     private final transient byte[] _base64ToAsciiB = new byte[64];
 
     /*
     /**********************************************************
     /* Other configuration
     /**********************************************************
      */

    
Symbolic name of variant; used for diagnostics/debugging.

Note that this is the only non-transient field; used when reading back from serialized state

 
     protected final String _name;

    
Whether this variant uses padding or not.
 
     protected final transient boolean _usesPadding;

    
Characted used for padding, if any (PADDING_CHAR_NONE if not).
 
     protected final transient char _paddingChar;
    
    
Maximum number of encoded base64 characters to output during encoding before adding a linefeed, if line length is to be limited (java.lang.Integer.MAX_VALUE if not limited).

Note: for some output modes (when writing attributes) linefeeds may need to be avoided, and this value ignored.

    protected final transient int _maxLineLength;
    /*
    /**********************************************************
    /* Life-cycle
    /**********************************************************
     */
    public Base64Variant(String nameString base64Alphabetboolean usesPaddingchar paddingCharint maxLineLength)
    {
         = name;
         = usesPadding;
         = paddingChar;
         = maxLineLength;
        // Ok and then we need to create codec tables.
        // First the main encoding table:
        int alphaLen = base64Alphabet.length();
        if (alphaLen != 64) {
            throw new IllegalArgumentException("Base64Alphabet length must be exactly 64 (was "+alphaLen+")");
        }
        // And then secondary encoding table and decoding table:
        base64Alphabet.getChars(0, alphaLen, 0);
        Arrays.fill();
        for (int i = 0; i < alphaLen; ++i) {
            char alpha = [i];
            [i] = (bytealpha;
            [alpha] = i;
        }
        // Plus if we use padding, add that in too
        if (usesPadding) {
            [(intpaddingChar] = ;
        }
    }

    
"Copy constructor" that can be used when the base alphabet is identical to one used by another variant except for the maximum line length (and obviously, name).
    public Base64Variant(Base64Variant baseString nameint maxLineLength)
    {
        this(basenamebase._usesPaddingbase._paddingCharmaxLineLength);
    }

    
"Copy constructor" that can be used when the base alphabet is identical to one used by another variant, but other details (padding, maximum line length) differ
    public Base64Variant(Base64Variant baseString nameboolean usesPaddingchar paddingCharint maxLineLength)
    {
         = name;
        byte[] srcB = base._base64ToAsciiB;
        System.arraycopy(srcB, 0, this., 0, srcB.length);
        char[] srcC = base._base64ToAsciiC;
        System.arraycopy(srcC, 0, this., 0, srcC.length);
        int[] srcV = base._asciiToBase64;
        System.arraycopy(srcV, 0, this., 0, srcV.length);
         = usesPadding;
         = paddingChar;
         = maxLineLength;
    }
    /*
    /**********************************************************
    /* Serializable overrides
    /**********************************************************
     */

    
Method used to "demote" deserialized instances back to canonical ones
    protected Object readResolve() {
        return Base64Variants.valueOf();
    }
    
    /*
    /**********************************************************
    /* Public accessors
    /**********************************************************
     */
    public String getName() { return ; }
    public boolean usesPadding() { return ; }
    public boolean usesPaddingChar(char c) { return c == ; }
    public boolean usesPaddingChar(int ch) { return ch == (int; }
    public char getPaddingChar() { return ; }
    public byte getPaddingByte() { return (byte); }
    public int getMaxLineLength() { return ; }
    /*
    /**********************************************************
    /* Decoding support
    /**********************************************************
     */

    

Returns:
6-bit decoded value, if valid character;
    public int decodeBase64Char(char c)
    {
        int ch = (intc;
        return (ch <= 127) ? [ch] : ;
    }
    public int decodeBase64Char(int ch)
    {
        return (ch <= 127) ? [ch] : ;
    }
    public int decodeBase64Byte(byte b)
    {
        int ch = (intb;
        return (ch <= 127) ? [ch] : ;
    }
    /*
    /**********************************************************
    /* Encoding support
    /**********************************************************
     */
    public char encodeBase64BitsAsChar(int value)
    {
        /* Let's assume caller has done necessary checks; this
         * method must be fast and inlinable
         */
        return [value];
    }

    
Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 characters, stored in given result buffer.
    public int encodeBase64Chunk(int b24char[] bufferint ptr)
    {
        buffer[ptr++] = [(b24 >> 18) & 0x3F];
        buffer[ptr++] = [(b24 >> 12) & 0x3F];
        buffer[ptr++] = [(b24 >> 6) & 0x3F];
        buffer[ptr++] = [b24 & 0x3F];
        return ptr;
    }
    public void encodeBase64Chunk(StringBuilder sbint b24)
    {
        sb.append([(b24 >> 18) & 0x3F]);
        sb.append([(b24 >> 12) & 0x3F]);
        sb.append([(b24 >> 6) & 0x3F]);
        sb.append([b24 & 0x3F]);
    }

    
Method that outputs partial chunk (which only encodes one or two bytes of data). Data given is still aligned same as if it as full data; that is, missing data is at the "right end" (LSB) of int.

Parameters:
outputBytes Number of encoded bytes included (either 1 or 2)
    public int encodeBase64Partial(int bitsint outputByteschar[] bufferint outPtr)
    {
        buffer[outPtr++] = [(bits >> 18) & 0x3F];
        buffer[outPtr++] = [(bits >> 12) & 0x3F];
        if () {
            buffer[outPtr++] = (outputBytes == 2) ?
                [(bits >> 6) & 0x3F] : ;
            buffer[outPtr++] = ;
        } else {
            if (outputBytes == 2) {
                buffer[outPtr++] = [(bits >> 6) & 0x3F];
            }
        }
        return outPtr;
    }
    public void encodeBase64Partial(StringBuilder sbint bitsint outputBytes)
    {
        sb.append([(bits >> 18) & 0x3F]);
        sb.append([(bits >> 12) & 0x3F]);
        if () {
            sb.append((outputBytes == 2) ?
                      [(bits >> 6) & 0x3F] : );
            sb.append();
        } else {
            if (outputBytes == 2) {
                sb.append([(bits >> 6) & 0x3F]);
            }
        }
    }
    public byte encodeBase64BitsAsByte(int value)
    {
        // As with above, assuming it is 6-bit value
        return [value];
    }

    
Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 bytes (ascii), stored in given result buffer.
    public int encodeBase64Chunk(int b24byte[] bufferint ptr)
    {
        buffer[ptr++] = [(b24 >> 18) & 0x3F];
        buffer[ptr++] = [(b24 >> 12) & 0x3F];
        buffer[ptr++] = [(b24 >> 6) & 0x3F];
        buffer[ptr++] = [b24 & 0x3F];
        return ptr;
    }

    
Method that outputs partial chunk (which only encodes one or two bytes of data). Data given is still aligned same as if it as full data; that is, missing data is at the "right end" (LSB) of int.

Parameters:
outputBytes Number of encoded bytes included (either 1 or 2)
    public int encodeBase64Partial(int bitsint outputBytesbyte[] bufferint outPtr)
    {
        buffer[outPtr++] = [(bits >> 18) & 0x3F];
        buffer[outPtr++] = [(bits >> 12) & 0x3F];
        if () {
            byte pb = (byte;
            buffer[outPtr++] = (outputBytes == 2) ?
                [(bits >> 6) & 0x3F] : pb;
            buffer[outPtr++] = pb;
        } else {
            if (outputBytes == 2) {
                buffer[outPtr++] = [(bits >> 6) & 0x3F];
            }
        }
        return outPtr;
    }

    
Convenience method for converting given byte array as base64 encoded String using this variant's settings. Resulting value is "raw", that is, not enclosed in double-quotes.

Parameters:
input Byte array to encode
    public String encode(byte[] input)
    {
        return encode(inputfalse);
    }

    
Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes.

Parameters:
input Byte array to encode
addQuotes Whether to surround resulting value in double quotes or not
    public String encode(byte[] inputboolean addQuotes)
    {
        int inputEnd = input.length;
        StringBuilder sb;
        {
            // let's approximate... 33% overhead, ~= 3/8 (0.375)
            int outputLen = inputEnd + (inputEnd >> 2) + (inputEnd >> 3);
            sb = new StringBuilder(outputLen);
        }
        if (addQuotes) {
            sb.append('"');
        }
        int chunksBeforeLF = getMaxLineLength() >> 2;
        // Ok, first we loop through all full triplets of data:
        int inputPtr = 0;
        int safeInputEnd = inputEnd-3; // to get only full triplets
        while (inputPtr <= safeInputEnd) {
            // First, mash 3 bytes into lsb of 32-bit int
            int b24 = ((intinput[inputPtr++]) << 8;
            b24 |= ((intinput[inputPtr++]) & 0xFF;
            b24 = (b24 << 8) | (((intinput[inputPtr++]) & 0xFF);
            encodeBase64Chunk(sbb24);
            if (--chunksBeforeLF <= 0) {
                // note: must quote in JSON value, so not really useful...
                sb.append('\\');
                sb.append('n');
                chunksBeforeLF = 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?
            int b24 = ((intinput[inputPtr++]) << 16;
            if (inputLeft == 2) {
                b24 |= (((intinput[inputPtr++]) & 0xFF) << 8;
            }
            encodeBase64Partial(sbb24inputLeft);
        }
        if (addQuotes) {
            sb.append('"');
        }
        return sb.toString();
    }
    
    /*
    /**********************************************************
    /* other methods
    /**********************************************************
     */
    @Override
    public String toString() { return ; }
New to GrepCode? Check out our FAQ X