Search in sources :

Example 56 with Charset

use of java.nio.charset.Charset in project AntennaPod by AntennaPod.

the class VorbisCommentReader method readUTF8String.

private String readUTF8String(InputStream input, long length) throws IOException {
    byte[] buffer = new byte[(int) length];
    IOUtils.readFully(input, buffer);
    Charset charset = Charset.forName("UTF-8");
    return charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString();
}
Also used : Charset(java.nio.charset.Charset)

Example 57 with Charset

use of java.nio.charset.Charset in project android_frameworks_base by DirtyUnicorns.

the class GsmAlphabet method gsm8BitUnpackedToString.

/**
     * Convert a GSM alphabet string that's stored in 8-bit unpacked
     * format (as it often appears in SIM records) into a String
     *
     * Field may be padded with trailing 0xff's. The decode stops
     * at the first 0xff encountered.
     *
     * Additionally, in some country(ex. Korea), there are non-ASCII or MBCS characters.
     * If a character set is given, characters in data are treat as MBCS.
     */
public static String gsm8BitUnpackedToString(byte[] data, int offset, int length, String characterset) {
    boolean isMbcs = false;
    Charset charset = null;
    ByteBuffer mbcsBuffer = null;
    if (!TextUtils.isEmpty(characterset) && !characterset.equalsIgnoreCase("us-ascii") && Charset.isSupported(characterset)) {
        isMbcs = true;
        charset = Charset.forName(characterset);
        mbcsBuffer = ByteBuffer.allocate(2);
    }
    // Always use GSM 7 bit default alphabet table for this method
    String languageTableToChar = sLanguageTables[0];
    String shiftTableToChar = sLanguageShiftTables[0];
    StringBuilder ret = new StringBuilder(length);
    boolean prevWasEscape = false;
    for (int i = offset; i < offset + length; i++) {
        // Never underestimate the pain that can be caused
        // by signed bytes
        int c = data[i] & 0xff;
        if (c == 0xff) {
            break;
        } else if (c == GSM_EXTENDED_ESCAPE) {
            if (prevWasEscape) {
                // Two escape chars in a row
                // We treat this as a space
                // See Note 1 in table 6.2.1.1 of TS 23.038 v7.00
                ret.append(' ');
                prevWasEscape = false;
            } else {
                prevWasEscape = true;
            }
        } else {
            if (prevWasEscape) {
                char shiftChar = c < shiftTableToChar.length() ? shiftTableToChar.charAt(c) : ' ';
                if (shiftChar == ' ') {
                    // display character from main table if not present in shift table
                    if (c < languageTableToChar.length()) {
                        ret.append(languageTableToChar.charAt(c));
                    } else {
                        ret.append(' ');
                    }
                } else {
                    ret.append(shiftChar);
                }
            } else {
                if (!isMbcs || c < 0x80 || i + 1 >= offset + length) {
                    if (c < languageTableToChar.length()) {
                        ret.append(languageTableToChar.charAt(c));
                    } else {
                        ret.append(' ');
                    }
                } else {
                    // isMbcs must be true. So both mbcsBuffer and charset are initialized.
                    mbcsBuffer.clear();
                    mbcsBuffer.put(data, i++, 2);
                    mbcsBuffer.flip();
                    ret.append(charset.decode(mbcsBuffer).toString());
                }
            }
            prevWasEscape = false;
        }
    }
    return ret.toString();
}
Also used : Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer)

Example 58 with Charset

use of java.nio.charset.Charset in project jdk8u_jdk by JetBrains.

the class CheckEncodingPropertiesFile method test.

public static void test(Properties props) throws Exception {
    // First, build a mapping from the properties read from the resource
    // file.
    // We're going to check the consistency of the resource file
    // while building this mapping, and throw errors if the file
    // does not meet our assumptions.
    //
    Map<String, Collection<String>> lines = new HashMap<>();
    final CheckCharsetMapping mapping = new CheckCharsetMapping();
    for (String key : props.stringPropertyNames()) {
        Collection<String> values = getValues(props.getProperty(key));
        lines.put(key, values);
        mapping.addMapping(key, values);
    }
    // Then build maps of EncodingInfos, and print along debugging
    // information that should help understand the content of the
    // resource file and the mapping it defines.
    //
    // Map indexed by java names
    Map<String, EncodingInfo> javaInfos = new HashMap<>();
    // Map indexed by XML names
    Map<String, EncodingInfo> xmlMap = new HashMap<>();
    Map<String, String> preferred = // Java Name -> Preferred Mime Name
    new HashMap<>(mapping.preferredMime);
    // unused...
    List<EncodingInfo> all = new ArrayList<>();
    for (Entry<String, Collection<String>> e : lines.entrySet()) {
        final String charsetName = mapping.getCharsetNameFor(e.getKey());
        if (charsetName == null) {
            System.out.println("!! No charset for: " + e.getKey() + " " + e.getValue());
            continue;
        }
        Charset c = Charset.forName(charsetName);
        EncodingInfo info;
        final String k = e.getKey().toUpperCase();
        final String kc = charsetName.toUpperCase();
        StringBuilder sb = new StringBuilder();
        for (String xml : e.getValue()) {
            final String kx = xml.toUpperCase();
            info = xmlMap.get(kx);
            if (info == null) {
                info = new EncodingInfo(xml, charsetName);
                System.out.println("** XML: " + xml + " -> " + charsetName);
                xmlMap.put(kx, info);
                all.add(info);
            }
            if (!javaInfos.containsKey(k)) {
                javaInfos.put(k, info);
                if (!preferred.containsKey(k)) {
                    preferred.put(k, xml);
                }
                sb.append("** Java: ").append(k).append(" -> ").append(xml).append(" (charset: ").append(charsetName).append(")\n");
            }
            if (!javaInfos.containsKey(kc)) {
                if (!preferred.containsKey(kc)) {
                    preferred.put(kc, xml);
                }
                javaInfos.put(kc, info);
                sb.append("** Java: ").append(kc).append(" -> ").append(xml).append(" (charset: ").append(charsetName).append(")\n");
            }
            if (!javaInfos.containsKey(c.name().toUpperCase())) {
                if (!preferred.containsKey(c.name().toUpperCase())) {
                    preferred.put(c.name().toUpperCase(), xml);
                }
                javaInfos.put(c.name().toUpperCase(), info);
                sb.append("** Java: ").append(c.name().toUpperCase()).append(" -> ").append(xml).append(" (charset: ").append(charsetName).append(")\n");
            }
        }
        if (sb.length() == 0) {
            System.out.println("Nothing new for " + charsetName + ": " + e.getKey() + " -> " + e.getValue());
        } else {
            System.out.print(sb);
        }
    }
    // Now we're going to verify that Encodings.java has done its job
    // correctly. We're going to ask Encodings to convert java names to mime
    // names and mime names to java names - and verify that the returned
    // java names do map to recognized charsets.
    //
    // We're also going to verify that Encodings has recorded the preferred
    // mime name correctly.
    Method m = Encodings.class.getDeclaredMethod("getMimeEncoding", String.class);
    m.setAccessible(true);
    Set<String> xNames = new HashSet<>();
    Set<String> jNames = new HashSet<>();
    for (String name : xmlMap.keySet()) {
        final String javaName = checkConvertMime2Java(name);
        checkPreferredMime(m, javaName, preferred);
        jNames.add(javaName);
        xNames.add(name);
    }
    for (String javaName : lines.keySet()) {
        final String javaCharsetName = mapping.getCharsetNameFor(javaName.toUpperCase());
        if (javaCharsetName == null)
            continue;
        if (!jNames.contains(javaName)) {
            checkPreferredMime(m, javaName, preferred);
            jNames.add(javaName);
        }
        for (String xml : lines.get(javaName)) {
            if (xNames.contains(xml))
                continue;
            final String jName = checkConvertMime2Java(xml);
            xNames.add(xml);
            if (jNames.contains(jName))
                continue;
            checkPreferredMime(m, jName, preferred);
        }
    }
}
Also used : EncodingInfo(com.sun.org.apache.xml.internal.serializer.EncodingInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) Method(java.lang.reflect.Method) Collection(java.util.Collection) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 59 with Charset

use of java.nio.charset.Charset in project jdk8u_jdk by JetBrains.

the class NIOJISAutoDetectTest method detectingCharset.

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) Charset(java.nio.charset.Charset)

Example 60 with Charset

use of java.nio.charset.Charset in project weex-example by KalicyZhou.

the class AztecWriter method encode.

@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
    Charset charset = DEFAULT_CHARSET;
    int eccPercent = Encoder.DEFAULT_EC_PERCENT;
    int layers = Encoder.DEFAULT_AZTEC_LAYERS;
    if (hints != null) {
        if (hints.containsKey(EncodeHintType.CHARACTER_SET)) {
            charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
        }
        if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
            eccPercent = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
        }
        if (hints.containsKey(EncodeHintType.AZTEC_LAYERS)) {
            layers = Integer.parseInt(hints.get(EncodeHintType.AZTEC_LAYERS).toString());
        }
    }
    return encode(contents, format, width, height, charset, eccPercent, layers);
}
Also used : Charset(java.nio.charset.Charset)

Aggregations

Charset (java.nio.charset.Charset)1427 IOException (java.io.IOException)268 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)111 File (java.io.File)106 ArrayList (java.util.ArrayList)106 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)66 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 CharsetDecoder (java.nio.charset.CharsetDecoder)57 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 CharsetEncoder (java.nio.charset.CharsetEncoder)50 Path (java.nio.file.Path)50 FileInputStream (java.io.FileInputStream)49 BufferedReader (java.io.BufferedReader)48