1 28 29 package com.caucho.vfs.i18n; 30 31 import com.caucho.vfs.OutputStreamWithBuffer; 32 33 import java.io.IOException ; 34 35 39 public class UTF8Writer extends EncodingWriter { 40 private final static UTF8Writer _writer = new UTF8Writer(); 41 42 45 public UTF8Writer() 46 { 47 } 48 49 52 public String getJavaEncoding() 53 { 54 return "UTF8"; 55 } 56 57 62 public EncodingWriter create(String javaEncoding) 63 { 64 return _writer; 65 } 66 67 72 public EncodingWriter create() 73 { 74 return _writer; 75 } 76 77 82 public void write(OutputStreamWithBuffer os, char ch) 83 throws IOException 84 { 85 byte []buffer = os.getBuffer(); 86 int length = os.getBufferOffset(); 87 88 if (buffer.length <= length + 2) { 89 buffer = os.nextBuffer(length); 90 length = os.getBufferOffset(); 91 } 92 93 if (ch < 0x80) { 94 buffer[length++] = (byte) ch; 95 } 96 else if (ch < 0x800) { 97 buffer[length++] = (byte) (0xc0 + (ch >> 6)); 98 buffer[length++] = (byte) (0x80 + (ch & 0x3f)); 99 } 100 else { 101 buffer[length++] = (byte) (0xe0 + (ch >> 12)); 102 buffer[length++] = (byte) (0x80 + ((ch >> 6) & 0x3f)); 103 buffer[length++] = (byte) (0x80 + (ch & 0x3f)); 104 } 105 106 os.setBufferOffset(length); 107 } 108 109 116 public void write(OutputStreamWithBuffer os, char []cbuf, int off, int len) 117 throws IOException 118 { 119 byte []buffer = os.getBuffer(); 120 int length = os.getBufferOffset(); 121 int capacity = buffer.length; 122 123 for (int i = 0; i < len; i++) { 124 if (capacity <= length + 2) { 125 buffer = os.nextBuffer(length); 126 length = os.getBufferOffset(); 127 } 128 129 char ch = cbuf[off + i]; 130 131 if (ch < 0x80) 132 buffer[length++] = (byte) ch; 133 else if (ch < 0x800) { 134 buffer[length++] = (byte) (0xc0 + (ch >> 6)); 135 buffer[length++] = (byte) (0x80 + (ch & 0x3f)); 136 } 137 else if (ch < 0xd800 || 0xdfff < ch) { 138 buffer[length++] = (byte) (0xe0 + (ch >> 12)); 139 buffer[length++] = (byte) (0x80 + ((ch >> 6) & 0x3f)); 140 buffer[length++] = (byte) (0x80 + (ch & 0x3f)); 141 } 142 else { 143 char ch2 = cbuf[off + i + 1]; 144 int v = 0x10000 + (ch & 0x3ff) * 0x400 + (ch2 & 0x3ff); 145 146 i += 1; 147 148 buffer[length++] = (byte) (0xf0 + (v >> 18)); 149 buffer[length++] = (byte) (0x80 + ((v >> 12) & 0x3f)); 150 buffer[length++] = (byte) (0x80 + ((v >> 6) & 0x3f)); 151 buffer[length++] = (byte) (0x80 + (v & 0x3f)); 152 } 153 } 154 155 os.setBufferOffset(length); 156 } 157 } 158 159 160 | Popular Tags |