java类java.io.UTFDataFormatException的实例源码

Dex.java 文件源码 项目:multidex-maker 阅读 25 收藏 0 点赞 0 评论 0
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);
    }
}
Mutf8.java 文件源码 项目:multidex-maker 阅读 19 收藏 0 点赞 0 评论 0
/**
 * 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;
}
UTF8UtilTest.java 文件源码 项目:gemfirexd-oss 阅读 21 收藏 0 点赞 0 评论 0
/**
 * Tests that <code>skipFully</code> throws exception if there is a UTF-8
 * encoding error in the stream
 * 
 * @throws IOException if the test fails for some unexpected reason
 */
public void testSkipFullyOnInvalidStreamCJK()
        throws IOException {
    final int charLength = 10;
    InputStream in = new ReaderToUTF8Stream(
            new LoopingAlphabetReader(charLength, CharAlphabet.cjkSubset()),
            charLength, 0, "ignored-test-type");
    in.skip(2L); // Skip encoded length added by ReaderToUTF8Stream.
    in.skip(1L); // Skip one more byte to trigger a UTF error.
    try {
        UTF8Util.skipFully(in, charLength);
        fail("Should have failed because of UTF error.");
    } catch (UTFDataFormatException udfe) {
        // As expected, do nothing.
    }
}
UTF8UtilTest.java 文件源码 项目:gemfirexd-oss 阅读 21 收藏 0 点赞 0 评论 0
/**
 * 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
    }
}
BlockDataOutputStream.java 文件源码 项目:TayzGrid 阅读 19 收藏 0 点赞 0 评论 0
/**
     * Writes the given string in UTF format.  This method is used in
     * situations where the UTF encoding length of the string is already
     * known; specifying it explicitly avoids a prescan of the string to
     * determine its UTF length.
     */
    void writeUTF(String s, int utflen) throws IOException
    {
        if (utflen > 0xFFFFL)
        {
            throw new UTFDataFormatException();
        }

        writeInt(utflen);
//        write7BitEncodedInt(utflen);
        //writeShort((int) utflen);
        if (utflen == (long) s.length())
        {
            writeBytes(s);
        }
        else
        {
            writeUTFBody(s);
        }
    }
ModifiedUtf8.java 文件源码 项目:j2objc 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Returns the number of bytes the modified UTF-8 representation of 's' would take. Note
 * that this is just the space for the bytes representing the characters, not the length
 * which precedes those bytes, because different callers represent the length differently,
 * as two, four, or even eight bytes. If {@code shortLength} is true, we'll throw an
 * exception if the string is too long for its length to be represented by a short.
 */
public 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;
}
CacheDataOutput.java 文件源码 项目:systemml 阅读 20 收藏 0 点赞 0 评论 0
@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( c>= 0x0001 && c<=0x007F ) //1 byte range
            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));
        }
    }
}
TextStackerTest.java 文件源码 项目:filestacker 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Testa diversos casos de strings utf-8: com caracteres de 1, 2 e 3 bytes
 */
@Test
public final void testStrBytesConversions() throws UTFDataFormatException {
    String[] data = {   "pàpêpípõpü",
                        "\t \n \r", 
                        "", 
                        " ", 
                        "ЊДОШПЦФ",
                        "ดตญทธยษส", 
                        "ヅテガシジツミポブ", 
                        "สçヅยãテОガ;ธ§Д" };

    for (String str : data) {
        assertEquals(str, TextStacker.toStr(TextStacker.toBytes(str)));
    }
}
DataIoUtils.java 文件源码 项目:asakusafw-compiler 阅读 19 收藏 0 点赞 0 评论 0
/**
 * Emulates {@link DataOutput#writeUTF(String)} without using it method.
 * @param output the target {@link DataOutput}
 * @param value the target value
 * @throws IOException if failed to write String into {@link DataOutput}
 */
public static void writeUTF(DataOutput output, String value) throws IOException {
    int size = computeUtfBodySize(value);
    if (size >>> Short.SIZE != 0) {
        throw new UTFDataFormatException("too long UTF string");
    }
    output.writeShort(size);
    for (int i = 0, n = value.length(); i < n; i++) {
        char c = value.charAt(i);
        if (c != CHAR_ZERO && c <= CHAR_MAX1) {
            output.write(c);
        } else if (c <= CHAR_MAX2) {
            output.write(MASK_HEAD2 | ((c >> 6) & MASK_BODY5));
            output.write(MASK_HEAD1 | (c & MASK_BODY6));
        } else {
            output.write(MASK_HEAD3 | ((c >> 12) & MASK_BODY4));
            output.write(MASK_HEAD1 | ((c >>  6) & MASK_BODY6));
            output.write(MASK_HEAD1 | (c & MASK_BODY6));
        }
    }
}
UTFDataFormatExceptionTest.java 文件源码 项目:In-the-Box-Fork 阅读 20 收藏 0 点赞 0 评论 0
/**
 * @tests java.io.UTFDataFormatException#UTFDataFormatException()
 */
@TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "UTFDataFormatException",
        args = {}
    )
public void test_Constructor() {
    try {
        if (true) // To avoid unreachable code compilation error.
            throw new UTFDataFormatException();
        fail("Test 1: UTFDataFormatException expected.");
    } catch (UTFDataFormatException e) {
        assertNull("Test 2: Null expected for exceptions constructed without a message.",
                e.getMessage());
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号