本文整理了Java中java.nio.charset.Charset.decode()
方法的一些代码示例,展示了Charset.decode()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Charset.decode()
方法的具体详情如下:
包路径:java.nio.charset.Charset
类名称:Charset
方法名:decode
[英]Returns a new CharBuffer containing the characters decoded from buffer. This method uses CodingErrorAction.REPLACE.
Applications should generally create a CharsetDecoder using #newDecoderfor performance.
[中]返回包含从缓冲区解码的字符的新CharBuffer。此方法使用CodingErrorAction。代替
应用程序通常应使用#newdecoderforperformance创建CharsetDecoder。
代码示例来源:origin: apache/kafka
/**
* Attempt to read a file as a string
* @throws IOException
*/
public static String readFileAsString(String path, Charset charset) throws IOException {
if (charset == null) charset = Charset.defaultCharset();
try (FileChannel fc = FileChannel.open(Paths.get(path))) {
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
return charset.decode(bb).toString();
}
}
代码示例来源:origin: stackoverflow.com
Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-1");
ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[]{(byte)0xC3, (byte)0xA2});
// decode UTF-8
CharBuffer data = utf8charset.decode(inputBuffer);
// encode ISO-8559-1
ByteBuffer outputBuffer = iso88591charset.encode(data);
byte[] outputData = outputBuffer.array();
代码示例来源:origin: apache/nifi
/**
* Reads a string encoded with UTF_16LE of a given length
*
* @param length the number of characters
* @return the string
*/
public String readWString(int length) {
int numBytes = length * 2;
String result = StandardCharsets.UTF_16LE.decode(ByteBuffer.wrap(bytes, position, numBytes)).toString();
position += numBytes;
return result;
}
代码示例来源:origin: centic9/jgit-cookbook
private static String getTextFromFilePath(Path file) throws IOException {
byte[] bytes = Files.readAllBytes(file);
CharBuffer chars = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes));
return chars.toString();
}
}
代码示例来源:origin: stackoverflow.com
System.out.println(Charset.defaultCharset().name());
ByteBuffer bybf = ByteBuffer.wrap("Olé".getBytes());
Charset utf16 = Charset.forName("UTF-16");
CharBuffer chbf = utf16.decode(bybf);
System.out.println(chbf);
bybf = ByteBuffer.wrap("Olé".getBytes(utf16));
chbf = utf16.decode(bybf);
System.out.println(chbf);
代码示例来源:origin: Atmosphere/atmosphere
/**
* Decodes octets to characters using the UTF-8 decoding and appends
* the characters to a StringBuffer.
* @return the index to the next unchecked character in the string to decode
*/
private static int decodeOctets(int i, ByteBuffer bb, StringBuilder sb) {
// If there is only one octet and is an ASCII character
if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) {
// Octet can be appended directly
sb.append((char) bb.get(0));
return i + 2;
} else {
//
CharBuffer cb = UTF_8_CHARSET.decode(bb);
sb.append(cb.toString());
return i + bb.limit() * 3 - 1;
}
}
代码示例来源:origin: org.apache.commons/commons-text
@Test
public void testAppendTakingTwoIntsWithIndexOutOfBoundsThrowsStringIndexOutOfBoundsExceptionTwo() {
assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> {
final Charset charset = Charset.defaultCharset();
final ByteBuffer byteBuffer = charset.encode("asdf");
final CharBuffer charBuffer = charset.decode(byteBuffer);
new StrBuilder().append(charBuffer, 933, 654);
});
}
代码示例来源:origin: brianfrankcooper/YCSB
/** Consumes remaining contents of this object, and returns them as a string. */
public String toString() {
Charset cset = Charset.forName("UTF-8");
CharBuffer cb = cset.decode(ByteBuffer.wrap(this.toArray()));
return cb.toString();
}
代码示例来源:origin: centic9/jgit-cookbook
private static String getTextFromFilePath(Path file) throws IOException {
byte[] bytes = Files.readAllBytes(file);
CharBuffer chars = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes));
return chars.toString();
}
}
代码示例来源:origin: geoserver/geoserver
/** Converts byte array to char array. */
public static char[] toChars(byte[] b, Charset charset) {
CharBuffer buff = charset.decode(ByteBuffer.wrap(b));
char[] tmp = new char[buff.limit()];
buff.get(tmp);
return tmp;
}
代码示例来源:origin: looly/hutool
/**
* 将编码的byteBuffer数据转换为字符串
*
* @param data 数据
* @param charset 字符集,如果为空使用当前系统字符集
* @return 字符串
*/
public static String str(ByteBuffer data, Charset charset) {
if (null == charset) {
charset = Charset.defaultCharset();
}
return charset.decode(data).toString();
}
代码示例来源:origin: jersey/jersey
/**
* Decodes octets to characters using the UTF-8 decoding and appends
* the characters to a StringBuffer.
*
* @return the index to the next unchecked character in the string to decode
*/
private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb) {
// If there is only one octet and is an ASCII character
if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) {
// Octet can be appended directly
sb.append((char) bb.get(0));
return i + 2;
} else {
//
final CharBuffer cb = UTF_8_CHARSET.decode(bb);
sb.append(cb.toString());
return i + bb.limit() * 3 - 1;
}
}
代码示例来源:origin: org.apache.commons/commons-text
@Test
public void testAppendTakingTwoIntsWithZeroThrowsStringIndexOutOfBoundsException() {
assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> {
final Charset charset = Charset.defaultCharset();
final ByteBuffer byteBuffer = charset.encode("end < start");
final CharBuffer charBuffer = charset.decode(byteBuffer);
new StrBuilder(630).append(charBuffer, 0, 630);
});
}
代码示例来源:origin: jenkinsci/jenkins
raf.readFully(tail);
String tails = cs.decode(java.nio.ByteBuffer.wrap(tail)).toString();
代码示例来源:origin: stackoverflow.com
String content = readFile(jsonFilePath, Charset.defaultCharset());
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}
代码示例来源:origin: commonsguy/cw-omnibus
static char[] toChars(byte[] bytes) {
Charset charset=Charset.forName("UTF-8");
ByteBuffer byteBuffer=ByteBuffer.wrap(bytes);
CharBuffer charBuffer=charset.decode(byteBuffer);
char[] chars=Arrays.copyOf(charBuffer.array(), charBuffer.limit());
Arrays.fill(charBuffer.array(), '\u0000'); // clear the cleartext
Arrays.fill(byteBuffer.array(), (byte) 0); // clear the ciphertext
return chars;
}
}
代码示例来源:origin: looly/hutool
/**
* 将编码的byteBuffer数据转换为字符串
*
* @param data 数据
* @param charset 字符集,如果为空使用当前系统字符集
* @return 字符串
*/
public static String str(ByteBuffer data, Charset charset) {
if (null == charset) {
charset = Charset.defaultCharset();
}
return charset.decode(data).toString();
}
代码示例来源:origin: jersey/jersey
/**
* Decodes octets to characters using the UTF-8 decoding and appends
* the characters to a StringBuffer.
*
* @return the index to the next unchecked character in the string to decode
*/
private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb) {
// If there is only one octet and is an ASCII character
if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) {
// Octet can be appended directly
sb.append((char) bb.get(0));
return i + 2;
} else {
//
final CharBuffer cb = UTF_8_CHARSET.decode(bb);
sb.append(cb.toString());
return i + bb.limit() * 3 - 1;
}
}
代码示例来源:origin: org.apache.commons/commons-text
@Test
public void testAppendTakingTwoIntsWithZeroThrowsStringIndexOutOfBoundsException() {
assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> {
final Charset charset = Charset.defaultCharset();
final ByteBuffer byteBuffer = charset.encode("end < start");
final CharBuffer charBuffer = charset.decode(byteBuffer);
new TextStringBuilder(630).append(charBuffer, 0, 630);
});
}
代码示例来源:origin: spotify/helios
@Override
public void write(int val) throws IOException {
if (System.currentTimeMillis() > deadline) {
throw new IOException("timed out trying to succeed");
}
lineBuffer[counter] = (byte) val;
counter++;
if (val != 10) {
return;
}
final String line = Charsets.UTF_8.decode(
ByteBuffer.wrap(lineBuffer, 0, counter)).toString();
outputLines.add(line);
counter = 0;
if (line.contains(abbreviatedTestHost) && !line.contains("UNKNOWN")) {
success.set(true);
throw new IOException("output closed");
}
}
};
内容来源于网络,如有侵权,请联系作者删除!