1 9 package javolution.io; 10 11 import j2me.lang.CharSequence; 12 import j2me.lang.IllegalStateException; 13 import j2me.io.CharConversionException; 14 import j2me.nio.ByteBuffer; 15 16 import java.io.IOException ; 17 import java.io.Writer ; 18 19 import javolution.lang.Reusable; 20 21 43 public final class UTF8ByteBufferWriter extends Writer implements Reusable { 44 45 48 private ByteBuffer _byteBuffer; 49 50 53 public UTF8ByteBufferWriter() { 54 } 55 56 64 public UTF8ByteBufferWriter setOutput(ByteBuffer byteBuffer) { 65 if (_byteBuffer != null) 66 throw new IllegalStateException ("Writer not closed or reset"); 67 _byteBuffer = byteBuffer; 68 return this; 69 } 70 71 79 public void write(char c) throws IOException { 80 if ((c < 0xd800) || (c > 0xdfff)) { 81 write((int) c); 82 } else if (c < 0xdc00) { _highSurrogate = c; 84 } else { int code = ((_highSurrogate - 0xd800) << 10) + (c - 0xdc00) 86 + 0x10000; 87 write(code); 88 } 89 } 90 91 private char _highSurrogate; 92 93 99 public void write(int code) throws IOException { 100 if ((code & 0xffffff80) == 0) { 101 _byteBuffer.put((byte) code); 102 } else { write2(code); 104 } 105 } 106 107 private void write2(int c) throws IOException { 108 if ((c & 0xfffff800) == 0) { _byteBuffer.put((byte) (0xc0 | (c >> 6))); 110 _byteBuffer.put((byte) (0x80 | (c & 0x3f))); 111 } else if ((c & 0xffff0000) == 0) { _byteBuffer.put((byte) (0xe0 | (c >> 12))); 113 _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3f))); 114 _byteBuffer.put((byte) (0x80 | (c & 0x3f))); 115 } else if ((c & 0xff200000) == 0) { _byteBuffer.put((byte) (0xf0 | (c >> 18))); 117 _byteBuffer.put((byte) (0x80 | ((c >> 12) & 0x3f))); 118 _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3f))); 119 _byteBuffer.put((byte) (0x80 | (c & 0x3f))); 120 } else if ((c & 0xf4000000) == 0) { _byteBuffer.put((byte) (0xf8 | (c >> 24))); 122 _byteBuffer.put((byte) (0x80 | ((c >> 18) & 0x3f))); 123 _byteBuffer.put((byte) (0x80 | ((c >> 12) & 0x3f))); 124 _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3f))); 125 _byteBuffer.put((byte) (0x80 | (c & 0x3f))); 126 } else if ((c & 0x80000000) == 0) { _byteBuffer.put((byte) (0xfc | (c >> 30))); 128 _byteBuffer.put((byte) (0x80 | ((c >> 24) & 0x3f))); 129 _byteBuffer.put((byte) (0x80 | ((c >> 18) & 0x3f))); 130 _byteBuffer.put((byte) (0x80 | ((c >> 12) & 0x3F))); 131 _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3F))); 132 _byteBuffer.put((byte) (0x80 | (c & 0x3F))); 133 } else { 134 throw new CharConversionException("Illegal character U+" 135 + Integer.toHexString(c)); 136 } 137 } 138 139 147 public void write(char cbuf[], int off, int len) throws IOException { 148 final int off_plus_len = off + len; 149 for (int i = off; i < off_plus_len;) { 150 char c = cbuf[i++]; 151 if (c < 0x80) { 152 _byteBuffer.put((byte) c); 153 } else { 154 write(c); 155 } 156 } 157 } 158 159 167 public void write(String str, int off, int len) throws IOException { 168 final int off_plus_len = off + len; 169 for (int i = off; i < off_plus_len;) { 170 char c = str.charAt(i++); 171 if (c < 0x80) { 172 _byteBuffer.put((byte) c); 173 } else { 174 write(c); 175 } 176 } 177 } 178 179 185 public void write(CharSequence csq) throws IOException { 186 final int length = csq.length(); 187 for (int i = 0; i < length;) { 188 char c = csq.charAt(i++); 189 if (c < 0x80) { 190 _byteBuffer.put((byte) c); 191 } else { 192 write(c); 193 } 194 } 195 } 196 197 203 public void flush() throws IOException { 204 if (_byteBuffer == null) { 205 throw new IOException ("Writer closed"); 206 } 207 } 208 209 214 public void close() throws IOException { 215 if (_byteBuffer != null) { 216 reset(); 217 } 218 } 219 220 public void reset() { 222 _byteBuffer = null; 223 _highSurrogate = 0; 224 } 225 226 229 public UTF8ByteBufferWriter setByteBuffer(ByteBuffer byteBuffer) { 230 return this.setOutput(byteBuffer); 231 } 232 233 } | Popular Tags |