Java Code Examples for java.nio.charset.CharsetDecoder#detectedCharset()

The following examples show how to use java.nio.charset.CharsetDecoder#detectedCharset() . 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: NIOJISAutoDetectTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 2
Source File: NIOJISAutoDetectTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 3
Source File: NIOJISAutoDetectTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 4
Source File: NIOJISAutoDetectTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 5
Source File: NIOJISAutoDetectTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 6
Source File: NIOJISAutoDetectTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 7
Source File: NIOJISAutoDetectTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 8
Source File: NIOJISAutoDetectTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 9
Source File: NIOJISAutoDetectTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 10
Source File: NIOJISAutoDetectTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 11
Source File: NIOJISAutoDetectTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 12
Source File: NIOJISAutoDetectTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 13
Source File: NIOJISAutoDetectTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 14
Source File: AbstractMatcher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Handle an error thrown while file decoding. Inform search listener and
 * append detailed info into the IDE Log.
 */
protected final void handleDecodingError(SearchListener listener,
        FileObject file, CharsetDecoder decoder,
        CharacterCodingException e) {

    String charsetName;
    try {
        if (decoder.isAutoDetecting() && decoder.isCharsetDetected()) {
            Charset c = decoder.detectedCharset();
            if (c != null) {
                charsetName = c.displayName();
            } else {
                charsetName = decoder.charset().displayName();
            }
        } else {
            charsetName = decoder.charset().displayName();
        }
    } catch (Exception ex) {
        LOG.log(Level.INFO, "Failed to obtain actual charset", ex); //NOI18N
        charsetName = decoder == null ? "null" : decoder.toString();//NOI18N
    }

    String msg = NbBundle.getMessage(ResultView.class,
            "TEXT_INFO_ERROR_ENCODING", charsetName);               //NOI18N
    listener.fileContentMatchingError(file.getPath(),
            new Exception(msg, e));
    LOG.log(Level.INFO, "{0}; UnmappableCharacterException: {1}", //NOI18N
            new Object[]{file.getPath(), e.getMessage()});
}