[6785] | 1 | /**************************************************************** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
| 3 | * or more contributor license agreements. See the NOTICE file * |
---|
| 4 | * distributed with this work for additional information * |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file * |
---|
| 6 | * to you under the Apache License, Version 2.0 (the * |
---|
| 7 | * "License"); you may not use this file except in compliance * |
---|
| 8 | * with the License. You may obtain a copy of the License at * |
---|
| 9 | * * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
| 11 | * * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, * |
---|
| 13 | * software distributed under the License is distributed on an * |
---|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
| 15 | * KIND, either express or implied. See the License for the * |
---|
| 16 | * specific language governing permissions and limitations * |
---|
| 17 | * under the License. * |
---|
| 18 | ****************************************************************/ |
---|
| 19 | |
---|
| 20 | package org.apache.james.mime4j; |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | public class EncodeUtils { |
---|
| 24 | |
---|
| 25 | public static final int MASK = 0x3F; |
---|
| 26 | public static final int FIRST_MASK = MASK << 18; |
---|
| 27 | public static final int SECOND_MASK = MASK << 12; |
---|
| 28 | public static final int THIRD_MASK = MASK << 6; |
---|
| 29 | public static final int FORTH_MASK = MASK; |
---|
| 30 | |
---|
| 31 | public static final byte[] ENCODING = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
---|
| 32 | 'O', 'P' ,'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', |
---|
| 33 | 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
---|
| 34 | '4', '5', '6', '7', '8', '9', '+', '/'}; |
---|
| 35 | |
---|
| 36 | public static final void main(String[] args) throws Exception { |
---|
| 37 | byte[] bytes = {(byte) 0, (byte) 128, (byte) 0}; |
---|
| 38 | System.out.println(new String(toBase64(bytes))); |
---|
| 39 | System.out.println(new String(toBase64("Hello, World".getBytes()))); |
---|
| 40 | System.out.println(new String(toBase64("Monday".getBytes()))); |
---|
| 41 | System.out.println(new String(toBase64("M\u00F6nchengladbach\r\n".getBytes("ISO-8859-1")))); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | public static byte[] toBase64(byte[] in) { |
---|
| 45 | int inputLength = in.length; |
---|
| 46 | int outputLength = (int) Math.floor((4*inputLength) / 3f) + 3; |
---|
| 47 | outputLength = outputLength + 2 * (int) Math.floor(outputLength / 76f); |
---|
| 48 | byte[] results = new byte[outputLength]; |
---|
| 49 | int inputIndex = 0; |
---|
| 50 | int outputIndex = 0; |
---|
| 51 | while (inputLength - inputIndex > 2) { |
---|
| 52 | int one = (toInt(in[inputIndex++]) << 16); |
---|
| 53 | int two = (toInt(in[inputIndex++]) << 8); |
---|
| 54 | int three = toInt(in[inputIndex++]); |
---|
| 55 | int quantum = one | two | three; |
---|
| 56 | int index = (quantum & FIRST_MASK) >> 18; |
---|
| 57 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 58 | index = (quantum & SECOND_MASK) >> 12; |
---|
| 59 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 60 | index = (quantum & THIRD_MASK) >> 6; |
---|
| 61 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 62 | index = (quantum & FORTH_MASK); |
---|
| 63 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | switch (inputLength - inputIndex) { |
---|
| 67 | case 1: |
---|
| 68 | int quantum = in[inputIndex++] << 16; |
---|
| 69 | int index = (quantum & FIRST_MASK) >> 18; |
---|
| 70 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 71 | index = (quantum & SECOND_MASK) >> 12; |
---|
| 72 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 73 | outputIndex = setResult(results, outputIndex, (byte) '='); |
---|
| 74 | outputIndex = setResult(results, outputIndex, (byte) '='); |
---|
| 75 | break; |
---|
| 76 | |
---|
| 77 | case 2: |
---|
| 78 | quantum = (in[inputIndex++] << 16) + (in[inputIndex++] << 8); |
---|
| 79 | index = (quantum & FIRST_MASK) >> 18; |
---|
| 80 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 81 | index = (quantum & SECOND_MASK) >> 12; |
---|
| 82 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 83 | index = (quantum & THIRD_MASK) >> 6; |
---|
| 84 | outputIndex = setResult(results, outputIndex, ENCODING[index]); |
---|
| 85 | outputIndex = setResult(results, outputIndex, (byte) '='); |
---|
| 86 | break; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | return results; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | private static int toInt(byte b) { |
---|
| 93 | return 255 & b; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | private static int setResult(byte[] results, int outputIndex, byte value) { |
---|
| 98 | results[outputIndex++] = value; |
---|
| 99 | outputIndex = checkLineLength(results, outputIndex); |
---|
| 100 | return outputIndex; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | private static int checkLineLength(byte[] results, int outputIndex) { |
---|
| 105 | if (outputIndex == 76 || outputIndex > 76 && (outputIndex - 2*Math.floor(outputIndex/76f - 1)) % 76 == 0) { |
---|
| 106 | results[outputIndex++] = '\r'; |
---|
| 107 | results[outputIndex++] = '\n'; |
---|
| 108 | } |
---|
| 109 | return outputIndex; |
---|
| 110 | } |
---|
| 111 | } |
---|