/** * Read the contents of an unparsed text file * * @param checker NameChecker for checking whether characters are valid XML characters * @param reader Reader to be used for reading the file * @return a CharSequence representing the contents of the file * @throws IOException if a failure occurs reading the file * @throws XPathException if the file contains illegal characters */ public static CharSequence readFile(NameChecker checker, Reader reader) throws IOException, XPathException { FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.LARGE); char[] buffer = new char[2048]; boolean first = true; int actual; int line = 1; int column = 1; while (true) { actual = reader.read(buffer, 0, 2048); if (actual < 0) { break; } for (int c = 0; c < actual; ) { int ch32 = buffer[c++]; if (ch32 == '\n') { line++; column = 0; } column++; if (UTF16CharacterSet.isHighSurrogate(ch32)) { if (c == actual) { actual = reader.read(buffer, 0, 2048); c = 0; } char low = buffer[c++]; ch32 = UTF16CharacterSet.combinePair((char) ch32, low); } if (!checker.isValidChar(ch32)) { XPathException err = new XPathException( "The unparsed-text file contains a character that is illegal in XML (line=" + line + " column=" + column + " value=hex " + Integer.toHexString(ch32) + ')'); err.setErrorCode("XTDE1190"); throw err; } } if (first) { first = false; if (buffer[0] == '\ufeff') { // don't include the BOM in the result sb.append(buffer, 1, actual - 1); } else { sb.append(buffer, 0, actual); } } else { sb.append(buffer, 0, actual); } } reader.close(); return sb.condense(); }
public static void main(String[] args) throws Exception { FastStringBuffer sb1 = new FastStringBuffer(FastStringBuffer.MEDIUM); FastStringBuffer sb2 = new FastStringBuffer(FastStringBuffer.MEDIUM); File file = new File(args[0]); InputStream is = new FileInputStream(file); while (true) { int b = is.read(); if (b < 0) { System.out.println(sb1.toString()); System.out.println(sb2.toString()); break; } sb1.append(Integer.toHexString(b) + " "); sb2.append((char) b + " "); if (sb1.length() > 80) { System.out.println(sb1.toString()); System.out.println(sb2.toString()); sb1 = new FastStringBuffer(FastStringBuffer.MEDIUM); sb2 = new FastStringBuffer(FastStringBuffer.MEDIUM); } } is.close(); }
private String generateId() { FastStringBuffer buff = new FastStringBuffer(FastStringBuffer.TINY); generateId(buff); return buff.toString(); }