001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.geronimo.util.encoders; 020 021 import java.io.ByteArrayOutputStream; 022 import java.io.IOException; 023 import java.io.OutputStream; 024 025 /** 026 * Convert binary data to and from UrlBase64 encoding. This is identical to 027 * Base64 encoding, except that the padding character is "." and the other 028 * non-alphanumeric characters are "-" and "_" instead of "+" and "/". 029 * <p> 030 * The purpose of UrlBase64 encoding is to provide a compact encoding of binary 031 * data that is safe for use as an URL parameter. Base64 encoding does not 032 * produce encoded values that are safe for use in URLs, since "/" can be 033 * interpreted as a path delimiter; "+" is the encoded form of a space; and 034 * "=" is used to separate a name from the corresponding value in an URL 035 * parameter. 036 */ 037 public class UrlBase64 038 { 039 private static final Encoder encoder = new UrlBase64Encoder(); 040 041 /** 042 * Encode the input data producing a URL safe base 64 encoded byte array. 043 * 044 * @return a byte array containing the URL safe base 64 encoded data. 045 */ 046 public static byte[] encode( 047 byte[] data) 048 { 049 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 050 051 try 052 { 053 encoder.encode(data, 0, data.length, bOut); 054 } 055 catch (IOException e) 056 { 057 throw new RuntimeException("exception encoding URL safe base64 string: " + e); 058 } 059 060 return bOut.toByteArray(); 061 } 062 063 /** 064 * Encode the byte data writing it to the given output stream. 065 * 066 * @return the number of bytes produced. 067 */ 068 public static int encode( 069 byte[] data, 070 OutputStream out) 071 throws IOException 072 { 073 return encoder.encode(data, 0, data.length, out); 074 } 075 076 /** 077 * Decode the URL safe base 64 encoded input data - white space will be ignored. 078 * 079 * @return a byte array representing the decoded data. 080 */ 081 public static byte[] decode( 082 byte[] data) 083 { 084 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 085 086 try 087 { 088 encoder.decode(data, 0, data.length, bOut); 089 } 090 catch (IOException e) 091 { 092 throw new RuntimeException("exception decoding URL safe base64 string: " + e); 093 } 094 095 return bOut.toByteArray(); 096 } 097 098 /** 099 * decode the URL safe base 64 encoded byte data writing it to the given output stream, 100 * whitespace characters will be ignored. 101 * 102 * @return the number of bytes produced. 103 */ 104 public static int decode( 105 byte[] data, 106 OutputStream out) 107 throws IOException 108 { 109 return encoder.decode(data, 0, data.length, out); 110 } 111 112 /** 113 * decode the URL safe base 64 encoded String data - whitespace will be ignored. 114 * 115 * @return a byte array representing the decoded data. 116 */ 117 public static byte[] decode( 118 String data) 119 { 120 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 121 122 try 123 { 124 encoder.decode(data, bOut); 125 } 126 catch (IOException e) 127 { 128 throw new RuntimeException("exception decoding URL safe base64 string: " + e); 129 } 130 131 return bOut.toByteArray(); 132 } 133 134 /** 135 * Decode the URL safe base 64 encoded String data writing it to the given output stream, 136 * whitespace characters will be ignored. 137 * 138 * @return the number of bytes produced. 139 */ 140 public static int decode( 141 String data, 142 OutputStream out) 143 throws IOException 144 { 145 return encoder.decode(data, out); 146 } 147 }