+-

YUI Compressor 是一个用来压缩 JS 和 CSS 文件的工具,采用Java开发。
使用方法:
//压缩JS
java -jar yuicompressor-2.4.2.jar --type js --charset utf-8 -v src.js > packed.js
//压缩CSS
java -jar yuicompressor-2.4.2.jar --type css --charset utf-8 -v src.css > packed.css
网上搜索的 YUI Compressor 都是直接调用Java命令使用的或者通过文件流输入和文件流输出直接压缩js和css。
没有直接是字符串输入,压缩后 字符串输出的,没有办法自己研究吧,以下是研究后的成果,给大家共享下:
在线使用地址:http://www.aiisen.com/jscompress.html
1、先下载 yuicompressor.jar(此版本是2.4.2,如果需要下载最新的请到http://yuilibrary.com/download/#yuicompressor 下载)
在java上使用例子(主要是利用 ByteArrayInputStream 和 ByteArrayOutputStream):
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import org.mozilla.javascript.ErrorReporter; import org.mozilla.javascript.EvaluatorException; import com.yahoo.platform.yui.compressor.CssCompressor; import com.yahoo.platform.yui.compressor.JavaScriptCompressor; /** * @author [email protected] * @version 2013-06-15 */ public class YUICompressor { public static void main(String args[]) { String content = "function aiisen(stringA,stringB){\nvar stringA=\"你好爱森\";\nalert(\"hello world\"\n)};"; System.out.println(content); String com = YUICompressor.compress("js", content, -1, true); System.out.println(com); } private static StringBuffer errs = null; /** * @param type: js/css * @param content * @param linebreakpos 字节换行 * @param munge ture/false JS标识符混淆 */ public static String compress(String type, String content, int linebreakpos, boolean munge) { Reader in = null; Writer out = null; ByteArrayOutputStream byteOut = null; ByteArrayInputStream byteIn = null; String dcontent = null; try { String charset = "UTF-8"; if (type == null) { return "type can not be null"; } if (!type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) { return "type can only be 'js' or 'css'"; } byteIn = new ByteArrayInputStream(content.getBytes(charset)); in = new InputStreamReader(byteIn, charset); byteOut = new ByteArrayOutputStream(1024000); if (type.equalsIgnoreCase("js")) { // 只在压缩JS的时候才实例化 errs = new StringBuffer(); try { JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() { public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) { if (line < 0) { logErr("\n[ERROR] " + message); } else { logErr("\n[ERROR] " + line + ':' + lineOffset + ':' + message); } } public void error(String message, String sourceName, int line, String lineSource, int lineOffset) { if (line < 0) { logErr("\n[ERROR] " + message); } else { logErr("\n[ERROR] " + line + ':' + lineOffset + ':' + message); } } public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) { error(message, sourceName, line, lineSource, lineOffset); return new EvaluatorException(message); } }); out = new OutputStreamWriter(byteOut, charset); boolean preserveAllSemiColons = false; boolean disableOptimizations = false; compressor.compress(out, linebreakpos, munge, false, preserveAllSemiColons, disableOptimizations); out.flush(); dcontent = byteOut.toString(charset); } catch (EvaluatorException e) { e.printStackTrace(); return (errs == null ? "" : errs.toString()) + e.getMessage(); } } else if (type.equalsIgnoreCase("css")) { CssCompressor compressor = new CssCompressor(in); out = new OutputStreamWriter(byteOut, charset); compressor.compress(out, linebreakpos); out.flush(); dcontent = byteOut.toString(charset); } } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } } if (byteIn != null) { try { byteIn.close(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } } if (byteOut != null) { try { byteOut.close(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } } } return dcontent; } private static void logErr(String msg) { if (errs != null) { errs.append(msg); } } public static boolean isUTF8(byte[] data) { int count_good_utf = 0; int count_bad_utf = 0; byte current_byte = 0x00; byte previous_byte = 0x00; for (int i = 1; i < data.length; i++) { current_byte = data[i]; previous_byte = data[i - 1]; if ((current_byte & 0xC0) == 0x80) { if ((previous_byte & 0xC0) == 0xC0) { count_good_utf++; } else if ((previous_byte & 0x80) == 0x00) { count_bad_utf++; } } else if ((previous_byte & 0xC0) == 0xC0) { count_bad_utf++; } } return (count_good_utf > count_bad_utf); } }