Java 类com.sun.codemodel.util.UnicodeEscapeWriter 实例源码

项目:GitHub    文件:FileCodeWriterWithEncoding.java   
@Override
public Writer openSource(JPackage pkg, String fileName) throws IOException {
    final Writer bw = new OutputStreamWriter(openBinary(pkg, fileName), encoding);

    return new UnicodeEscapeWriter(bw) {
        private final CharsetEncoder encoder = Charset.forName(encoding).newEncoder();

        @Override
        protected boolean requireEscaping(int ch) {
            // control characters
            if (ch < 0x20 && " \t\r\n".indexOf(ch) == -1) {
                return true;
            }
            // ASCII chars
            if (ch < 0x80) {
                return false;
            }
            return !encoder.canEncode((char) ch);
        }
    };
}
项目:LRPaGe    文件:CodeWriter.java   
/**
 * Called by CodeModel to store the specified file.
 * The callee must allocate a storage to store the specified file.
 *
 * <p>
 * The returned stream will be closed before the next file is
 * stored. So the callee can assume that only one OutputStream
 * is active at any given time.
 *
 * @param   pkg
 *      The package of the file to be written.
 * @param   fileName
 *      File name without the path. Something like
 *      "Foo.java" or "Bar.properties"
 */
public Writer openSource( JPackage pkg, String fileName ) throws IOException {
    final OutputStreamWriter bw = encoding != null
            ? new OutputStreamWriter(openBinary(pkg,fileName), encoding)
            : new OutputStreamWriter(openBinary(pkg,fileName));

    // create writer
    try {
        return new UnicodeEscapeWriter(bw) {
            // can't change this signature to Encoder because
            // we can't have Encoder in method signature
            private final CharsetEncoder encoder = EncoderFactory.createEncoder(bw.getEncoding());
            @Override
            protected boolean requireEscaping(int ch) {
                // control characters
                if( ch<0x20 && " \t\r\n".indexOf(ch)==-1 )  return true;
                // check ASCII chars, for better performance
                if( ch<0x80 )       return false;

                return !encoder.canEncode((char)ch);
            }
        };
    } catch( Throwable t ) {
        return new UnicodeEscapeWriter(bw);
    }
}