java.io.UTFDataFormatException Java Examples
The following examples show how to use
java.io.UTFDataFormatException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Mutf8.java From buck with Apache License 2.0 | 6 votes |
/** * Returns the number of bytes the modified UTF8 representation of 's' would take. */ private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException { long result = 0; final int length = s.length(); for (int i = 0; i < length; ++i) { char ch = s.charAt(i); if (ch != 0 && ch <= 127) { // U+0000 uses two bytes. ++result; } else if (ch <= 2047) { result += 2; } else { result += 3; } if (shortLength && result > 65535) { throw new UTFDataFormatException("String more than 65535 UTF bytes long"); } } return result; }
Example #2
Source File: Mutf8.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Returns the number of bytes the modified UTF8 representation of 's' would take. */ private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException { long result = 0; final int length = s.length(); for (int i = 0; i < length; ++i) { char ch = s.charAt(i); if (ch != 0 && ch <= 127) { // U+0000 uses two bytes. ++result; } else if (ch <= 2047) { result += 2; } else { result += 3; } if (shortLength && result > 65535) { throw new UTFDataFormatException("String more than 65535 UTF bytes long"); } } return result; }
Example #3
Source File: Dex.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
public String readString() { int offset = readInt(); int savedPosition = data.position(); int savedLimit = data.limit(); data.position(offset); data.limit(data.capacity()); try { int expectedLength = readUleb128(); String result = Mutf8.decode(this, new char[expectedLength]); if (result.length() != expectedLength) { throw new DexException("Declared length " + expectedLength + " doesn't match decoded length of " + result.length()); } return result; } catch (UTFDataFormatException e) { throw new DexException(e); } finally { data.position(savedPosition); data.limit(savedLimit); } }
Example #4
Source File: IOUtil.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * private utility method to convert a byte[] to String * * @param bytearre * @throws UTFDataFormatException */ private static String convertBytes2String( byte[] bytearr ) throws UTFDataFormatException { int utflen = bytearr.length; char[] chararr = new char[utflen]; int c; int chararr_count = 0; int count = 0; while ( count < utflen ) { c = (int) bytearr[count] & 0xff; if ( c > 127 ) break; count++; chararr[chararr_count++] = (char) c; } chararr_count = generateCharArray( chararr, bytearr, count, chararr_count ); // The number of chars produced may be less than utflen return new String( chararr, 0, chararr_count ); }
Example #5
Source File: Dex.java From Box with Apache License 2.0 | 6 votes |
public String readString() { int offset = readInt(); int savedPosition = data.position(); int savedLimit = data.limit(); data.position(offset); data.limit(data.capacity()); try { int expectedLength = readUleb128(); String result = Mutf8.decode(this, new char[expectedLength]); if (result.length() != expectedLength) { throw new DexException("Declared length " + expectedLength + " doesn't match decoded length of " + result.length()); } return result; } catch (UTFDataFormatException e) { throw new DexException(e); } finally { data.position(savedPosition); data.limit(savedLimit); } }
Example #6
Source File: UTF8UtilTest.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Demonstrates that skipping incorrectly encoded character sequences * works because the stream is not checked for well-formedness. */ public void testSkippingInvalidEncodingWorks() throws IOException { // The array contains three valid characters and one invalid three-byte // representation that only has two bytes present. // When skipping, this sequence is (incorrectly) taken as a sequence of // three characters ('a' - some three byte character - 'a'). // 0xef = 11101111, 0xb8 = 10111000 byte[] data = {'a', (byte)0xef, (byte)0xb8, 'a', 'a'}; byte[] dataWithLength = {0x0, 0x5, 'a', (byte)0xef, (byte)0xb8, 'a', 'a'}; InputStream is = new ByteArrayInputStream(data); // This is actually incorrect, but does work currently. UTF8Util.skipFully(is, 3); // Verify that decoding this actually fails. DataInputStream dis = new DataInputStream( new ByteArrayInputStream(dataWithLength)); try { dis.readUTF(); fail("UTF-8 expected to be invalid, read should fail"); } catch (UTFDataFormatException udfe) { // This is expected, since the UTF-8 encoding is invalid } }
Example #7
Source File: Mutf8.java From Box with Apache License 2.0 | 6 votes |
/** * Returns the number of bytes the modified UTF8 representation of 's' would take. */ private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException { long result = 0; final int length = s.length(); for (int i = 0; i < length; ++i) { char ch = s.charAt(i); if (ch != 0 && ch <= 127) { // U+0000 uses two bytes. ++result; } else if (ch <= 2047) { result += 2; } else { result += 3; } if (shortLength && result > 65535) { throw new UTFDataFormatException("String more than 65535 UTF bytes long"); } } return result; }
Example #8
Source File: UTF8.java From Dexer with Apache License 2.0 | 6 votes |
/** * Returns the number of bytes the modified UTF8 representation of 's' would take. */ private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException { long result = 0; final int length = s.length(); for (int i = 0; i < length; ++i) { char ch = s.charAt(i); if (ch != 0 && ch <= 127) { // U+0000 uses two bytes. ++result; } else if (ch <= 2047) { result += 2; } else { result += 3; } if (shortLength && result > 65535) { throw new UTFDataFormatException("String more than 65535 UTF bytes long"); } } return result; }
Example #9
Source File: ClassFileDeserializer.java From jd-core with GNU General Public License v3.0 | 6 votes |
protected ClassFile loadClassFile(ClassFileReader reader) throws UTFDataFormatException { int magic = reader.readInt(); if (magic != ClassFileReader.JAVA_MAGIC_NUMBER) throw new ClassFileFormatException("Invalid CLASS file"); int minorVersion = reader.readUnsignedShort(); int majorVersion = reader.readUnsignedShort(); ConstantPool constants = new ConstantPool(loadConstants(reader)); int accessFlags = reader.readUnsignedShort(); int thisClassIndex = reader.readUnsignedShort(); int superClassIndex = reader.readUnsignedShort(); String internalTypeName = constants.getConstantTypeName(thisClassIndex); String superTypeName = (superClassIndex == 0) ? null : constants.getConstantTypeName(superClassIndex); String[] interfaceTypeNames = loadInterfaces(reader, constants); Field[] fields = loadFields(reader, constants); Method[] methods = loadMethods(reader, constants); HashMap<String, Attribute> attributes = loadAttributes(reader, constants); return new ClassFile(majorVersion, minorVersion, accessFlags, internalTypeName, superTypeName, interfaceTypeNames, fields, methods, attributes); }
Example #10
Source File: Dex.java From buck with Apache License 2.0 | 6 votes |
public String readString() { int offset = readInt(); int savedPosition = data.position(); int savedLimit = data.limit(); data.position(offset); data.limit(data.capacity()); try { int expectedLength = readUleb128(); String result = Mutf8.decode(this, new char[expectedLength]); if (result.length() != expectedLength) { throw new DexException("Declared length " + expectedLength + " doesn't match decoded length of " + result.length()); } return result; } catch (UTFDataFormatException e) { throw new DexException(e); } finally { data.position(savedPosition); data.limit(savedLimit); } }
Example #11
Source File: InstantCoder.java From beam with Apache License 2.0 | 6 votes |
@Override public Instant decode(InputStream inStream) throws CoderException, IOException { long shiftedMillis; try { shiftedMillis = new DataInputStream(inStream).readLong(); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } // Produces an {@link Instant} from a {@code long} representing its millis-since-epoch, // but shifted so that the byte representation of negative values are lexicographically // ordered before the byte representation of positive values. // // This deliberately utilizes the well-defined overflow for {@code long} values. // See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2 return new Instant(shiftedMillis + Long.MIN_VALUE); }
Example #12
Source File: Dex.java From Box with Apache License 2.0 | 6 votes |
public String readString() { int offset = readInt(); int savedPosition = data.position(); int savedLimit = data.limit(); data.position(offset); data.limit(data.capacity()); try { int expectedLength = readUleb128(); String result = Mutf8.decode(this, new char[expectedLength]); if (result.length() != expectedLength) { throw new DexException("Declared length " + expectedLength + " doesn't match decoded length of " + result.length()); } return result; } catch (UTFDataFormatException e) { throw new DexException(e); } finally { data.position(savedPosition); data.limit(savedLimit); } }
Example #13
Source File: Mutf8.java From aapt with Apache License 2.0 | 6 votes |
/** * Returns the number of bytes the modified UTF8 representation of 's' would take. */ private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException { long result = 0; final int length = s.length(); for (int i = 0; i < length; ++i) { char ch = s.charAt(i); if (ch != 0 && ch <= 127) { // U+0000 uses two bytes. ++result; } else if (ch <= 2047) { result += 2; } else { result += 3; } if (shortLength && result > 65535) { throw new UTFDataFormatException("String more than 65535 UTF bytes long"); } } return result; }
Example #14
Source File: VarLongCoder.java From beam with Apache License 2.0 | 5 votes |
@Override public Long decode(InputStream inStream) throws IOException, CoderException { try { return VarInt.decodeLong(inStream); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
Example #15
Source File: DecodeTask.java From Dexer with Apache License 2.0 | 5 votes |
private static String getString(int id) { if (id == -1) { return null; } int offset = stringId.getInt(id * 4); stringData.position(offset); int length = readULeb128(stringData); try { StringBuilder buff = new StringBuilder(length); return UTF8.decode(stringData, buff).trim(); } catch (UTFDataFormatException e) { e.printStackTrace(); return ""; } }
Example #16
Source File: UTF8Reader.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** Throws an exception for invalid surrogate bits. */ private void invalidSurrogate(int uuuuu) throws UTFDataFormatException { throw new UTFDataFormatException( Localizer.getMessage("jsp.error.xml.invalidHighSurrogate", Integer.toHexString(uuuuu))); }
Example #17
Source File: DataOutputStreamTest.java From j2objc with Apache License 2.0 | 5 votes |
public void test_writeUTF_too_long() throws Exception { String tooLong = new String(new char[65536]); try { os.writeUTF(tooLong); fail("should throw UTFDataFormatException"); } catch (UTFDataFormatException expected) { } assertEquals("[]", toHexString(bytes.toByteArray())); }
Example #18
Source File: UTF8UtilTest.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Tries to skip characters where the data is incomplete. * <p> * In this test, the encoding states there is a character represented by * three bytes present. However, only one byte is provided. */ public void testMissingSecondByteOfThree() throws IOException { // 0xef = 11101111 byte[] data = {'a', (byte)0xef}; InputStream is = new ByteArrayInputStream(data); try { UTF8Util.skipFully(is, 2); fail("Reading invalid UTF-8 should fail"); } catch (UTFDataFormatException udfe) { // As expected } }
Example #19
Source File: ByteCoder.java From beam with Apache License 2.0 | 5 votes |
@Override public Byte decode(InputStream inStream) throws IOException, CoderException { try { // value will be between 0-255, -1 for EOF int value = inStream.read(); if (value == -1) { throw new EOFException("EOF encountered decoding 1 byte from input stream"); } return (byte) value; } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
Example #20
Source File: ImageStringsReader.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static void charsFromMUTF8(char[] chars, byte[] bytes, int offset, int count) throws UTFDataFormatException { int j = 0; for (int i = offset; i < offset + count; i++) { byte ch = bytes[i]; if (ch == 0) { break; } boolean is_unicode = (ch & 0x80) != 0; int uch = ch & 0x7F; if (is_unicode) { int mask = 0x40; while ((uch & mask) != 0) { ch = bytes[++i]; if ((ch & 0xC0) != 0x80) { throw new UTFDataFormatException("bad continuation 0x" + Integer.toHexString(ch)); } uch = ((uch & ~mask) << 6) | (ch & 0x3F); mask <<= 6 - 1; } if ((uch & 0xFFFF) != uch) { throw new UTFDataFormatException("character out of range \\u" + Integer.toHexString(uch)); } } chars[j++] = (char)uch; } }
Example #21
Source File: UTF8Reader.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** Throws an exception for expected byte. */ private void expectedByte(int position, int count) throws UTFDataFormatException { throw new UTFDataFormatException( Localizer.getMessage("jsp.error.xml.expectedByte", Integer.toString(position), Integer.toString(count))); }
Example #22
Source File: Mutf8.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
/** * Returns an array containing the <i>modified UTF-8</i> form of {@code s}. */ public static byte[] encode(String s) throws UTFDataFormatException { int utfCount = (int) countBytes(s, true); byte[] result = new byte[utfCount]; encode(result, 0, s); return result; }
Example #23
Source File: FrameLeaf.java From uima-uimaj with Apache License 2.0 | 5 votes |
/** * Converts the UTF-8 data to a Java boolean. * * @exception LeafCastException * if the underlying data was not utf-8 (which in general should not happen). * @return - */ public boolean toBoolean() { try { return UTFConverter.convertUTFToBool(data); } catch (UTFDataFormatException e) { throw new LeafCastException(NOT_UTF8_ERROR); } }
Example #24
Source File: SchemaRecordWriter.java From localization_nifi with Apache License 2.0 | 5 votes |
private void writeUTFLimited(final DataOutputStream out, final String utfString, final String fieldName) throws IOException { try { out.writeUTF(utfString); } catch (UTFDataFormatException e) { final String truncated = utfString.substring(0, getCharsInUTF8Limit(utfString, MAX_ALLOWED_UTF_LENGTH)); logger.warn("Truncating repository record value for field '{}'! Attempted to write {} chars that encode to a UTF8 byte length greater than " + "supported maximum ({}), truncating to {} chars.", (fieldName == null) ? "" : fieldName, utfString.length(), MAX_ALLOWED_UTF_LENGTH, truncated.length()); if (logger.isDebugEnabled()) { logger.warn("String value was:\n{}", truncated); } out.writeUTF(truncated); } }
Example #25
Source File: Mutf8.java From atlas with Apache License 2.0 | 5 votes |
/** * Returns an array containing the <i>modified UTF-8</i> form of {@code s}. */ public static byte[] encode(String s) throws UTFDataFormatException { int utfCount = (int) countBytes(s, true); byte[] result = new byte[utfCount]; encode(result, 0, s); return result; }
Example #26
Source File: ImageStringsReader.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static String stringFromMUTF8(byte[] bytes, int offset, int count) { int length = charsFromMUTF8Length(bytes, offset, count); char[] chars = new char[length]; try { charsFromMUTF8(chars, bytes, offset, count); } catch (UTFDataFormatException ex) { throw new InternalError("Attempt to convert non modified UTF-8 byte sequence", ex); } return new String(chars); }
Example #27
Source File: Dex.java From atlas with Apache License 2.0 | 5 votes |
public void writeStringData(String value) { try { int length = value.length(); writeUleb128(length); write(Mutf8.encode(value)); writeByte(0); } catch (UTFDataFormatException e) { throw new AssertionError(); } }
Example #28
Source File: UTF8UtilTest.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Tries to skip characters where the data is incomplete. * <p> * In this test, the encoding states there is a character represented by * two bytes present. However, only one byte is provided. */ public void testMissingSecondByteOfTwo() throws IOException { // 0xdf = 11011111 byte[] data = {'a', (byte)0xdf}; InputStream is = new ByteArrayInputStream(data); try { UTF8Util.skipFully(is, 2); fail("Reading invalid UTF-8 should fail"); } catch (UTFDataFormatException udfe) { // As expected } }
Example #29
Source File: Mutf8.java From atlas with Apache License 2.0 | 5 votes |
/** * Returns an array containing the <i>modified UTF-8</i> form of {@code s}. */ public static byte[] encode(String s) throws UTFDataFormatException { int utfCount = (int) countBytes(s, true); byte[] result = new byte[utfCount]; encode(result, 0, s); return result; }
Example #30
Source File: UTF8Reader.java From packagedrone with Eclipse Public License 1.0 | 5 votes |
/** Throws an exception for invalid surrogate bits. */ private void invalidSurrogate(int uuuuu) throws UTFDataFormatException { throw new UTFDataFormatException( Localizer.getMessage("jsp.error.xml.invalidHighSurrogate", Integer.toHexString(uuuuu))); }