Search in sources :

Example 26 with UTFDataFormatException

use of java.io.UTFDataFormatException in project JGroups by belaban.

the class ByteBufferOutputStream method writeUTF.

public void writeUTF(String str) throws IOException {
    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 + 2];
    bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
    bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
    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));
        }
    }
    write(bytearr, 0, utflen + 2);
}
Also used : UTFDataFormatException(java.io.UTFDataFormatException)

Example 27 with UTFDataFormatException

use of java.io.UTFDataFormatException in project J2ME-Loader by nikita36078.

the class Mutf8 method decode.

/**
 * Decodes bytes from {@code in} into {@code out} until a delimiter 0x00 is
 * encountered. Returns a new string containing the decoded characters.
 */
public static String decode(ByteInput in, char[] out) throws UTFDataFormatException {
    int s = 0;
    while (true) {
        char a = (char) (in.readByte() & 0xff);
        if (a == 0) {
            return new String(out, 0, s);
        }
        out[s] = a;
        if (a < '\u0080') {
            s++;
        } else if ((a & 0xe0) == 0xc0) {
            int b = in.readByte() & 0xff;
            if ((b & 0xC0) != 0x80) {
                throw new UTFDataFormatException("bad second byte");
            }
            out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
        } else if ((a & 0xf0) == 0xe0) {
            int b = in.readByte() & 0xff;
            int c = in.readByte() & 0xff;
            if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
                throw new UTFDataFormatException("bad second or third byte");
            }
            out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
        } else {
            throw new UTFDataFormatException("bad byte");
        }
    }
}
Also used : UTFDataFormatException(java.io.UTFDataFormatException)

Aggregations

UTFDataFormatException (java.io.UTFDataFormatException)27 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Nullable (android.support.annotation.Nullable)1 DexException (com.tencent.tinker.android.dex.DexException)1 StringData (com.tencent.tinker.android.dex.StringData)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutput (java.io.DataOutput)1 EOFException (java.io.EOFException)1 File (java.io.File)1 ObjectStreamClass (java.io.ObjectStreamClass)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 InetAddress (java.net.InetAddress)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1