Search in sources :

Example 71 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project ORCID-Source by ORCID.

the class PropertyFiles method loadProFiles.

@Before
public void loadProFiles() throws ZipException, IOException {
    // ResourceBundle resources = ResourceBundle.
    FileFilter fF = new FileFilter() {

        public boolean accept(String fileName) {
            return fileName.matches(".*messages[a-zA-Z\\_\\.]*\\.properties$");
        }
    };
    String[] propfiles = Classpath.getClasspathFileNames(fF, true);
    for (String propertiesFile : propfiles) {
        if (!propertiesFile.contains("orcid"))
            continue;
        byte[] encoded = Files.readAllBytes(Paths.get(propertiesFile));
        Charset utf = Charset.forName("UTF-8");
        CharsetDecoder dec = utf.newDecoder();
        dec.onUnmappableCharacter(CodingErrorAction.REPORT);
        String propStr = dec.decode(ByteBuffer.wrap(encoded)).toString();
        Properties prop = new Properties();
        prop.load(new StringReader(propStr));
        pList.add(prop);
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) FileFilter(org.orcid.core.utils.Classpath.FileFilter) Properties(java.util.Properties) Before(org.junit.Before)

Example 72 with CharsetDecoder

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

the class WifiSsid method toString.

@Override
public String toString() {
    byte[] ssidBytes = octets.toByteArray();
    // behavior of returning empty string for this case.
    if (octets.size() <= 0 || isArrayAllZeroes(ssidBytes))
        return "";
    // TODO: Handle conversion to other charsets upon failure
    Charset charset = Charset.forName("UTF-8");
    CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    CharBuffer out = CharBuffer.allocate(32);
    CoderResult result = decoder.decode(ByteBuffer.wrap(ssidBytes), out, true);
    out.flip();
    if (result.isError()) {
        return NONE;
    }
    return out.toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CoderResult(java.nio.charset.CoderResult)

Example 73 with CharsetDecoder

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

the class Files method newBufferedReader.

/**
     * Opens a file for reading, returning a {@code BufferedReader} that may be
     * used to read text from the file in an efficient manner. Bytes from the
     * file are decoded into characters using the specified charset. Reading
     * commences at the beginning of the file.
     *
     * <p> The {@code Reader} methods that read from the file throw {@code
     * IOException} if a malformed or unmappable byte sequence is read.
     *
     * @param   path
     *          the path to the file
     * @param   cs
     *          the charset to use for decoding
     *
     * @return  a new buffered reader, with default buffer size, to read text
     *          from the file
     *
     * @throws  IOException
     *          if an I/O error occurs opening the file
     * @throws  SecurityException
     *          In the case of the default provider, and a security manager is
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
     *          method is invoked to check read access to the file.
     *
     * @see #readAllLines
     */
public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException {
    CharsetDecoder decoder = cs.newDecoder();
    Reader reader = new InputStreamReader(newInputStream(path), decoder);
    return new BufferedReader(reader);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 74 with CharsetDecoder

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

the class URI method decode.

// Evaluates all escapes in s, applying UTF-8 decoding if needed.  Assumes
// that escapes are well-formed syntactically, i.e., of the form %XX.  If a
// sequence of escaped octets is not valid UTF-8 then the erroneous octets
// are replaced with '�'.
// Exception: any "%" found between "[]" is left alone. It is an IPv6 literal
//            with a scope_id
//
private static String decode(String s) {
    if (s == null)
        return s;
    int n = s.length();
    if (n == 0)
        return s;
    if (s.indexOf('%') < 0)
        return s;
    StringBuffer sb = new StringBuffer(n);
    ByteBuffer bb = ByteBuffer.allocate(n);
    CharBuffer cb = CharBuffer.allocate(n);
    CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8").onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    // This is not horribly efficient, but it will do for now
    char c = s.charAt(0);
    boolean betweenBrackets = false;
    for (int i = 0; i < n; ) {
        // Loop invariant
        assert c == s.charAt(i);
        if (c == '[') {
            betweenBrackets = true;
        } else if (betweenBrackets && c == ']') {
            betweenBrackets = false;
        }
        if (c != '%' || betweenBrackets) {
            sb.append(c);
            if (++i >= n)
                break;
            c = s.charAt(i);
            continue;
        }
        bb.clear();
        int ui = i;
        for (; ; ) {
            assert (n - i >= 2);
            bb.put(decode(s.charAt(++i), s.charAt(++i)));
            if (++i >= n)
                break;
            c = s.charAt(i);
            if (c != '%')
                break;
        }
        bb.flip();
        cb.clear();
        dec.reset();
        CoderResult cr = dec.decode(bb, cb, true);
        assert cr.isUnderflow();
        cr = dec.flush(cb);
        assert cr.isUnderflow();
        sb.append(cb.flip().toString());
    }
    return sb.toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 75 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project quorrabot by GloriousEggroll.

the class Charsetfunctions method stringUtf8.

/*public static String stringUtf8( byte[] bytes, int off, int length ) throws InvalidDataException {
		CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
		decode.onMalformedInput( codingErrorAction );
		decode.onUnmappableCharacter( codingErrorAction );
		//decode.replaceWith( "X" );
		String s;
		try {
			s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
		} catch ( CharacterCodingException e ) {
			throw new InvalidDataException( CloseFrame.NO_UTF8, e );
		}
		return s;
	}*/
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException {
    CharsetDecoder decode = Charset.forName("UTF8").newDecoder();
    decode.onMalformedInput(codingErrorAction);
    decode.onUnmappableCharacter(codingErrorAction);
    // decode.replaceWith( "X" );
    String s;
    try {
        bytes.mark();
        s = decode.decode(bytes).toString();
        bytes.reset();
    } catch (CharacterCodingException e) {
        throw new InvalidDataException(CloseFrame.NO_UTF8, e);
    }
    return s;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InvalidDataException(org.java_websocket.exceptions.InvalidDataException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Aggregations

CharsetDecoder (java.nio.charset.CharsetDecoder)90 CharBuffer (java.nio.CharBuffer)45 ByteBuffer (java.nio.ByteBuffer)33 CoderResult (java.nio.charset.CoderResult)25 Charset (java.nio.charset.Charset)24 InputStreamReader (java.io.InputStreamReader)11 CharacterCodingException (java.nio.charset.CharacterCodingException)9 IOException (java.io.IOException)8 BufferedReader (java.io.BufferedReader)5 Properties (java.util.Properties)5 RegisterRequestProcessor (com.linkedin.databus.container.request.RegisterRequestProcessor)4 LogicalSource (com.linkedin.databus.core.data_model.LogicalSource)4 ChunkedWritableByteChannel (com.linkedin.databus2.core.container.ChunkedWritableByteChannel)4 DatabusRequest (com.linkedin.databus2.core.container.request.DatabusRequest)4 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)4 SourceIdNameRegistry (com.linkedin.databus2.schemas.SourceIdNameRegistry)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)3