1 29 30 package com.caucho.quercus.lib.i18n; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.UnimplementedException; 34 import com.caucho.quercus.env.*; 35 import com.caucho.vfs.TempCharBuffer; 36 import com.caucho.vfs.TempStream; 37 import com.caucho.vfs.WriteStream; 38 39 import javax.mail.internet.MimeUtility ; 40 import java.io.IOException ; 41 import java.io.Reader ; 42 import java.io.UnsupportedEncodingException ; 43 44 public class IconvUtility { 45 46 public static BinaryValue decodeEncode(StringValue str, 47 String inCharset, 48 String outCharset) 49 throws UnsupportedEncodingException 50 { 51 return decodeEncode(str, inCharset, outCharset, 0, Integer.MAX_VALUE); 52 } 53 54 public static BinaryValue decodeEncode(StringValue str, 55 String inCharset, 56 String outCharset, 57 int offset) 58 throws UnsupportedEncodingException 59 { 60 return decodeEncode(str, inCharset, outCharset, offset, Integer.MAX_VALUE); 61 } 62 63 66 public static BinaryValue decodeEncode(StringValue str, 67 String inCharset, 68 String outCharset, 69 int offset, 70 int length) 71 throws UnsupportedEncodingException 72 { 73 TempCharBuffer tb = TempCharBuffer.allocate(); 74 char[] charBuf = tb.getBuffer(); 75 76 try { 77 Reader in = str.toReader(inCharset); 78 79 TempStream ts = new TempStream(); 80 WriteStream out = new WriteStream(ts); 81 out.setEncoding(outCharset); 82 83 while (offset > 0) { 84 if (in.read() < 0) 85 break; 86 offset--; 87 } 88 89 int sublen; 90 91 while (length > 0 && 92 (sublen = in.read(charBuf, 0, charBuf.length)) >= 0) { 93 94 sublen = Math.min(length, sublen); 95 96 out.print(charBuf, 0, sublen); 97 length -= sublen; 98 } 99 100 out.flush(); 101 return new TempBufferStringValue(ts.getHead()); 102 103 } catch (IOException e) { 104 throw new QuercusModuleException(e.getMessage()); 105 } 106 107 finally { 108 TempCharBuffer.free(tb); 109 } 110 } 111 112 115 public static BinaryValue decodeMime(Env env, 116 CharSequence word, 117 String charset) 118 throws UnsupportedEncodingException 119 { 120 StringValue str = new StringValueImpl( 121 MimeUtility.unfold(MimeUtility.decodeText(word.toString()))); 122 123 return str.toBinaryValue(env, charset); 124 } 125 126 public static Value encodeMime(Env env, 127 StringValue name, 128 StringValue value, 129 String inCharset, 130 String outCharset, 131 String scheme) 132 throws UnsupportedEncodingException 133 { 134 return encodeMime(env, 135 name, 136 value, 137 inCharset, 138 outCharset, 139 scheme, 140 "\r\n", 141 76); 142 } 143 144 153 156 public static StringValue encodeMime(Env env, 157 StringValue name, 158 StringValue value, 159 String inCharset, 160 String outCharset, 161 String scheme, 162 String lineBreakChars, 163 int lineLength) 164 throws UnsupportedEncodingException 165 { 166 name = name.toUnicodeValue(env, inCharset); 167 value = value.toUnicodeValue(env, inCharset); 168 169 StringBuilderValue sb = new StringBuilderValue(); 170 sb.append(name); 171 sb.append(':'); 172 sb.append(' '); 173 174 String word = encodeMimeWord( 175 value.toString(), outCharset, scheme, lineBreakChars, lineLength); 176 177 sb.append(MimeUtility.fold(sb.length(), word)); 178 179 return sb; 180 } 181 182 public static String encodeMimeWord(String value, 183 String charset, 184 String scheme, 185 String lineBreakChars, 186 int lineLength) 187 throws UnsupportedEncodingException 188 { 189 if (lineLength != 76) 190 throw new UnimplementedException("Mime line length option"); 191 192 if (! lineBreakChars.equals("\r\n")) 193 throw new UnimplementedException("Mime line break option"); 194 195 return MimeUtility.encodeWord(value, charset, scheme); 196 } 197 } 198 | Popular Tags |