Java Byte Array Decode decodeString(byte[] bytearr)

Here you can find the source of decodeString(byte[] bytearr)

Description

decode String

License

Apache License

Parameter

Parameter Description
bytearr byte data to decode

Exception

Parameter Description
UTFDataFormatException if the byte data is not valid UTF-8

Return

the decoded string

Declaration

public static String decodeString(byte[] bytearr) throws UTFDataFormatException 

Method Source Code

//package com.java2s;
/**// w w  w  .j a  v a  2  s  .  com
 * Copyright (C) 2009 - present by OpenGamma Inc. and other contributors.
 *
 * 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.
 */

import java.io.UTFDataFormatException;

public class Main {
    /**
     * @param bytearr byte data to decode
     * @return the decoded string
     * @throws UTFDataFormatException if the byte data is not valid UTF-8
     */
    public static String decodeString(byte[] bytearr) throws UTFDataFormatException {
        int utflen = bytearr.length;
        char[] chararr = new char[utflen];
        int c, char2, char3;
        int count = 0;
        int chararr_count = 0;

        while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            if (c > 127)
                break;
            count++;
            chararr[chararr_count++] = (char) c;
        }

        while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                /* 0xxxxxxx*/
                count++;
                chararr[chararr_count++] = (char) c;
                break;
            case 12:
            case 13:
                /* 110x xxxx   10xx xxxx*/
                count += 2;
                if (count > utflen)
                    throw new UTFDataFormatException("malformed input: partial character at end");
                char2 = (int) bytearr[count - 1];
                if ((char2 & 0xC0) != 0x80)
                    throw new UTFDataFormatException("malformed input around byte " + count);
                chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                /* 1110 xxxx  10xx xxxx  10xx xxxx */
                count += 3;
                if (count > utflen)
                    throw new UTFDataFormatException("malformed input: partial character at end");
                char2 = (int) bytearr[count - 2];
                char3 = (int) bytearr[count - 1];
                if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
                    throw new UTFDataFormatException("malformed input around byte " + (count - 1));
                chararr[chararr_count++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6)
                        | ((char3 & 0x3F) << 0));
                break;
            default:
                /* 10xx xxxx,  1111 xxxx */
                throw new UTFDataFormatException("malformed input around byte " + count);
            }
        }
        // The number of chars produced may be less than utflen
        return new String(chararr, 0, chararr_count);
    }
}

Related

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