Search in sources :

Example 36 with Charset

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

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 37 with Charset

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

the class HTTPResponse method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("Status: ").append(mStatusCode).append(CRLF);
    for (Map.Entry<String, String> entry : mHeaders.entrySet()) {
        sb.append(entry.getKey()).append(": ").append(entry.getValue()).append(CRLF);
    }
    sb.append(CRLF);
    Charset charset;
    try {
        charset = Charset.forName(getCharset());
    } catch (IllegalArgumentException iae) {
        charset = StandardCharsets.ISO_8859_1;
    }
    sb.append(new String(mBody.array(), mBody.position(), mBody.limit() - mBody.position(), charset));
    return sb.toString();
}
Also used : Charset(java.nio.charset.Charset) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 38 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class xpath method exists.

// ---( server methods )---
public static final void exists(IData pipeline) throws ServiceException {
    // --- <<IS-START(exists)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] field:0:required $expression
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [o] field:0:required $exists?
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        String expression = IDataHelper.get(cursor, "$expression", String.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        XPathExpression compiledExpression = XPathHelper.compile(expression, namespace);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        IDataHelper.put(cursor, "$exists?", XPathHelper.exists(node, compiledExpression), String.class);
    } catch (XPathExpressionException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset)

Example 39 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class yaml method emit.

// ---( server methods )---
public static final void emit(IData pipeline) throws ServiceException {
    // --- <<IS-START(emit)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] record:0:optional $document
    // [i] field:0:optional $encoding
    // [i] field:0:optional $mode {"stream","bytes","string"}
    // [o] object:0:optional $content
    IDataCursor cursor = pipeline.getCursor();
    try {
        IData document = IDataHelper.get(cursor, "$document", IData.class);
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        ObjectConvertMode mode = IDataHelper.get(cursor, "$mode", ObjectConvertMode.class);
        if (document != null) {
            IDataHelper.put(cursor, "$content", ObjectHelper.convert(IDataYAMLParser.getInstance().emit(document, charset), charset, mode));
        }
    } catch (IOException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : ObjectConvertMode(permafrost.tundra.lang.ObjectConvertMode) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Example 40 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class yaml method parse.

public static final void parse(IData pipeline) throws ServiceException {
    // --- <<IS-START(parse)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [o] record:0:optional $document
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        if (content != null) {
            IDataHelper.put(cursor, "$document", IDataYAMLParser.getInstance().parse(InputStreamHelper.normalize(content, charset)));
        }
    } catch (IOException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Aggregations

Charset (java.nio.charset.Charset)1400 IOException (java.io.IOException)259 Test (org.junit.Test)186 InputStream (java.io.InputStream)114 ByteBuffer (java.nio.ByteBuffer)110 File (java.io.File)104 ArrayList (java.util.ArrayList)102 InputStreamReader (java.io.InputStreamReader)99 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)64 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 CharsetDecoder (java.nio.charset.CharsetDecoder)55 ByteArrayInputStream (java.io.ByteArrayInputStream)54 List (java.util.List)54 Path (java.nio.file.Path)50 CharsetEncoder (java.nio.charset.CharsetEncoder)49 FileInputStream (java.io.FileInputStream)48 OutputStream (java.io.OutputStream)47