| Start/ | End/ | |||
| True | False | - | Line | Source |
| 1 | /* | |||
| 2 | * [ JIGA ] | |||
| 3 | * | |||
| 4 | * Copyright (c) 2004 Shiraz Kanga <skanga at findant.com> | |||
| 5 | * | |||
| 6 | * This code is distributed under the GNU Library General Public License | |||
| 7 | * | |||
| 8 | * This library is free software; you can redistribute it and/or modify it under | |||
| 9 | * the terms of the GNU Library General Public License as published by the Free | |||
| 10 | * Software Foundation; either version 2 of the License, or (at your option) any | |||
| 11 | * later version. | |||
| 12 | * | |||
| 13 | * This library is distributed in the hope that it will be useful, but WITHOUT | |||
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |||
| 15 | * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more | |||
| 16 | * details. | |||
| 17 | * | |||
| 18 | * You should have received a copy of the GNU Library General Public License | |||
| 19 | * along with this program; if not, write to the Free Software Foundation, Inc., | |||
| 20 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |||
| 21 | * | |||
| 22 | * | |||
| 23 | * [http://glenn.sanson.free.fr/jiga/] | |||
| 24 | */ | |||
| 25 | ||||
| 26 | package net.library.jiga; | |||
| 27 | ||||
| 28 | /** | |||
| 29 | * A simple BASE64 codec. | |||
| 30 | * It implements both normal and safe BASE64 alphabets. | |||
| 31 | * The only difference between these two versions is the use of symbols '-' and '_' | |||
| 32 | * in replacement of '+' and '/' (often used as special characters) | |||
| 33 | * | |||
| 34 | * @author Glenn Sanson | |||
| 35 | */ | |||
| 36 | public class Base64 { | |||
| 37 | /** Characters of the normal BASE64 alphabet */ | |||
| 38 | public final static char[] BASE64_ALPHABET = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; | |||
| 39 | /** Characters of the safe BASE64 alphabet */ | |||
| 40 | public final static char[] BASE64_SAFE_ALPHABET = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' }; | |||
| 41 | ||||
| 0 | 0 | - | 42 | private Base64() {} |
| 43 | ||||
| 44 | /** | |||
| 45 | * Converts an ISO-8859-1 <code>String</code> into its BASE64 (normal set) representation | |||
| 46 | * @param data The data to convert | |||
| 47 | * @return Its BASE64 interpretation | |||
| 48 | */ | |||
| 0 | 0 | - | 49 | public static String toBase64(String data) { |
| 0 | - | 50 | return toBase64(data, BASE64_ALPHABET); | |
| 51 | } | |||
| 52 | ||||
| 53 | /** | |||
| 54 | * Converts an ISO-8859-1 <code>String</code> into its BASE64 representation | |||
| 55 | * @param data The data to convert | |||
| 56 | * @param base64 The set to use | |||
| 57 | * @return Its BASE64 interpretation | |||
| 58 | */ | |||
| 0 | 0 | - | 59 | public static String toBase64(String data, char[] base64) { |
| 60 | String output = new String(); | |||
| 61 | ||||
| 62 | int[] input = null; | |||
| 0 | 0 | - | 63 | try { |
| 64 | byte[] temp = data.getBytes("ISO-8859-1"); | |||
| 65 | ||||
| 66 | input = new int[temp.length]; | |||
| 0 | 0 | - | 67 | for (int i = 0; i < input.length; i++) { |
| 0 | 0 | - | 68 | if (temp[i] < 0) { |
| 69 | input[i] = 128 - temp[i]; | |||
| 70 | } else { | |||
| 71 | input[i] = temp[i]; | |||
| 72 | } | |||
| 73 | } | |||
| 0 | - | 74 | } catch (Exception e) { | |
| 75 | System.err.println("Character set not recognized"); | |||
| 76 | } | |||
| 77 | ||||
| 78 | int current = 0; | |||
| 79 | ||||
| 0 | 0 | - | 80 | for (int i = 0; i < input.length; i++) { |
| 81 | ||||
| 82 | output += base64[current + (input[i] >> ((i % 3 + 1) * 2))]; | |||
| 83 | current = ((input[i] << ((3 - i % 3) * 2)) & 255) >> 2; | |||
| 84 | ||||
| 0 | 0 | - | 85 | if (i % 3 == 2) { |
| 86 | output += base64[current]; | |||
| 87 | current = 0; | |||
| 88 | } | |||
| 89 | } | |||
| 90 | ||||
| 0 | 0 | - | 91 | if (input.length % 3 != 0) { |
| 92 | ||||
| 93 | output += base64[current]; | |||
| 94 | ||||
| 0 | 0 | - | 95 | for (int i = 0; i < (3 - (input.length % 3)); i++) { |
| 96 | output += "="; | |||
| 97 | } | |||
| 98 | } | |||
| 99 | ||||
| 0 | - | 100 | return output; | |
| 101 | } | |||
| 102 | ||||
| 0 | 0 | - | 103 | public static String fromBase64(String data) { |
| 0 | - | 104 | return fromBase64(data, BASE64_ALPHABET); | |
| 105 | } | |||
| 106 | ||||
| 0 | 0 | - | 107 | public static String fromBase64(String data, char[] base64) { |
| 108 | ||||
| 109 | String texte = new String(); | |||
| 110 | ||||
| 111 | int newInt = 0; | |||
| 112 | int complet = 0; | |||
| 113 | ||||
| 0 | 0 | - | 114 | for (int i = 0; i < data.length(); i++) { |
| 115 | int value = getBase64Value(data.charAt(i), base64); | |||
| 116 | ||||
| 0 | 0 | - | 117 | if (value != -1) { |
| 0 | 0 | - | 118 | if (complet == 0) { |
| 119 | newInt += value << 2; | |||
| 120 | } | |||
| 0 | 0 | - | 121 | if (complet == 2) { |
| 122 | newInt += value; | |||
| 123 | texte += (char) newInt; | |||
| 124 | newInt = 0; | |||
| 125 | } | |||
| 0 | 0 | - | 126 | if (complet > 2) { |
| 127 | newInt += (value >> (complet - 2)) & 255; | |||
| 128 | texte += (char) newInt; | |||
| 129 | newInt = (value << (10 - complet)) & 255; | |||
| 130 | } | |||
| 131 | ||||
| 132 | complet += 6; | |||
| 133 | complet %= 8; | |||
| 134 | } | |||
| 135 | } | |||
| 136 | ||||
| 0 | 0 | - | 137 | if (newInt != 0) { |
| 138 | texte += (char) newInt; | |||
| 139 | } | |||
| 140 | ||||
| 0 | - | 141 | return texte; | |
| 142 | } | |||
| 143 | ||||
| 0 | 0 | - | 144 | private static int getBase64Value(char c, char[] base64) { |
| 0 | 0 | - | 145 | for (int i = 0; i < 64; i++) { |
| 0 | 0 | - | 146 | if (c == base64[i]) { |
| 0 | - | 147 | return i; | |
| 148 | } | |||
| 149 | } | |||
| 150 | ||||
| 0 | - | 151 | return -1; | |
| 152 | } | |||
| 153 | } | |||
| ***TER 0% (0/42) of SOURCE FILE Base64.java | ||||