Java Byte Array Decode decodeRequestParameter(String string, String encoding, byte[] buffer)

Here you can find the source of decodeRequestParameter(String string, String encoding, byte[] buffer)

Description

Given a parameter string and the name of a character encoding, fixes the string.

License

Apache License

Parameter

Parameter Description
string the string
encoding the name of a character encoding
buffer an optional byte buffer, for reuse; pass null if you're not calling this function repeatedly

Declaration

static public final String decodeRequestParameter(String string,
        String encoding, byte[] buffer)
        throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*/*from   w ww.j a  v a 2  s.c o  m*/
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF 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.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Given a parameter string and the name of a character encoding,
     * fixes the string.  The Servlet API builds Strings for all submitted
     * data as if they were ISO-8859-1 encoded;  this function undoes
     * the damage.  Clients should pass in the same encoding that they
     * used for generating the page that the submission comes from.
     * <p>
     * @param string the string
     * @param encoding the name of a character encoding
     * @param buffer an optional byte buffer, for reuse;  pass null
     *    if you're not calling this function repeatedly
     */
    static public final String decodeRequestParameter(String string,
            String encoding, byte[] buffer)
            throws UnsupportedEncodingException {
        if (encoding == null)
            return string;
        if (string == null)
            return null;

        int stringLength = string.length();

        if ((buffer == null) || (stringLength > buffer.length))
            buffer = new byte[stringLength];

        // As documented, this function doesn't convert "properly"
        // from characters to bytes.  But it happens to do
        // exactly the conversion we want
        // -= Simon Lessard =- 
        // TODO: Wouldn't getBytes() do the trick?
        string.getBytes(0, stringLength, buffer, 0);

        return new String(buffer, 0, stringLength, encoding);
    }
}

Related

  1. decode(String encoding, byte[] bytes)
  2. decode(String encoding, byte[] data)
  3. decodeBytes(byte[] data, String encoding)
  4. decodeObject(byte[] bytes)
  5. decodeObject(final byte[] bytes)
  6. decodeString(byte[] bytearr)
  7. decodeString(byte[] data)
  8. decodeString(byte[] data)
  9. decodeString(byte[] stringBytes)