001: package org.bouncycastle.util.encoders;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.IOException;
005: import java.io.OutputStream;
006:
007: /**
008: * Convert binary data to and from UrlBase64 encoding. This is identical to
009: * Base64 encoding, except that the padding character is "." and the other
010: * non-alphanumeric characters are "-" and "_" instead of "+" and "/".
011: * <p>
012: * The purpose of UrlBase64 encoding is to provide a compact encoding of binary
013: * data that is safe for use as an URL parameter. Base64 encoding does not
014: * produce encoded values that are safe for use in URLs, since "/" can be
015: * interpreted as a path delimiter; "+" is the encoded form of a space; and
016: * "=" is used to separate a name from the corresponding value in an URL
017: * parameter.
018: */
019: public class UrlBase64 {
020: private static final Encoder encoder = new UrlBase64Encoder();
021:
022: /**
023: * Encode the input data producing a URL safe base 64 encoded byte array.
024: *
025: * @return a byte array containing the URL safe base 64 encoded data.
026: */
027: public static byte[] encode(byte[] data) {
028: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
029:
030: try {
031: encoder.encode(data, 0, data.length, bOut);
032: } catch (IOException e) {
033: throw new RuntimeException(
034: "exception encoding URL safe base64 string: " + e);
035: }
036:
037: return bOut.toByteArray();
038: }
039:
040: /**
041: * Encode the byte data writing it to the given output stream.
042: *
043: * @return the number of bytes produced.
044: */
045: public static int encode(byte[] data, OutputStream out)
046: throws IOException {
047: return encoder.encode(data, 0, data.length, out);
048: }
049:
050: /**
051: * Decode the URL safe base 64 encoded input data - white space will be ignored.
052: *
053: * @return a byte array representing the decoded data.
054: */
055: public static byte[] decode(byte[] data) {
056: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
057:
058: try {
059: encoder.decode(data, 0, data.length, bOut);
060: } catch (IOException e) {
061: throw new RuntimeException(
062: "exception decoding URL safe base64 string: " + e);
063: }
064:
065: return bOut.toByteArray();
066: }
067:
068: /**
069: * decode the URL safe base 64 encoded byte data writing it to the given output stream,
070: * whitespace characters will be ignored.
071: *
072: * @return the number of bytes produced.
073: */
074: public static int decode(byte[] data, OutputStream out)
075: throws IOException {
076: return encoder.decode(data, 0, data.length, out);
077: }
078:
079: /**
080: * decode the URL safe base 64 encoded String data - whitespace will be ignored.
081: *
082: * @return a byte array representing the decoded data.
083: */
084: public static byte[] decode(String data) {
085: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
086:
087: try {
088: encoder.decode(data, bOut);
089: } catch (IOException e) {
090: throw new RuntimeException(
091: "exception decoding URL safe base64 string: " + e);
092: }
093:
094: return bOut.toByteArray();
095: }
096:
097: /**
098: * Decode the URL safe base 64 encoded String data writing it to the given output stream,
099: * whitespace characters will be ignored.
100: *
101: * @return the number of bytes produced.
102: */
103: public static int decode(String data, OutputStream out)
104: throws IOException {
105: return encoder.decode(data, out);
106: }
107: }
|