/*
 * Copyright 2011 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.handler.codec.http;

import java.nio.charset.Charset;

import io.netty.buffer.ChannelBuffer;
import io.netty.util.CharsetUtil;

/**
 * Shared Static object between HttpMessageDecoder, HttpPostRequestDecoder and HttpPostRequestEncoder
 */
final class HttpPostBodyUtil {

    public static int chunkSize = 8096;
    /**
     * HTTP content disposition header name.
     */
    public static final String CONTENT_DISPOSITION = "Content-Disposition";

    public static final String NAME = "name";

    public static final String FILENAME = "filename";

    /**
     * Content-disposition value for form data.
     */
    public static final String FORM_DATA = "form-data";

    /**
     * Content-disposition value for file attachment.
     */
    public static final String ATTACHMENT = "attachment";

    /**
     * Content-disposition value for file attachment.
     */
    public static final String FILE = "file";

    /**
     * HTTP content type body attribute for multiple uploads.
     */
    public static final String MULTIPART_MIXED = "multipart/mixed";

    /**
     * Charset for 8BIT
     */
    public static final Charset ISO_8859_1 = CharsetUtil.ISO_8859_1;

    /**
     * Charset for 7BIT
     */
    public static final Charset US_ASCII = CharsetUtil.US_ASCII;

    /**
     * Default Content-Type in binary form
     */
    public static final String DEFAULT_BINARY_CONTENT_TYPE = "application/octet-stream";

    /**
     * Default Content-Type in Text form
     */
    public static final String DEFAULT_TEXT_CONTENT_TYPE = "text/plain";

    /**
     * Allowed mechanism for multipart
     * mechanism := "7bit"
                  / "8bit"
                  / "binary"
       Not allowed: "quoted-printable"
                  / "base64"
     */
    public enum TransferEncodingMechanism {
        /**
         * Default encoding
         */
        BIT7("7bit"),
        /**
         * Short lines but not in ASCII - no encoding
         */
        BIT8("8bit"),
        /**
         * Could be long text not in ASCII - no encoding
         */
        BINARY("binary");

        public String value;

        TransferEncodingMechanism(String value) {
            this.value = value;
        }

        TransferEncodingMechanism() {
            value = name();
        }

        @Override
        public String toString() {
            return value;
        }
    }

    private HttpPostBodyUtil() {
    }

    //Some commons methods between HttpPostRequestDecoder and HttpMessageDecoder
    /**
     * Skip control Characters
     * @param buffer
     */
    static void skipControlCharacters(ChannelBuffer buffer) {
        for (;;) {
            char c = (char) buffer.readUnsignedByte();
            if (!Character.isISOControl(c) && !Character.isWhitespace(c)) {
                buffer.readerIndex(buffer.readerIndex() - 1);
                break;
            }
        }
    }

    /**
     * Find the first non whitespace
     * @param sb
     * @param offset
     * @return the rank of the first non whitespace
     */
    static int findNonWhitespace(String sb, int offset) {
        int result;
        for (result = offset; result < sb.length(); result ++) {
            if (!Character.isWhitespace(sb.charAt(result))) {
                break;
            }
        }
        return result;
    }

    /**
     * Find the first whitespace
     * @param sb
     * @param offset
     * @return the rank of the first whitespace
     */
    static int findWhitespace(String sb, int offset) {
        int result;
        for (result = offset; result < sb.length(); result ++) {
            if (Character.isWhitespace(sb.charAt(result))) {
                break;
            }
        }
        return result;
    }

    /**
     * Find the end of String
     * @param sb
     * @return the rank of the end of string
     */
    static int findEndOfString(String sb) {
        int result;
        for (result = sb.length(); result > 0; result --) {
            if (!Character.isWhitespace(sb.charAt(result - 1))) {
                break;
            }
        }
        return result;
    }

}

@@ -3080,123 +3080,503 @@
    /
-/Some commons methods between HttpPostRequestDecoder and HttpMessageDecoder%0A    /**%0A     * Skip control Characters%0A
+**%0A     * Exception when NO Backend Array is found%0A     */ %0A    static class SeekAheadNoBackArray extends Exception %7B%0A        private static final long serialVersionUID = -630418804938699495L;%0A    %7D%0A%0A    /**%0A     * This class intends to decrease the CPU in seeking ahead some bytes in %0A     * HttpPostRequestDecoder%0A     */%0A    static class SeekAheadOptimize %7B%0A        byte%5B%5D bytes;%0A%0A        int readerIndex;%0A%0A        int pos;%0A%0A        int limit;%0A%0A        ChannelBuffer buffer;%0A%0A        /**%0A    
     
@@ -3601,222 +3601,510 @@
     
+    
 */%0A    
-static void skipControlCharacters(ChannelBuffer buffer) %7B%0A        for (;;) %7B%0A            char c = (char) buffer.readUnsignedByte();%0A            if (!Character.isISOControl(c) && !Character.isWhitespace(c)) %7B
+    SeekAheadOptimize(ChannelBuffer buffer)%0A                throws SeekAheadNoBackArray %7B%0A            if (! buffer.hasArray()) %7B%0A                throw new SeekAheadNoBackArray();%0A            %7D%0A            this.buffer = buffer;%0A            this.bytes = buffer.array();%0A            this.pos = this.readerIndex = buffer.readerIndex();%0A            this.limit = buffer.writerIndex();%0A        %7D%0A        /**%0A         * %0A         * @param minus this value will be used as (currentPos - minus) to set
 %0A   
@@ -4113,97 +4113,403 @@
     
+*
       
- buffer.readerIndex(buffer.readerIndex() - 1);%0A                break;%0A            %7D
+the current readerIndex in the buffer. %0A         */%0A        void setReadPosition(int minus) %7B%0A            pos -= minus;%0A            readerIndex = pos;%0A            buffer.readerIndex(readerIndex);%0A        %7D%0A        %0A        void clear() %7B%0A            this.buffer = null;%0A            this.bytes = null;%0A            this.limit = 0;%0A            this.pos = 0;%0A            this.readerIndex = 0;
 %0A   

Add the SeekAheadOptimize class to enable faster seek of bytes values in HttpPostRequestDecoder