Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 SAP AG Licensed 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 com.sap.core.odata.core.commons;
Decodes a Java String containing a percent-encoded UTF-8 String value into a Java String (in its internal UTF-16 encoding).

Author(s):
SAP AG
public class Decoder {

  
Decodes a percent-encoded UTF-8 String value into a Java String (in its internal UTF-16 encoding).

Parameters:
value the encoded String
Returns:
the Java String
Throws:
java.lang.IllegalArgumentException if value contains characters not representing UTF-8 bytes or ends with an unfinished percent-encoded character
java.lang.NumberFormatException if the two characters after a percent character are not hexadecimal digits
  public static String decode(final String valuethrows IllegalArgumentExceptionNumberFormatException {
    if (value == null) {
      return value;
    }
    // Use a tiny finite-state machine to handle decoding on byte level.
    // There are only three states:
    //   -2: normal bytes
    //   -1: a byte representing the percent character has been read
    // >= 0: a byte representing the first half-byte of a percent-encoded byte has been read
    // The variable holding the state is also used to store the value of the first half-byte.
    byte[] result = new byte[value.length()];
    int position = 0;
    byte encodedPart = -2;
    for (final char c : value.toCharArray()) {
      if (c <= .) {
        if (c == '%') {
          if (encodedPart == -2) {
            encodedPart = -1;
          } else {
            throw new IllegalArgumentException();
          }
        } else if (encodedPart == -1) {
          encodedPart = (bytec;
        } else if (encodedPart >= 0) {
          final int i = Integer.parseInt(String.valueOf(new char[] { (charencodedPartc }), 16);
          if (i >= 0) {
            result[position++] = (bytei;
          } else {
            throw new NumberFormatException();
          }
          encodedPart = -2;
        } else {
          result[position++] = (bytec;
        }
      } else {
        throw new IllegalArgumentException();
      }
    }
    if (encodedPart >= 0) {
      throw new IllegalArgumentException();
    }
    try {
      return new String(result, 0, position"UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalArgumentException(e);
    }
  }
New to GrepCode? Check out our FAQ X