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);
}
}
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();
}
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);
}
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();
}
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;
}
Aggregations