use of java.io.UTFDataFormatException in project rabbitmq-jms-client by rabbitmq.
the class RMQStreamMessage method readPrimitiveType.
private Object readPrimitiveType(Class<?> type) throws JMSException {
if (!this.reading)
throw new MessageNotReadableException(NOT_READABLE);
if (this.readbuf != null) {
throw new MessageFormatException("You must call 'int readBytes(byte[])' since the buffer is not empty");
}
boolean success = true;
try {
this.bin.mark(0);
Object o = RMQMessage.readPrimitive(in);
if (o instanceof byte[]) {
if (type == ByteArray.class || type == Object.class) {
return o;
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte[]"));
}
} else if (type == ByteArray.class) {
if (o == null) {
return null;
}
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte[]"));
} else if (type == Boolean.class) {
if (o == null) {
return Boolean.FALSE;
} else if (o instanceof Boolean) {
return o;
} else if (o instanceof String) {
return Boolean.parseBoolean((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "boolean"));
}
} else if (type == Byte.class) {
if (o instanceof Byte) {
return o;
} else if (o instanceof String) {
return Byte.parseByte((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte"));
}
} else if (type == Short.class) {
if (o instanceof Byte) {
return (short) (Byte) o;
} else if (o instanceof Short) {
return o;
} else if (o instanceof String) {
return Short.parseShort((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte"));
}
} else if (type == Integer.class) {
if (o instanceof Byte) {
return (int) (Byte) o;
} else if (o instanceof Short) {
return (int) (Short) o;
} else if (o instanceof Integer) {
return o;
} else if (o instanceof String) {
return Integer.parseInt((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "int"));
}
} else if (type == Character.class) {
if (o instanceof Character) {
return o;
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "char"));
}
} else if (type == Long.class) {
if (o instanceof Byte) {
return (long) (Byte) o;
} else if (o instanceof Short) {
return (long) (Short) o;
} else if (o instanceof Integer) {
return (long) (Integer) o;
} else if (o instanceof Long) {
return o;
} else if (o instanceof String) {
return Long.parseLong((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "long"));
}
} else if (type == Float.class) {
if (o instanceof Float) {
return (Float) o;
} else if (o instanceof String) {
return Float.parseFloat((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "float"));
}
} else if (type == Double.class) {
if (o instanceof Float) {
return (double) (Float) o;
} else if (o instanceof Double) {
return (Double) o;
} else if (o instanceof String) {
return Double.parseDouble((String) o);
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "double"));
}
} else if (type == String.class) {
if (o == null) {
return null;
} else if (o instanceof byte[]) {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "String"));
} else {
return o.toString();
}
} else if (type == Object.class) {
return o;
} else {
throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, type.toString()));
}
} catch (NumberFormatException x) {
success = false;
throw x;
} catch (ClassNotFoundException x) {
success = false;
throw new RMQJMSException(x);
} catch (EOFException x) {
success = false;
throw new MessageEOFException(MSG_EOF);
} catch (UTFDataFormatException x) {
success = false;
throw new RMQMessageFormatException(x);
} catch (IOException x) {
success = false;
throw new RMQJMSException(x);
} catch (Exception x) {
success = false;
if (x instanceof JMSException) {
throw (JMSException) x;
} else {
throw new RMQJMSException(x);
}
} finally {
if (!success) {
this.bin.reset();
}
}
}
use of java.io.UTFDataFormatException in project invesdwin-util by invesdwin.
the class InputStreams method readUTF.
/**
* Reads from the stream {@code in} a representation of a Unicode character string encoded in
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format; this string of characters is then returned as
* a {@code String}. The details of the modified UTF-8 representation are exactly the same as for the
* {@code readUTF} method of {@code DataInput}.
*
* @param in
* a data input stream.
* @return a Unicode string.
* @throws EOFException
* if the input stream reaches the end before all the bytes.
* @throws IOException
* the stream has been closed and the contained input stream does not support reading after close, or
* another I/O error occurs.
* @throws UTFDataFormatException
* if the bytes do not represent a valid modified UTF-8 encoding of a Unicode string.
* @see java.io.DataInputStream#readUnsignedShort()
*/
public static String readUTF(final InputStream in) throws IOException {
final int utflen = readUnsignedShort(in);
final IByteBuffer bytearr = ByteBuffers.EXPANDABLE_POOL.borrowObject().ensureCapacity(utflen);
try {
final char[] chararr = new char[utflen];
int c, char2, char3;
int count = 0;
int chararr_count = 0;
bytearr.putBytesTo(0, in, utflen);
while (count < utflen) {
c = bytearr.getByte(count) & 0xff;
if (c > 127) {
break;
}
count++;
chararr[chararr_count++] = (char) c;
}
while (count < utflen) {
c = bytearr.getByte(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 = bytearr.getByte(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 = bytearr.getByte(count - 2);
char3 = bytearr.getByte(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);
} finally {
ByteBuffers.EXPANDABLE_POOL.returnObject(bytearr);
}
}
use of java.io.UTFDataFormatException in project openj9 by eclipse-openj9.
the class Util 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)
/*[MSG "K0062", "Second byte at {0} does not match UTF8 Specification"]*/
throw // $NON-NLS-1$
new UTFDataFormatException(com.ibm.oti.util.Msg.getString("K0062", count));
int b = buf[count++];
if ((b & 0xC0) != 0x80)
/*[MSG "K0062", "Second byte at {0} does not match UTF8 Specification"]*/
throw // $NON-NLS-1$
new UTFDataFormatException(com.ibm.oti.util.Msg.getString("K0062", (count - 1)));
out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
} else if ((a & 0xf0) == 0xe0) {
if (count + 1 >= utfSize)
/*[MSG "K0063", "Third byte at {0} does not match UTF8 Specification"]*/
throw // $NON-NLS-1$
new UTFDataFormatException(com.ibm.oti.util.Msg.getString("K0063", (count + 1)));
int b = buf[count++];
int c = buf[count++];
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80))
/*[MSG "K0064", "Second or third byte at {0} does not match UTF8 Specification"]*/
throw // $NON-NLS-1$
new UTFDataFormatException(com.ibm.oti.util.Msg.getString("K0064", (count - 2)));
out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
} else {
// $NON-NLS-1$
throw new UTFDataFormatException(com.ibm.oti.util.Msg.getString("K0065", (count - 1)));
}
}
return new String(out, 0, s);
}
use of java.io.UTFDataFormatException in project xmrwallet by m2049r.
the class LittleEndianDataInputStream method readUTF.
/**
* Reads from the stream <code>in</code> a representation of a Unicode
* character string encoded in <a
* href="DataInput.html#modified-utf-8">modified UTF-8</a> format; this
* string of characters is then returned as a <code>String</code>. The
* details of the modified UTF-8 representation are exactly the same as for
* the <code>readUTF</code> method of <code>DataInput</code>.
*
* @param in a data input stream.
* @return a Unicode string.
* @throws EOFException if the input stream reaches the end before all the bytes.
* @throws IOException the stream has been closed and the contained input stream
* does not support reading after close, or another I/O error
* occurs.
* @throws UTFDataFormatException if the bytes do not represent a valid modified UTF-8
* encoding of a Unicode string.
* @see java.io.DataInputStream#readUnsignedShort()
*/
public static final String readUTF(DataInput in) throws IOException {
int utflen = in.readUnsignedShort();
byte[] bytearr = null;
char[] chararr = null;
if (in instanceof LittleEndianDataInputStream) {
LittleEndianDataInputStream dis = (LittleEndianDataInputStream) in;
if (dis.bytearr.length < utflen) {
dis.bytearr = new byte[utflen * 2];
dis.chararr = new char[utflen * 2];
}
chararr = dis.chararr;
bytearr = dis.bytearr;
} else {
bytearr = new byte[utflen];
chararr = new char[utflen];
}
int c, char2, char3;
int count = 0;
int chararr_count = 0;
in.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 rsdb by environmentalinformatics-marburg.
the class LittleEndianDataOutputStream method writeUTF.
/**
* Writes a string to the specified DataOutput using
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
* encoding in a machine-independent manner.
* <p>
* First, two bytes are written to out as if by the <code>writeShort</code>
* method giving the number of bytes to follow. This value is the number of
* bytes actually written out, not the length of the string. Following the
* length, each character of the string is output, in sequence, using the
* modified UTF-8 encoding for the character. If no exception is thrown, the
* counter <code>written</code> is incremented by the total number of
* bytes written to the output stream. This will be at least two
* plus the length of <code>str</code>, and at most two plus
* thrice the length of <code>str</code>.
*
* @param str a string to be written.
* @param out destination to write to
* @return The number of bytes written out.
* @exception IOException if an I/O error occurs.
*/
static int writeUTF(String str, DataOutput out) 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 = null;
if (out instanceof LittleEndianDataOutputStream) {
LittleEndianDataOutputStream dos = (LittleEndianDataOutputStream) out;
if (dos.bytearr == null || (dos.bytearr.length < (utflen + 2)))
dos.bytearr = new byte[(utflen * 2) + 2];
bytearr = dos.bytearr;
} else {
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));
}
}
out.write(bytearr, 0, utflen + 2);
return utflen + 2;
}
Aggregations