001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with 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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.mail.util; 021 022 import java.io.ByteArrayOutputStream; 023 import java.io.IOException; 024 import java.io.OutputStream; 025 026 /** 027 * Encoder for RFC1891 xtext. 028 * 029 * xtext strings are defined as 030 * 031 * xtext = *( xchar / hexchar ) 032 * 033 * where 034 * 035 * xchar is any ASCII character in the range 33-126, EXCEPT 036 * the characters "+" and "=". 037 * 038 * hexchar is an ASCII "+" followed by two upper case 039 * hexadecimal digits. 040 * 041 * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $ 042 */ 043 public class XText 044 { 045 private static final Encoder encoder = new XTextEncoder(); 046 047 /** 048 * encode the input data producing an xtext encoded byte array. 049 * 050 * @return a byte array containing the xtext encoded data. 051 */ 052 public static byte[] encode( 053 byte[] data) 054 { 055 return encode(data, 0, data.length); 056 } 057 058 /** 059 * encode the input data producing an xtext encoded byte array. 060 * 061 * @return a byte array containing the xtext encoded data. 062 */ 063 public static byte[] encode( 064 byte[] data, 065 int off, 066 int length) 067 { 068 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 069 070 try 071 { 072 encoder.encode(data, off, length, bOut); 073 } 074 catch (IOException e) 075 { 076 throw new RuntimeException("exception encoding xtext string: " + e); 077 } 078 079 return bOut.toByteArray(); 080 } 081 082 /** 083 * xtext encode the byte data writing it to the given output stream. 084 * 085 * @return the number of bytes produced. 086 */ 087 public static int encode( 088 byte[] data, 089 OutputStream out) 090 throws IOException 091 { 092 return encoder.encode(data, 0, data.length, out); 093 } 094 095 /** 096 * extext encode the byte data writing it to the given output stream. 097 * 098 * @return the number of bytes produced. 099 */ 100 public static int encode( 101 byte[] data, 102 int off, 103 int length, 104 OutputStream out) 105 throws IOException 106 { 107 return encoder.encode(data, 0, data.length, out); 108 } 109 110 /** 111 * decode the xtext encoded input data. It is assumed the input data is valid. 112 * 113 * @return a byte array representing the decoded data. 114 */ 115 public static byte[] decode( 116 byte[] data) 117 { 118 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 119 120 try 121 { 122 encoder.decode(data, 0, data.length, bOut); 123 } 124 catch (IOException e) 125 { 126 throw new RuntimeException("exception decoding xtext string: " + e); 127 } 128 129 return bOut.toByteArray(); 130 } 131 132 /** 133 * decode the xtext encoded String data - whitespace will be ignored. 134 * 135 * @return a byte array representing the decoded data. 136 */ 137 public static byte[] decode( 138 String data) 139 { 140 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 141 142 try 143 { 144 encoder.decode(data, bOut); 145 } 146 catch (IOException e) 147 { 148 throw new RuntimeException("exception decoding xtext string: " + e); 149 } 150 151 return bOut.toByteArray(); 152 } 153 154 /** 155 * decode the xtext encoded String data writing it to the given output stream, 156 * whitespace characters will be ignored. 157 * 158 * @return the number of bytes produced. 159 */ 160 public static int decode( 161 String data, 162 OutputStream out) 163 throws IOException 164 { 165 return encoder.decode(data, out); 166 } 167 } 168