1 52 53 package com.go.trove.io; 54 55 import java.io.*; 56 57 70 public class UnicodeReader extends EscapeReader { 71 72 private char[] mMinibuf = new char[4]; 73 74 private boolean mEscaped; 75 76 81 public UnicodeReader(Reader source) { 82 super(source, 6); 83 } 84 85 public int read() throws IOException { 86 int c = mSource.read(); 87 88 if (c != '\\' || !mEscapesEnabled) { 89 mEscaped = false; 90 return c; 91 } 92 93 c = mSource.read(); 94 95 if (c == '\\') { 97 mEscaped = !mEscaped; 98 mSource.unread(); 99 return '\\'; 100 } 101 102 if (c != 'u') { 104 mSource.unread(); 105 return '\\'; 106 } 107 108 110 if (mEscaped) { 112 mEscaped = false; 113 mSource.unread(); 114 return '\\'; 115 } 116 117 int len = mSource.read(mMinibuf, 0, 4); 118 119 if (len == 4) { 120 try { 121 int val = 122 Integer.valueOf(new String (mMinibuf, 0, 4), 16).intValue(); 123 124 return val; 125 } 126 catch (NumberFormatException e) { 127 } 131 } 132 133 if (len >= 0) { 135 mSource.unread(len + 1); 136 } 137 138 return '\\'; 139 } 140 141 public static void main(String [] arg) throws Exception { 142 Tester.test(arg); 143 } 144 145 private static class Tester { 146 public static void test(String [] arg) throws Exception { 147 String str = 148 "This is \\" + "u0061 test.\n" + 149 "This is \\" + "u00612 test.\n" + 150 "This is \\" + "u0061" + "\\" + "u0061" + " test.\n" + 151 "This is \\" + "u061 test.\n" + 152 "This is \\\\" + "u0061 test.\n" + 153 "This is \\" + "a test.\n"; 154 155 System.out.println("\nOriginal:\n"); 156 157 Reader reader = new StringReader(str); 158 159 int c; 160 while ( (c = reader.read()) >= 0 ) { 161 System.out.print((char)c); 162 } 163 164 System.out.println("\nConverted:\n"); 165 166 reader = new StringReader(str); 167 reader = new UnicodeReader(reader); 168 169 while ( (c = reader.read()) != -1 ) { 170 System.out.print((char)c); 171 } 172 173 System.out.println("\nUnread test 1:\n"); 174 175 reader = new StringReader(str); 176 PushbackPositionReader pr = 177 new PushbackPositionReader(new UnicodeReader(reader), 1); 178 179 while ( (c = pr.read()) != -1 ) { 180 pr.unread(); 181 c = pr.read(); 182 System.out.print((char)c); 183 } 184 185 System.out.println("\nUnread test 2:\n"); 186 187 reader = new StringReader(str); 188 pr = new PushbackPositionReader(new UnicodeReader(reader), 2); 189 190 int i = 0; 191 while ( (c = pr.read()) != -1 ) { 192 if ( (i++ % 5) == 0 ) { 193 c = pr.read(); 194 pr.unread(); 195 pr.unread(); 196 c = pr.read(); 197 } 198 199 System.out.print((char)c); 200 } 201 202 System.out.println("\nUnread position test:\n"); 203 204 reader = new StringReader(str); 205 pr = new PushbackPositionReader(new UnicodeReader(reader), 2); 206 207 System.out.print(pr.getNextPosition() + "\t"); 208 i = 0; 209 while ( (c = pr.read()) != -1 ) { 210 if ( (i++ % 5) == 0 ) { 211 c = pr.read(); 212 pr.unread(); 213 pr.unread(); 214 c = pr.read(); 215 } 216 217 System.out.println((char)c); 218 System.out.print(pr.getNextPosition() + "\t"); 219 } 220 } 221 } 222 } 223 | Popular Tags |