PageRenderTime 103ms CodeModel.GetById 50ms app.highlight 18ms RepoModel.GetById 32ms app.codeStats 0ms

/src/mpv5/utils/text/Base64Encoder.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 184 lines | 97 code | 29 blank | 58 comment | 18 complexity | ab857ee2614109bebd9250e0afa2ae11 MD5 | raw file
  1package mpv5.utils.text;
  2
  3/* BASE.java --
  4   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  5
  6This file is part of GNU Classpath.
  7
  8GNU Classpath is free software; you can redistribute it and/or modify
  9it under the terms of the GNU General Public License as published by
 10the Free Software Foundation; either version 2, or (at your option)
 11any later version.
 12
 13GNU Classpath is distributed in the hope that it will be useful, but
 14WITHOUT ANY WARRANTY; without even the implied warranty of
 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16General Public License for more details.
 17
 18You should have received a copy of the GNU General Public License
 19along with GNU Classpath; see the file COPYING.  If not, write to the
 20Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 2102111-1307 USA.
 22
 23Linking this library statically or dynamically with other modules is
 24making a combined work based on this library.  Thus, the terms and
 25conditions of the GNU General Public License cover the whole
 26combination.
 27
 28As a special exception, the copyright holders of this library give you
 29permission to link this library with independent modules to produce an
 30executable, regardless of the license terms of these independent
 31modules, and to copy and distribute the resulting executable under
 32terms of your choice, provided that you also meet, for each linked
 33independent module, the terms and conditions of the license of that
 34module.  An independent module is a module which is not derived from
 35or based on this library.  If you modify this library, you may extend
 36this exception to your version of the library, but you are not
 37obligated to do so.  If you do not wish to do so, delete this
 38exception statement from your version. */
 39
 40/**
 41 * Encodes and decodes text according to the BASE64 encoding.
 42 *
 43 * @author Chris Burdess (dog@gnu.org)
 44 */
 45public final class Base64Encoder {
 46    private static final byte[] src = {
 47        0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
 48        0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
 49        0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
 50        0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f
 51    };
 52    private static final byte[] dst;
 53
 54    static {
 55        dst = new byte[0x100];
 56
 57        for (int i = 0x0; i < 0xff; i++) {
 58            dst[i] = -1;
 59        }
 60
 61        for (int i = 0; i < src.length; i++) {
 62            dst[src[i]] = (byte) i;
 63        }
 64    }
 65
 66    private Base64Encoder() {}
 67
 68    /**
 69     * Encode the specified byte array using the BASE64 algorithm.
 70     *
 71     * @param bs the source byte array
 72     */
 73    public static byte[] encode(byte[] bs) {
 74        int    si = 0,
 75               ti = 0;                                      // source/target array indices
 76        byte[] bt = new byte[((bs.length + 2) * 4) / 3];    // target byte array
 77
 78        for (; si < bs.length; si += 3) {
 79            int buflen = bs.length - si;
 80
 81            if (buflen == 1) {
 82                byte b = bs[si];
 83                int  i = 0;
 84
 85                bt[ti++] = src[b >>> 2 & 0x3f];
 86                bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
 87            } else if (buflen == 2) {
 88                byte b1 = bs[si],
 89                     b2 = bs[si + 1];
 90                int  i  = 0;
 91
 92                bt[ti++] = src[b1 >>> 2 & 0x3f];
 93                bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
 94                bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
 95            } else {
 96                byte b1 = bs[si],
 97                     b2 = bs[si + 1],
 98                     b3 = bs[si + 2];
 99
100                bt[ti++] = src[b1 >>> 2 & 0x3f];
101                bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
102                bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
103                bt[ti++] = src[b3 & 0x3f];
104            }
105        }
106
107        /*
108         * while (ti < bt.length)
109         * {
110         *   bt[ti++] = 0x3d;
111         * }
112         */
113        return bt;
114    }
115
116    /**
117     * Decode the specified byte array using the BASE64 algorithm.
118     *
119     * @param bs the source byte array
120     */
121    public static byte[] decode(byte[] bs) {
122        int srclen = bs.length;
123
124        while ((srclen > 0) && (bs[srclen - 1] == 0x3d)) {
125            srclen--;    /* strip padding character */
126        }
127
128        byte[] buffer = new byte[srclen];
129        int    buflen = 0;
130        int    si     = 0;
131        int    len    = srclen - si;
132
133        while (len > 0) {
134            byte b0 = dst[bs[si++] & 0xff];
135            byte b2 = dst[bs[si++] & 0xff];
136
137            buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
138
139            if (len > 2) {
140                b0               = b2;
141                b2               = dst[bs[si++] & 0xff];
142                buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
143
144                if (len > 3) {
145                    b0               = b2;
146                    b2               = dst[bs[si++] & 0xff];
147                    buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
148                }
149            }
150
151            len = srclen - si;
152        }
153
154        byte[] bt = new byte[buflen];
155
156        System.arraycopy(buffer, 0, bt, 0, buflen);
157
158        return bt;
159    }
160
161    public static void main(String[] args) {
162        boolean decode = false;
163
164        for (int i = 0; i < args.length; i++) {
165            if (args[i].equals("-d")) {
166                decode = true;
167            } else {
168                try {
169                    byte[] in  = args[i].getBytes("US-ASCII");
170                    byte[] out = decode
171                                 ? decode(in)
172                                 : encode(in);
173
174                    System.out.println(args[i] + " = " + new String(out, "US-ASCII"));
175                } catch (java.io.UnsupportedEncodingException e) {
176                    e.printStackTrace(System.err);
177                }
178            }
179        }
180    }
181}
182
183
184//~ Formatted by Jindent --- http://www.jindent.com