Example usage for java.io UTFDataFormatException UTFDataFormatException

List of usage examples for java.io UTFDataFormatException UTFDataFormatException

Introduction

In this page you can find the example usage for java.io UTFDataFormatException UTFDataFormatException.

Prototype

public UTFDataFormatException() 

Source Link

Document

Constructs a UTFDataFormatException with null as its error detail message.

Usage

From source file:ArrayByte.java

public String readUTF() throws IndexOutOfBoundsException, UTFDataFormatException {
    checkAvailable(2);//from   w  w  w.ja v a2  s  .c o  m
    int utfLength = readShort() & 0xffff;
    checkAvailable(utfLength);

    int goalPosition = position() + utfLength;

    StringBuffer string = new StringBuffer(utfLength);
    while (position() < goalPosition) {
        int a = readByte() & 0xff;
        if ((a & 0x80) == 0) {
            string.append((char) a);
        } else {
            int b = readByte() & 0xff;
            if ((b & 0xc0) != 0x80) {
                throw new UTFDataFormatException();
            }

            if ((a & 0xe0) == 0xc0) {
                char ch = (char) (((a & 0x1f) << 6) | (b & 0x3f));
                string.append(ch);
            } else if ((a & 0xf0) == 0xe0) {
                int c = readByte() & 0xff;
                if ((c & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                char ch = (char) (((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f));
                string.append(ch);
            } else {
                throw new UTFDataFormatException();
            }
        }
    }
    return string.toString();
}

From source file:ArrayByte.java

public void writeUTF(String s) throws UTFDataFormatException {

    int utfLength = 0;
    for (int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
        if (ch > 0 && ch < 0x80) {
            utfLength++;/*  w w  w  . j  a va  2 s .c o  m*/
        } else if (ch == 0 || (ch >= 0x80 && ch < 0x800)) {
            utfLength += 2;
        } else {
            utfLength += 3;
        }
    }

    if (utfLength > 65535) {
        throw new UTFDataFormatException();
    }

    ensureCapacity(2 + utfLength);
    writeShort(utfLength);

    for (int i = 0; i < s.length(); i++) {
        int ch = s.charAt(i);
        if (ch > 0 && ch < 0x80) {
            writeByte(ch);
        } else if (ch == 0 || (ch >= 0x80 && ch < 0x800)) {
            writeByte(0xc0 | (0x1f & (ch >> 6)));
            writeByte(0x80 | (0x3f & ch));
        } else {
            writeByte(0xe0 | (0x0f & (ch >> 12)));
            writeByte(0x80 | (0x3f & (ch >> 6)));
            writeByte(0x80 | (0x3f & ch));
        }
    }
}

From source file:com.exadel.flamingo.flex.messaging.amf.io.AMF0Deserializer.java

/**
 * This is a hacked verison of Java's DataInputStream.readUTF(), which only
 * supports Strings <= 65535 UTF-8-encoded characters
 *///w w w  . j ava  2 s .  c o  m
private Object readLongUTF(DataInputStream in) throws IOException {
    int utflen = in.readInt();
    StringBuffer str = new StringBuffer(utflen);
    byte bytearr[] = new byte[utflen];
    int c, char2, char3;
    int count = 0;

    in.readFully(bytearr, 0, utflen);

    while (count < utflen) {
        c = bytearr[count] & 0xff;
        switch (c >> 4) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            /* 0xxxxxxx*/
            count++;
            str.append((char) c);
            break;
        case 12:
        case 13:
            /* 110x xxxx   10xx xxxx*/
            count += 2;
            if (count > utflen)
                throw new UTFDataFormatException();
            char2 = bytearr[count - 1];
            if ((char2 & 0xC0) != 0x80)
                throw new UTFDataFormatException();
            str.append((char) (((c & 0x1F) << 6) | (char2 & 0x3F)));
            break;
        case 14:
            /* 1110 xxxx  10xx xxxx  10xx xxxx */
            count += 3;
            if (count > utflen)
                throw new UTFDataFormatException();
            char2 = bytearr[count - 2];
            char3 = bytearr[count - 1];
            if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
                throw new UTFDataFormatException();
            str.append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
            break;
        default:
            /* 10xx xxxx,  1111 xxxx */
            throw new UTFDataFormatException();
        }
    }

    // The number of chars produced may be less than utflen
    return new String(str);
}

From source file:org.openamf.io.AMFDeserializer.java

/**
 * This is a hacked verison of Java's DataInputStream.readUTF(), which only
 * supports Strings <= 65535 UTF-8-encoded characters
 *///from   ww  w.  jav  a  2  s. c o  m
private Object readLongUTF(DataInputStream in) throws IOException {
    int utflen = in.readInt();
    StringBuffer str = new StringBuffer(utflen);
    byte bytearr[] = new byte[utflen];
    int c, char2, char3;
    int count = 0;

    in.readFully(bytearr, 0, utflen);

    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++;
            str.append((char) c);
            break;
        case 12:
        case 13:
            /* 110x xxxx   10xx xxxx*/
            count += 2;
            if (count > utflen)
                throw new UTFDataFormatException();
            char2 = (int) bytearr[count - 1];
            if ((char2 & 0xC0) != 0x80)
                throw new UTFDataFormatException();
            str.append((char) (((c & 0x1F) << 6) | (char2 & 0x3F)));
            break;
        case 14:
            /* 1110 xxxx  10xx xxxx  10xx xxxx */
            count += 3;
            if (count > utflen)
                throw new UTFDataFormatException();
            char2 = (int) bytearr[count - 2];
            char3 = (int) bytearr[count - 1];
            if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
                throw new UTFDataFormatException();
            str.append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
            break;
        default:
            /* 10xx xxxx,  1111 xxxx */
            throw new UTFDataFormatException();
        }
    }

    // The number of chars produced may be less than utflen
    return new String(str);
}