Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  Kontraktor-Http Copyright (c) Ruediger Moeller, All rights reserved.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 3.0 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 See https://www.gnu.org/licenses/lgpl.txt
 */
 package org.nustaq.kontraktor.remoting.http.javascript.jsmin;
 // original package ch.simschla.minify.js;
 
 import java.io.*;
This is a java port of Douglas Crockford's jsmin utility by https://github.com/simschla released under Apache 2.0 License Currently implemented version: 2a943dba6bae746075749499b1da7955474a47b1
 
 public class JSMin {
 
     private final InputStream inputStream;
     private final OutputStream outputStream;
 
     private final String customHeader;
     public static final int EOF = -1;
 
     private int theA;
     private int theB;
     private int theLookahead = ;
     private int theX = ;
     private int theY = ;
 
     private JSMin(Builder builder) {
         this. = builder.inputStream();
         this. = builder.outputStream();
         this. = builder.customHeader();
     }
 
     public static byte[] minifybyte bytes[] ) {
         ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
         ByteArrayOutputStream bout = new ByteArrayOutputStream(bytes.length);
         builder().inputStream(bin).outputStream(bout).build().minify();
         return bout.toByteArray();
     }
 
     public void minify() {
         try {
             writeCustomHeader();
             jsmin();
         } catch (IOException e) {
             throw new RuntimeException(e);
         } finally {
             try {
                 .flush();
             } catch (IOException e) {
                 //ignore silently
             }
         }
     }
 
     protected void writeCustomHeader() throws IOException {
     }
 
     /* jsmin -- Copy the input to the output, deleting the characters which are
         insignificant to JavaScript. Comments will be removed. Tabs will be
         replaced with spaces. Carriage returns will be replaced with linefeeds.
         Most spaces and linefeeds will be removed.
     */
     private void jsmin() throws IOException {
         if (peek() == 0xEF) {
             get();
             get();
             get();
         }
          = '\n';
         action(3);
         while ( != ) {
             switch () {
                 case ' ':
                     action(isAlphanum() ? 1 : 2);
                     break;
                 case '\n':
                     switch () {
                         case '{':
                         case '[':
                         case '(':
                         case '+':
                         case '-':
                         case '!':
                         case '~':
                             action(1);
                             break;
                        case ' ':
                            action(3);
                            break;
                        default:
                            action(isAlphanum() ? 1 : 2);
                    }
                    break;
                default:
                    switch () {
                        case ' ':
                            action(isAlphanum() ? 1 : 3);
                            break;
                        case '\n':
                            switch () {
                                case '}':
                                case ']':
                                case ')':
                                case '+':
                                case '-':
                                case '"':
                                case '\'':
                                case '`':
                                    action(1);
                                    break;
                                default:
                                    action(isAlphanum() ? 1 : 3);
                            }
                            break;
                        default:
                            action(1);
                            break;
                    }
            }
        }
    }
    /* get -- return the next character from input. Watch out for lookahead. If
        the character is a control character, translate it to a space or
        linefeed.
    */
    private int get() throws IOException {
        int c = ;
         = ;
        if (c == ) {
            c = .read();
        }
        if (c >= ' ' || c == '\n' || c == ) {
            return c;
        }
        if (c == '\r') {
            return '\n';
        }
        return ' ';
    }
    /* peek -- get the next character without getting it.
    */
    private int peek() throws IOException {
         = get();
        return ;
    }
    /* next -- get the next character, excluding comments. peek() is used to see
        if a '/' is followed by a '/' or '*'.
*/
    private int next() throws IOException {
        int c = get();
        if (c == '/') {
            switch (peek()) {
                case '/':
                    for (; ; ) {
                        c = get();
                        if (c <= '\n') {
                            break;
                        }
                    }
                    break;
                case '*':
                    get();
                    while (c != ' ') {
                        switch (get()) {
                            case '*':
                                if (peek() == '/') {
                                    get();
                                    c = ' ';
                                }
                                break;
                            case :
                                error("Unterminated comment.");
                        }
                    }
                    break;
            }
        }
         = ;
         = c;
        return c;
    }
	/* action -- do something! What you do is determined by the argument:
        1   Output A. Copy B to A. Get the next B.
        2   Copy B to A. Get the next B. (Delete A).
        3   Get the next B. (Delete B).
   action treats a string as a single character. Wow!
   action recognizes a regular expression if it is preceded by ( or , or =.
*/
    private void action(int dthrows IOException {
        int p;
        switch (d) {
            case 1:
                .write();
                if (
                        ( == '\n' ||  == ' ') &&
                                ( == '+' ||  == '-' ||  == '*' ||  == '/') &&
                                ( == '+' ||  == '-' ||  == '*' ||  == '/')
                        ) {
                    .write();
                }
            case 2:
                 = ;
                if ( == '\'' ||  == '"' ||  == '`') {
                    for (; ; ) {
                        .write();
                         = get();
                        if ( == ) {
                            break;
                        }
                        if ( == '\\') {
                            .write();
                             = get();
                        }
                        if ( == ) {
                            error("Unterminated string literal.");
                        }
                    }
                }
            case 3:
                 = next();
                if ( == '/' && (
                         == '(' ||  == ',' ||  == '=' ||  == ':' ||
                                 == '[' ||  == '!' ||  == '&' ||  == '|' ||
                                 == '?' ||  == '+' ||  == '-' ||  == '~' ||
                                 == '*' ||  == '/' ||  == '\n'
                )) {
                    .write();
                    if ( == '/' ||  == '*') {
                        .write(' ');
                    }
                    .write();
                    for (; ; ) {
                         = get();
                        if ( == '[') {
                            for (; ; ) {
                                .write();
                                 = get();
                                if ( == ']') {
                                    break;
                                }
                                if ( == '\\') {
                                    .write();
                                     = get();
                                }
                                if ( == ) {
                                    error("Unterminated set in Regular Expression literal.");
                                }
                            }
                        } else if ( == '/') {
                            switch (peek()) {
                                case '/':
                                case '*':
                                    error("Unterminated set in Regular Expression literal.");
                            }
                            break;
                        } else if ( == '\\') {
                            .write();
                             = get();
                        }
                        if ( == ) {
                            error("Unterminated Regular Expression literal.");
                        }
                        .write();
                    }
                     = next();
                }
        }
    }
    private void error(String s) {
        ..println("JSMin Error: " + s);
        throw new JsMinException(s);
    }
	/* isAlphanum -- return true if the character is a letter, digit, underscore,
        dollar sign, or non-ASCII character.
*/
    private boolean isAlphanum(int c) {
        return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
                (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
                c > 126);
    }
    public static Builder builder() {
        return new Builder();
    }
    //--- inner classes
    static final class JsMinException extends RuntimeException {
        private JsMinException(String message) {
            super(message);
        }
    }
    public static final class Builder {
        private InputStream inputStream = .;
        private OutputStream outputStream = .;
        private String customHeader = "";
        private Charset charset = Charset.forName("UTF-8");
        public InputStream inputStream() {
            return this.;
        }
        public OutputStream outputStream() {
            return this.;
        }
        public Builder inputStream(final InputStream inputStream) {
            this. = inputStream;
            return this;
        }
        public Builder outputStream(final OutputStream outputStream) {
            this. = outputStream;
            return this;
        }
        public String customHeader() {
            return this.;
        }
        public Builder customHeader(final String customHeader) {
            this. = customHeader;
            return this;
        }
        public Charset charset() {
            return this.;
        }
        public Builder charset(final Charset charset) {
            this. = charset;
            return this;
        }
        public JSMin build() {
            return new JSMin(this);
        }
    }
    public static void main(String[] args) {
        JSMin.builder().build().minify();
    }
New to GrepCode? Check out our FAQ X