Android Byte Array Encode encode(byte[] input, int offset, int len, int flags)

Here you can find the source of encode(byte[] input, int offset, int len, int flags)

Description

Base64-encode the given data and return a newly allocated byte[] with the result.

License

Apache License

Parameter

Parameter Description
input the data to encode
offset the position within the input array at which to start
len the number of bytes of input to encode
flags controls certain features of the encoded output. Passing DEFAULT results in output that adheres to RFC 2045.

Declaration

public static byte[] encode(byte[] input, int offset, int len, int flags) 

Method Source Code

/*/* ww  w  .  j av a  2s .  c o m*/
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

import java.io.UnsupportedEncodingException;

public class Main{
    /**
     * Base64-encode the given data and return a newly allocated
     * byte[] with the result.
     *
     * @param input  the data to encode
     * @param flags  controls certain features of the encoded output.
     *               Passing {@code DEFAULT} results in output that
     *               adheres to RFC 2045.
     */
    public static byte[] encode(byte[] input, int flags) {
        return encode(input, 0, input.length, flags);
    }
    /**
     * Base64-encode the given data and return a newly allocated
     * byte[] with the result.
     *
     * @param input  the data to encode
     * @param offset the position within the input array at which to
     *               start
     * @param len    the number of bytes of input to encode
     * @param flags  controls certain features of the encoded output.
     *               Passing {@code DEFAULT} results in output that
     *               adheres to RFC 2045.
     */
    public static byte[] encode(byte[] input, int offset, int len, int flags) {
        Encoder encoder = new Encoder(flags, null);

        // Compute the exact length of the array we will produce.
        int output_len = len / 3 * 4;

        // Account for the tail of the data and the padding bytes, if any.
        if (encoder.do_padding) {
            if (len % 3 > 0) {
                output_len += 4;
            }
        } else {
            switch (len % 3) {
            case 0:
                break;
            case 1:
                output_len += 2;
                break;
            case 2:
                output_len += 3;
                break;
            }
        }

        // Account for the newlines, if any.
        if (encoder.do_newline && len > 0) {
            output_len += (((len - 1) / (3 * Encoder.LINE_GROUPS)) + 1)
                    * (encoder.do_cr ? 2 : 1);
        }

        encoder.output = new byte[output_len];
        encoder.process(input, offset, len, true);

        assert encoder.op == output_len;

        return encoder.output;
    }
}

Related

  1. encode(@Nonnull byte[] input)
  2. encode(@Nonnull byte[] input)
  3. encode(@Nonnull byte[] input)
  4. encode(byte[] input, int flags)
  5. encode(byte[] source)
  6. encode(byte[] source)
  7. encode(byte[] source)
  8. encode(byte[] source, int off, int len, byte[] alphabet, boolean doPadding)