1 8 package org.apache.avalon.excalibur.source; 9 10 import org.xml.sax.InputSource ; 11 12 import java.io.ByteArrayOutputStream ; 13 import java.io.IOException ; 14 import java.io.OutputStreamWriter ; 15 import java.util.BitSet ; 16 17 18 25 public final class SourceUtil 26 { 27 28 private static final char [ ] alphabet = { 29 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; 38 42 public static String encodeBASE64 ( String s ) 43 { 44 return encodeBASE64 ( s.getBytes ( ) ); 45 } 46 47 51 public static String encodeBASE64 ( byte [ ] octetString ) 52 { 53 int bits24; 54 int bits6; 55 56 char [ ] out = new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ]; 57 58 int outIndex = 0; 59 int i = 0; 60 61 while ( ( i + 3 ) <= octetString.length ) 62 { 63 bits24 = ( octetString [ i++ ] & 0xFF ) << 16; 65 bits24 |= ( octetString [ i++ ] & 0xFF ) << 8; 66 bits24 |= ( octetString [ i++ ] & 0xFF ) << 0; 67 68 bits6 = ( bits24 & 0x00FC0000 ) >> 18; 69 out [ outIndex++ ] = alphabet [ bits6 ]; 70 bits6 = ( bits24 & 0x0003F000 ) >> 12; 71 out [ outIndex++ ] = alphabet [ bits6 ]; 72 bits6 = ( bits24 & 0x00000FC0 ) >> 6; 73 out [ outIndex++ ] = alphabet [ bits6 ]; 74 bits6 = ( bits24 & 0x0000003F ); 75 out [ outIndex++ ] = alphabet [ bits6 ]; 76 } 77 78 if ( octetString.length - i == 2 ) 79 { 80 bits24 = ( octetString [ i ] & 0xFF ) << 16; 82 bits24 |= ( octetString [ i + 1 ] & 0xFF ) << 8; 83 84 bits6 = ( bits24 & 0x00FC0000 ) >> 18; 85 out [ outIndex++ ] = alphabet [ bits6 ]; 86 bits6 = ( bits24 & 0x0003F000 ) >> 12; 87 out [ outIndex++ ] = alphabet [ bits6 ]; 88 bits6 = ( bits24 & 0x00000FC0 ) >> 6; 89 out [ outIndex++ ] = alphabet [ bits6 ]; 90 91 out [ outIndex++ ] = '='; 93 } 94 else if ( octetString.length - i == 1 ) 95 { 96 bits24 = ( octetString [ i ] & 0xFF ) << 16; 98 99 bits6 = ( bits24 & 0x00FC0000 ) >> 18; 100 out [ outIndex++ ] = alphabet [ bits6 ]; 101 bits6 = ( bits24 & 0x0003F000 ) >> 12; 102 out [ outIndex++ ] = alphabet [ bits6 ]; 103 104 out [ outIndex++ ] = '='; 106 out [ outIndex++ ] = '='; 107 } 108 109 return new String ( out ); 110 } 111 112 113 static BitSet charactersDontNeedingEncoding; 114 static final int characterCaseDiff = ('a' - 'A'); 115 116 117 static 118 { 119 charactersDontNeedingEncoding = new BitSet (256); 120 int i; 121 for (i = 'a'; i <= 'z'; i++) 122 { 123 charactersDontNeedingEncoding.set(i); 124 } 125 for (i = 'A'; i <= 'Z'; i++) 126 { 127 charactersDontNeedingEncoding.set(i); 128 } 129 for (i = '0'; i <= '9'; i++) 130 { 131 charactersDontNeedingEncoding.set(i); 132 } 133 charactersDontNeedingEncoding.set('-'); 134 charactersDontNeedingEncoding.set('_'); 135 charactersDontNeedingEncoding.set('.'); 136 charactersDontNeedingEncoding.set('*'); 137 charactersDontNeedingEncoding.set('"'); 138 } 139 140 146 public static String encode(String s) 147 { 148 final StringBuffer out = new StringBuffer ( s.length() ); 149 final ByteArrayOutputStream buf = new ByteArrayOutputStream ( 32 ); 150 final OutputStreamWriter writer = new OutputStreamWriter ( buf ); 151 for (int i = 0; i < s.length(); i++) 152 { 153 int c = (int)s.charAt(i); 154 if (charactersDontNeedingEncoding.get(c)) 155 { 156 out.append((char)c); 157 } 158 else 159 { 160 try 161 { 162 writer.write(c); 163 writer.flush(); 164 } 165 catch(IOException e) 166 { 167 buf.reset(); 168 continue; 169 } 170 byte[] ba = buf.toByteArray(); 171 for (int j = 0; j < ba.length; j++) 172 { 173 out.append('%'); 174 char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16); 175 if (Character.isLetter(ch)) 178 { 179 ch -= characterCaseDiff; 180 } 181 out.append(ch); 182 ch = Character.forDigit(ba[j] & 0xF, 16); 183 if (Character.isLetter(ch)) 184 { 185 ch -= characterCaseDiff; 186 } 187 out.append(ch); 188 } 189 buf.reset(); 190 } 191 } 192 193 return out.toString(); 194 } 195 196 201 public static InputSource getInputSource(Source source) 202 throws IOException 203 { 204 final InputSource newObject = new InputSource ( source.getInputStream() ); 205 newObject.setSystemId( source.getSystemId() ); 206 return newObject; 207 } 208 209 } 210 | Popular Tags |