1 29 30 package com.caucho.vfs.i18n; 31 32 import com.caucho.vfs.OutputStreamWithBuffer; 33 34 import java.io.IOException ; 35 import java.io.OutputStreamWriter ; 36 import java.io.UnsupportedEncodingException ; 37 import java.nio.charset.Charset ; 38 import java.util.logging.Level ; 39 40 43 public class JDKWriter extends EncodingWriter { 44 private String _javaEncoding; 45 46 49 public JDKWriter() 50 { 51 } 52 55 public String getJavaEncoding() 56 { 57 return _javaEncoding; 58 } 59 60 63 public void setJavaEncoding(String encoding) 64 { 65 _javaEncoding = encoding; 66 } 67 68 69 76 public EncodingWriter create(String javaEncoding) 77 { 78 try { 79 return new OutputStreamEncodingWriter(javaEncoding); 80 } catch (UnsupportedEncodingException e) { 81 log.log(Level.WARNING, e.toString(), e); 82 83 return null; 84 } 85 } 86 87 90 public void write(OutputStreamWithBuffer os, char ch) 91 throws IOException 92 { 93 throw new UnsupportedOperationException (); 94 } 95 96 static class OutputStreamEncodingWriter extends EncodingWriter { 97 private Charset _charset; 98 private String _encoding; 99 private OutputStreamWriter _writer; 100 private OutputStreamWithBuffer _os; 101 102 OutputStreamEncodingWriter(String javaEncoding) 103 throws UnsupportedEncodingException 104 { 105 try { 106 _encoding = javaEncoding; 107 108 if (Charset.isSupported(javaEncoding)) 109 _charset = Charset.forName(javaEncoding); 110 else { 111 throw new UnsupportedEncodingException (javaEncoding); 113 } 114 } catch (java.nio.charset.UnsupportedCharsetException e) { 115 throw new UnsupportedEncodingException (e.getMessage()); 116 } 117 } 118 119 122 public void write(OutputStreamWithBuffer os, char ch) 123 throws IOException 124 { 125 if (_os != os) { 126 if (_charset != null) 127 _writer = new OutputStreamWriter (os, _charset); 128 else 129 _writer = new OutputStreamWriter (os, _encoding); 130 131 _os = os; 132 } 133 134 _writer.write(ch); 135 _writer.flush(); 136 } 137 138 141 public void write(OutputStreamWithBuffer os, 142 char []buf, int offset, int length) 143 throws IOException 144 { 145 if (_os != os) { 146 if (_charset != null) 147 _writer = new OutputStreamWriter (os, _charset); 148 else 149 _writer = new OutputStreamWriter (os, _encoding); 150 151 _os = os; 152 } 153 154 _writer.write(buf, offset, length); 155 _writer.flush(); 156 } 157 158 161 public EncodingWriter create(String encoding) 162 { 163 throw new UnsupportedOperationException (); 164 } 165 } 166 } 167 | Popular Tags |