1 52 53 package com.go.trove.io; 54 55 import java.io.*; 56 import java.util.Set ; 57 import java.util.HashSet ; 58 59 68 public class FastCharToByteBuffer implements CharToByteBuffer, Serializable { 69 private static final int TEMP_BUF_LEN = 512; 70 private static final Set SUPPORTED_ENCODINGS; 71 72 static { 73 SUPPORTED_ENCODINGS = new HashSet (); 74 SUPPORTED_ENCODINGS.add("8859-1"); 75 SUPPORTED_ENCODINGS.add("8859_1"); 76 SUPPORTED_ENCODINGS.add("iso-8859-1"); 77 SUPPORTED_ENCODINGS.add("ISO-8859-1"); 78 SUPPORTED_ENCODINGS.add("iso8859_1"); 79 SUPPORTED_ENCODINGS.add("ISO8859_1"); 80 } 81 82 static boolean isSupportedEncoding(String encoding) { 83 return SUPPORTED_ENCODINGS.contains(encoding); 84 } 85 86 private ByteBuffer mBuffer; 87 88 private transient byte[] mTempBytes; 89 private transient char[] mTempChars; 90 91 private CharToByteBuffer mSlowConvertor; 92 93 96 public FastCharToByteBuffer(ByteBuffer buffer) { 97 this(buffer, null); 98 } 99 100 105 public FastCharToByteBuffer(ByteBuffer buffer, String defaultEncoding) { 106 mBuffer = buffer; 107 try { 108 setEncoding(defaultEncoding); 109 } 110 catch (IOException e) { 111 } 112 } 113 114 public void setEncoding(String enc) throws IOException { 115 drain(); 116 if (isSupportedEncoding(enc)) { 117 mSlowConvertor = null; 118 } 119 else { 120 mSlowConvertor = new DefaultCharToByteBuffer(mBuffer, enc); 121 } 122 } 123 124 public String getEncoding() throws IOException { 125 return mSlowConvertor != null ? 126 mSlowConvertor.getEncoding() : "ISO-8859-1"; 127 } 128 129 public long getBaseByteCount() throws IOException { 130 drain(); 131 return mBuffer.getBaseByteCount(); 132 } 133 134 public long getByteCount() throws IOException { 135 drain(); 136 return mBuffer.getByteCount(); 137 } 138 139 public void writeTo(OutputStream out) throws IOException { 140 drain(); 141 mBuffer.writeTo(out); 142 } 143 144 public void append(byte b) throws IOException { 145 drain(); 146 mBuffer.append(b); 147 } 148 149 public void append(byte[] bytes) throws IOException { 150 append(bytes, 0, bytes.length); 151 } 152 153 public void append(byte[] bytes, int offset, int length) 154 throws IOException 155 { 156 if (length != 0) { 157 drain(); 158 mBuffer.append(bytes, offset, length); 159 } 160 } 161 162 public void appendSurrogate(ByteData s) throws IOException { 163 if (s != null) { 164 drain(); 165 mBuffer.appendSurrogate(s); 166 } 167 } 168 169 public void addCaptureBuffer(ByteBuffer buffer) throws IOException { 170 drain(); 171 mBuffer.addCaptureBuffer(buffer); 172 } 173 174 public void removeCaptureBuffer(ByteBuffer buffer) throws IOException { 175 drain(); 176 mBuffer.removeCaptureBuffer(buffer); 177 } 178 179 public void append(char c) throws IOException { 180 char[] chars = getTempChars(); 181 chars[0] = c; 182 append(chars, 0, 1); 183 } 184 185 public void append(char[] chars) throws IOException { 186 append(chars, 0, chars.length); 187 } 188 189 public void append(char[] chars, int offset, int length) 190 throws IOException 191 { 192 if (mSlowConvertor != null) { 193 mSlowConvertor.append(chars, offset, length); 194 return; 195 } 196 197 appendFast(chars, offset, length); 198 } 199 200 public void append(String str) throws IOException { 201 append(str, 0, str.length()); 202 } 203 204 public void append(String str, int offset, int length) throws IOException { 205 if (mSlowConvertor != null) { 206 mSlowConvertor.append(str, offset, length); 207 return; 208 } 209 210 if (length == 0) { 211 return; 212 } 213 214 char[] tempChars = getTempChars(); 215 int bufLen = tempChars.length; 216 217 while (length >= bufLen) { 218 str.getChars(offset, offset + bufLen, tempChars, 0); 219 offset += bufLen; 220 length -= bufLen; 221 appendFast(tempChars, 0, bufLen); 222 } 223 224 if (length > 0) { 225 str.getChars(offset, offset + length, tempChars, 0); 226 appendFast(tempChars, 0, length); 227 } 228 } 229 230 public void reset() throws IOException { 231 mBuffer.reset(); 232 } 233 234 public void drain() throws IOException { 235 if (mSlowConvertor != null) { 236 mSlowConvertor.drain(); 237 } 238 } 239 240 private void appendFast(char[] chars, int offset, int length) 241 throws IOException 242 { 243 byte[] tempBytes = getTempBytes(); 244 int bufLen = tempBytes.length; 245 int bi = 0; 246 int climit = offset + length; 247 248 for (int ci = offset; ci < climit; ci++) { 249 tempBytes[bi++] = (byte)chars[ci]; 250 if (bi >= bufLen) { 251 mBuffer.append(tempBytes, 0, bufLen); 252 bi = 0; 253 } 254 } 255 256 if (bi > 0) { 257 mBuffer.append(tempBytes, 0, bi); 258 } 259 } 260 261 private byte[] getTempBytes() { 262 if (mTempBytes == null) { 263 mTempBytes = new byte[TEMP_BUF_LEN]; 264 } 265 return mTempBytes; 266 } 267 268 private char[] getTempChars() { 269 if (mTempChars == null) { 270 mTempChars = new char[TEMP_BUF_LEN]; 271 } 272 return mTempChars; 273 } 274 } 275 | Popular Tags |