1 |
/* |
/* |
2 |
* $Id$ |
* BASE64.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
42 |
* Encodes and decodes text according to the BASE64 encoding. |
* Encodes and decodes text according to the BASE64 encoding. |
43 |
* |
* |
44 |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
45 |
*/ |
*/ |
46 |
public final class BASE64 |
public final class BASE64 |
47 |
{ |
{ |
70 |
} |
} |
71 |
} |
} |
72 |
|
|
73 |
private BASE64 () |
private BASE64() |
74 |
{ |
{ |
75 |
} |
} |
76 |
|
|
79 |
* |
* |
80 |
* @param bs the source byte array |
* @param bs the source byte array |
81 |
*/ |
*/ |
82 |
public static byte[] encode (byte[] bs) |
public static byte[] encode(byte[] bs) |
83 |
{ |
{ |
84 |
int si = 0, ti = 0; // source/target array indices |
int si = 0, ti = 0; // source/target array indices |
85 |
byte[] bt = new byte[((bs.length + 2) * 4) / 3]; // target byte array |
byte[] bt = new byte[((bs.length + 2) * 4) / 3]; // target byte array |
154 |
len = srclen - si; |
len = srclen - si; |
155 |
} |
} |
156 |
byte[] bt = new byte[buflen]; |
byte[] bt = new byte[buflen]; |
157 |
System.arraycopy (buffer, 0, bt, 0, buflen); |
System.arraycopy(buffer, 0, bt, 0, buflen); |
158 |
return bt; |
return bt; |
159 |
} |
} |
160 |
|
|
161 |
public static void main (String[] args) |
public static void main(String[] args) |
162 |
{ |
{ |
163 |
boolean decode = false; |
boolean decode = false; |
164 |
for (int i = 0; i < args.length; i++) |
for (int i = 0; i < args.length; i++) |
165 |
{ |
{ |
166 |
if (args[i].equals ("-d")) |
if (args[i].equals("-d")) |
167 |
{ |
{ |
168 |
decode = true; |
decode = true; |
169 |
} |
} |
171 |
{ |
{ |
172 |
try |
try |
173 |
{ |
{ |
174 |
byte[] in = args[i].getBytes ("US-ASCII"); |
byte[] in = args[i].getBytes("US-ASCII"); |
175 |
byte[] out = decode ? decode (in) : encode (in); |
byte[] out = decode ? decode(in) : encode(in); |
176 |
System.out.println (args[i] + " = " + |
System.out.println(args[i] + " = " + |
177 |
new String (out, "US-ASCII")); |
new String(out, "US-ASCII")); |
178 |
} |
} |
179 |
catch (java.io.UnsupportedEncodingException e) |
catch (java.io.UnsupportedEncodingException e) |
180 |
{ |
{ |
181 |
e.printStackTrace (System.err); |
e.printStackTrace(System.err); |
182 |
} |
} |
183 |
} |
} |
184 |
} |
} |