1 52 53 package com.go.trove.io; 54 55 import java.io.*; 56 57 64 public class DefaultCharToByteBuffer 65 implements CharToByteBuffer, Serializable 66 { 67 private ByteBuffer mBuffer; 68 private transient OutputStreamWriter mConvertor; 69 70 private char[] mChars; 71 private int mCapacity; 72 private int mCursor; 73 74 private String mDefaultEncoding; 75 76 79 public DefaultCharToByteBuffer(ByteBuffer buffer) { 80 this(buffer, null); 81 } 82 83 88 public DefaultCharToByteBuffer(ByteBuffer buffer, String defaultEncoding) { 89 mBuffer = buffer; 90 mChars = new char[4000]; 91 mCapacity = mChars.length; 92 mDefaultEncoding = defaultEncoding; 93 } 94 95 public void setEncoding(String enc) throws IOException { 96 drain(true); 97 mConvertor = new OutputStreamWriter 98 (new ByteBufferOutputStream(mBuffer), enc); 99 } 100 101 public String getEncoding() { 102 return (mConvertor == null) ? mDefaultEncoding : 103 mConvertor.getEncoding(); 104 } 105 106 public long getBaseByteCount() throws IOException { 107 return mBuffer.getBaseByteCount(); 108 } 109 110 public long getByteCount() throws IOException { 111 drain(true); 112 return mBuffer.getByteCount(); 113 } 114 115 public void writeTo(OutputStream out) throws IOException { 116 drain(true); 117 mBuffer.writeTo(out); 118 } 119 120 public void append(byte b) throws IOException { 121 drain(true); 122 mBuffer.append(b); 123 } 124 125 public void append(byte[] bytes) throws IOException { 126 append(bytes, 0, bytes.length); 127 } 128 129 public void append(byte[] bytes, int offset, int length) 130 throws IOException { 131 132 if (length != 0) { 133 drain(true); 134 mBuffer.append(bytes, offset, length); 135 } 136 } 137 138 public void appendSurrogate(ByteData s) throws IOException { 139 if (s != null) { 140 drain(true); 141 mBuffer.appendSurrogate(s); 142 } 143 } 144 145 public void addCaptureBuffer(ByteBuffer buffer) throws IOException { 146 drain(true); 147 mBuffer.addCaptureBuffer(buffer); 148 } 149 150 public void removeCaptureBuffer(ByteBuffer buffer) throws IOException { 151 drain(true); 152 mBuffer.removeCaptureBuffer(buffer); 153 } 154 155 public void append(char c) throws IOException { 156 if (mCursor >= mCapacity) { 157 drain(false); 158 } 159 mChars[mCursor++] = c; 160 } 161 162 public void append(char[] chars) throws IOException { 163 append(chars, 0, chars.length); 164 } 165 166 public void append(char[] chars, int offset, int length) 167 throws IOException 168 { 169 if (length == 0) { 170 return; 171 } 172 173 int capacity = mCapacity; 174 175 if (length < (capacity - mCursor)) { 176 System.arraycopy(chars, offset, mChars, mCursor, length); 177 mCursor += length; 178 return; 179 } 180 181 drain(false); 183 184 if (length < capacity) { 185 System.arraycopy(chars, offset, mChars, mCursor, length); 186 mCursor += length; 187 return; 188 } 189 190 getConvertor().write(chars, offset, length); 192 } 193 194 public void append(String str) throws IOException { 195 append(str, 0, str.length()); 196 } 197 198 public void append(String str, int offset, int length) throws IOException { 199 if (length == 0) { 200 return; 201 } 202 203 int capacity = mCapacity; 204 int avail = capacity - mCursor; 205 206 if (length <= avail) { 207 str.getChars(offset, offset + length, mChars, mCursor); 208 mCursor += length; 209 return; 210 } 211 212 str.getChars(offset, offset + avail, mChars, mCursor); 214 offset += avail; 215 length -= avail; 216 mCursor = capacity; 217 drain(false); 218 219 while (length >= capacity) { 221 str.getChars(offset, offset + capacity, mChars, 0); 222 offset += capacity; 223 length -= capacity; 224 mCursor = capacity; 225 drain(false); 226 } 227 228 if (length > 0) { 230 str.getChars(offset, offset + length, mChars, 0); 231 mCursor = length; 232 } 233 } 234 235 public void reset() throws IOException { 236 mBuffer.reset(); 237 } 238 239 public void drain() throws IOException { 240 drain(true); 241 } 242 243 private OutputStreamWriter getConvertor() 244 throws UnsupportedEncodingException 245 { 246 if (mConvertor == null) { 247 if (mDefaultEncoding == null) { 248 mConvertor = new OutputStreamWriter 249 (new ByteBufferOutputStream(mBuffer)); 250 } 251 else { 252 mConvertor = new OutputStreamWriter 253 (new ByteBufferOutputStream(mBuffer), mDefaultEncoding); 254 } 255 } 256 return mConvertor; 257 } 258 259 private void drain(boolean flush) throws IOException { 260 if (mCursor != 0) { 261 try { 262 getConvertor().write(mChars, 0, mCursor); 263 } 264 finally { 265 mCursor = 0; 266 } 267 } 268 269 if (flush && mConvertor != null) { 270 mConvertor.flush(); 271 } 272 } 273 274 private void writeObject(ObjectOutputStream out) throws IOException { 275 out.defaultWriteObject(); 276 if (mConvertor == null) { 277 out.writeObject(null); 278 } 279 else { 280 out.writeObject(mConvertor.getEncoding()); 281 } 282 } 283 284 private void readObject(ObjectInputStream in) 285 throws IOException, ClassNotFoundException 286 { 287 in.defaultReadObject(); 288 String enc = (String )in.readObject(); 289 if (enc != null) { 290 mConvertor = new OutputStreamWriter 291 (new ByteBufferOutputStream(mBuffer), enc); 292 } 293 } 294 } 295 | Popular Tags |