Java Encode encodeAsModifiedUTF8(String str)

Here you can find the source of encodeAsModifiedUTF8(String str)

Description

encode As Modified UTF

License

Apache License

Parameter

Parameter Description
str string to encode

Exception

Parameter Description
UTFDataFormatException if the string is too long

Return

byte encoding

Declaration

public static byte[] encodeAsModifiedUTF8(String str) throws UTFDataFormatException 

Method Source Code

//package com.java2s;
/**// w  w w  . j  av  a  2  s  .  c o  m
 * 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 str string to encode
     * @return byte encoding
     * @throws UTFDataFormatException if the string is too long
     */
    public static byte[] encodeAsModifiedUTF8(String str) throws UTFDataFormatException {
        // REVIEW wyliekir 2009-08-17 -- This was taken almost verbatim from
        // DataOutputStream.
        int strlen = str.length();
        int utflen = 0;
        int c, count = 0;

        /* use charAt instead of copying String to char array */
        for (int i = 0; i < strlen; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }
        if (utflen > 65535)
            throw new UTFDataFormatException("encoded string too long: " + utflen + " bytes");

        byte[] bytearr = new byte[utflen];

        int i = 0;
        for (i = 0; i < strlen; i++) {
            c = str.charAt(i);
            if (!((c >= 0x0001) && (c <= 0x007F)))
                break;
            bytearr[count++] = (byte) c;
        }

        for (; i < strlen; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                bytearr[count++] = (byte) c;

            } else if (c > 0x07FF) {
                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
                bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            } else {
                bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
                bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            }
        }
        assert count == utflen;
        return bytearr;
    }
}

Related

  1. encode2Encode(String original, String encode1, String encode2)
  2. encode_u8(String what)
  3. encodeAddresses(String string, String charset)
  4. encodeArguments(final String arg)
  5. encodeArray(String[] sourceArray, String sysCharset, String charset)
  6. encodeAttribute(String value)
  7. encodeBackslashAhead(char c, char next, Appendable buffer)
  8. encodeCodePoolData(InputStream input)
  9. encodedInputStreamReader(InputStream stream, String encoding)