use of java.io.UTFDataFormatException in project geode by apache.
the class CacheServerHelper method fromUTF.
/**
* The logic used here is based on java's DataInputStream.readUTF() from the version 1.6.0_10.
*
* @param bytearr
* @return String
*/
public static String fromUTF(byte[] bytearr) {
int utflen = bytearr.length;
int c, char2, char3;
int count = 0;
int chararr_count = 0;
char[] chararr = new char[utflen];
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 RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), new UTFDataFormatException("malformed input: partial character at end"));
}
char2 = (int) bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
throw new RuntimeException("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 RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), 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 RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), 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 RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), new UTFDataFormatException("malformed input around byte " + count));
}
}
// The number of chars produced may be less than utflen
return new String(chararr, 0, chararr_count);
}
use of java.io.UTFDataFormatException in project incubator-systemml by apache.
the class CacheDataOutput method writeUTF.
@Override
public void writeUTF(String s) throws IOException {
int slen = s.length();
int utflen = IOUtilFunctions.getUTFSize(s) - 2;
if (utflen - 2 > 65535)
throw new UTFDataFormatException("encoded string too long: " + utflen);
// write utf len (2 bytes)
writeShort(utflen);
// write utf payload
for (int i = 0; i < slen; i++) {
char c = s.charAt(i);
if (// 1 byte range
c >= 0x0001 && c <= 0x007F)
writeByte(c);
else if (c >= 0x0800) {
// 3 byte range
_buff[_count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
_buff[_count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
_buff[_count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
} else {
// 2 byte range and null
_buff[_count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
_buff[_count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
}
use of java.io.UTFDataFormatException in project incubator-systemml by apache.
the class FastBufferedDataOutputStream method writeUTF.
@Override
public void writeUTF(String s) throws IOException {
int slen = s.length();
int utflen = IOUtilFunctions.getUTFSize(s) - 2;
if (utflen - 2 > 65535)
throw new UTFDataFormatException("encoded string too long: " + utflen);
// write utf len (2 bytes)
writeShort(utflen);
// write utf payload
for (int i = 0; i < slen; i++) {
if (_count + 3 > _bufflen)
flushBuffer();
char c = s.charAt(i);
if (// 1 byte range
c >= 0x0001 && c <= 0x007F)
_buff[_count++] = (byte) c;
else if (c >= 0x0800) {
// 3 byte range
_buff[_count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
_buff[_count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
_buff[_count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
} else {
// 2 byte range and null
_buff[_count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
_buff[_count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
}
use of java.io.UTFDataFormatException in project JGroups by belaban.
the class ByteArrayDataInputStream method readUTF.
public String readUTF() throws IOException {
int utflen = readUnsignedShort();
byte[] bytearr = new byte[utflen];
char[] chararr = new char[utflen];
if (((short) utflen) == -1)
return null;
int c, char2, char3;
int count = 0;
int chararr_count = 0;
readFully(bytearr, 0, utflen);
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);
}
use of java.io.UTFDataFormatException in project hive by apache.
the class NonSyncDataInputBuffer method convertUTF8WithBuf.
public static String convertUTF8WithBuf(byte[] buf, char[] out, int offset, int utfSize) throws UTFDataFormatException {
int count = 0, s = 0, a;
while (count < utfSize) {
if ((out[s] = (char) buf[offset + count++]) < '\u0080') {
s++;
} else if (((a = out[s]) & 0xe0) == 0xc0) {
if (count >= utfSize) {
throw new UTFDataFormatException();
}
int b = buf[count++];
if ((b & 0xC0) != 0x80) {
throw new UTFDataFormatException();
}
out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
} else if ((a & 0xf0) == 0xe0) {
if (count + 1 >= utfSize) {
throw new UTFDataFormatException();
}
int b = buf[count++];
int c = buf[count++];
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
throw new UTFDataFormatException();
}
out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
} else {
throw new UTFDataFormatException();
}
}
return new String(out, 0, s);
}
Aggregations