本文整理了Java中java.nio.charset.Charset.newEncoder()
方法的一些代码示例,展示了Charset.newEncoder()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Charset.newEncoder()
方法的具体详情如下:
包路径:java.nio.charset.Charset
类名称:Charset
方法名:newEncoder
[英]Returns a new instance of an encoder for this charset.
[中]返回此字符集的编码器的新实例。
代码示例来源:origin: google/guava
/**
* Creates a new input stream that will encode the characters from {@code reader} into bytes using
* the given character set. Malformed input and unmappable characters will be replaced.
*
* @param reader input source
* @param charset character set used for encoding chars to bytes
* @param bufferSize size of internal input and output buffers
* @throws IllegalArgumentException if bufferSize is non-positive
*/
ReaderInputStream(Reader reader, Charset charset, int bufferSize) {
this(
reader,
charset
.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE),
bufferSize);
}
代码示例来源:origin: com.sun.xml.bind/jaxb-impl
public NioEscapeHandler(String charsetName) {
// this(Charset.forName(charsetName));
this.encoder = Charset.forName(charsetName).newEncoder();
}
代码示例来源:origin: apache/flume
@Override
protected CharsetEncoder initialValue() {
return Charset.forName("UTF-8").newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
};
代码示例来源:origin: commons-io/commons-io
/**
* Constructor.
*
* @param cs the input character sequence
* @param charset the character set name to use
* @param bufferSize the buffer size to use.
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character
*/
public CharSequenceInputStream(final CharSequence cs, final Charset charset, final int bufferSize) {
super();
this.encoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
// Ensure that buffer is long enough to hold a complete character
final float maxBytesPerChar = encoder.maxBytesPerChar();
if (bufferSize < maxBytesPerChar) {
throw new IllegalArgumentException("Buffer size " + bufferSize + " is less than maxBytesPerChar " +
maxBytesPerChar);
}
this.bbuf = ByteBuffer.allocate(bufferSize);
this.bbuf.flip();
this.cbuf = CharBuffer.wrap(cs);
this.mark_cbuf = NO_MARK;
this.mark_bbuf = NO_MARK;
}
代码示例来源:origin: mpatric/mp3agic
protected static byte[] charBufferToBytes(CharBuffer charBuffer, String characterSet) throws CharacterCodingException {
Charset charset = Charset.forName(characterSet);
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer byteBuffer = encoder.encode(charBuffer);
return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit());
}
}
代码示例来源:origin: spring-projects/spring-security
/**
* Get the bytes of the String in UTF-8 encoded form.
*/
public static byte[] encode(CharSequence string) {
try {
ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string));
byte[] bytesCopy = new byte[bytes.limit()];
System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());
return bytesCopy;
}
catch (CharacterCodingException e) {
throw new IllegalArgumentException("Encoding failed", e);
}
}
代码示例来源:origin: jenkinsci/jenkins
out = new StringBuilder(i + (m - i) * 3);
out.append(s.substring(0, i));
enc = StandardCharsets.UTF_8.newEncoder();
buf = CharBuffer.allocate(1);
escaped = true;
buf.rewind();
try {
ByteBuffer bytes = enc.encode(buf);
while (bytes.hasRemaining()) {
byte b = bytes.get();
代码示例来源:origin: org.apache.xmlbeans/xmlbeans
private static final Writer writerForFile(File f) throws IOException
{
if (CHARSET == null)
return new FileWriter(f);
FileOutputStream fileStream = new FileOutputStream(f);
CharsetEncoder ce = CHARSET.newEncoder();
ce.onUnmappableCharacter(CodingErrorAction.REPORT);
return new OutputStreamWriter(fileStream, ce);
}
代码示例来源:origin: stackoverflow.com
// Create the encoder and decoder for ISO-8859-1
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
try {
// Convert a string to ISO-LATIN-1 bytes in a ByteBuffer
// The new ByteBuffer is ready to be read.
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string"));
// Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string.
// The new ByteBuffer is ready to be read.
CharBuffer cbuf = decoder.decode(bbuf);
String s = cbuf.toString();
} catch (CharacterCodingException e) {
}
代码示例来源:origin: stackoverflow.com
OutputStreamWriter char_output = new OutputStreamWriter(
new FileOutputStream("some_output.utf8"),
Charset.forName("UTF-8").newEncoder()
);
InputStreamReader char_input = new InputStreamReader(
new FileInputStream("some_input.utf8"),
Charset.forName("UTF-8").newDecoder()
);
代码示例来源:origin: org.apache.hadoop/hadoop-common
@Override
protected CharsetEncoder initialValue() {
return Charset.forName("UTF-8").newEncoder().
onMalformedInput(CodingErrorAction.REPORT).
onUnmappableCharacter(CodingErrorAction.REPORT);
}
};
代码示例来源:origin: stackoverflow.com
Charset utf8 = Charset.forName("UTF-8");
CharsetEncoder encoder = utf8.newEncoder();
CharBuffer input = //allocate in some way, or pass as parameter
ByteBuffer output = ByteBuffer.allocate(10);
int limit = input.limit();
while(input.position() < limit) {
output.clear();
input.mark();
input.limit(Math.max(input.position() + 2, input.capacity()));
if (Character.isHighSurrogate(input.get()) && !Character.isLowSurrogate(input.get())) {
//Malformed surrogate pair; do something!
}
input.limit(input.position());
input.reset();
encoder.encode(input, output, false);
int encodedLen = output.position();
}
代码示例来源:origin: commons-io/commons-io
/**
* Construct a new {@link ReaderInputStream}.
*
* @param reader the target {@link Reader}
* @param charset the charset encoding
* @param bufferSize the size of the input buffer in number of characters
*/
public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) {
this(reader,
charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE),
bufferSize);
}
代码示例来源:origin: org.springframework.security/spring-security-core
/**
* Get the bytes of the String in UTF-8 encoded form.
*/
public static byte[] encode(CharSequence string) {
try {
ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string));
byte[] bytesCopy = new byte[bytes.limit()];
System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());
return bytesCopy;
}
catch (CharacterCodingException e) {
throw new IllegalArgumentException("Encoding failed", e);
}
}
代码示例来源:origin: com.sleepycat/je
/**
* @return a buffer with position set to 0
*/
public static ByteBuffer toUTF8(CharBuffer chars) {
try {
final CharsetEncoder utf8Encoder = UTF8.newEncoder();
return utf8Encoder.encode(chars);
} catch (CharacterCodingException e) {
// Should never happen.
throw new RuntimeException(e);
}
}
代码示例来源:origin: robovm/robovm
/**
* Returns a new {@code ByteBuffer} containing the bytes encoding the characters from
* {@code buffer}.
* This method uses {@code CodingErrorAction.REPLACE}.
*
* <p>Applications should generally create a {@link CharsetEncoder} using {@link #newEncoder}
* for performance.
*
* @param buffer
* the character buffer containing the content to be encoded.
* @return the result of the encoding.
*/
public final ByteBuffer encode(CharBuffer buffer) {
try {
return newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE).encode(
buffer);
} catch (CharacterCodingException ex) {
throw new Error(ex.getMessage(), ex);
}
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
@Override
public Writer openSource(JPackage pkg, String fileName) throws IOException {
final Writer bw = new OutputStreamWriter(openBinary(pkg, fileName), encoding);
return new UnicodeEscapeWriter(bw) {
private final CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
@Override
protected boolean requireEscaping(int ch) {
// control characters
if (ch < 0x20 && " \t\r\n".indexOf(ch) == -1) {
return true;
}
// ASCII chars
if (ch < 0x80) {
return false;
}
return !encoder.canEncode((char) ch);
}
};
}
代码示例来源:origin: apache/drill
@Override
protected CharsetEncoder initialValue() {
return Charset.forName("UTF-8").newEncoder().
onMalformedInput(CodingErrorAction.REPORT).
onUnmappableCharacter(CodingErrorAction.REPORT);
}
};
代码示例来源:origin: com.mpatric/mp3agic
protected static byte[] charBufferToBytes(CharBuffer charBuffer, String characterSet) throws CharacterCodingException {
Charset charset = Charset.forName(characterSet);
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer byteBuffer = encoder.encode(charBuffer);
return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit());
}
}
代码示例来源:origin: prestodb/presto
/**
* Creates a new input stream that will encode the characters from {@code reader} into bytes using
* the given character set. Malformed input and unmappable characters will be replaced.
*
* @param reader input source
* @param charset character set used for encoding chars to bytes
* @param bufferSize size of internal input and output buffers
* @throws IllegalArgumentException if bufferSize is non-positive
*/
ReaderInputStream(Reader reader, Charset charset, int bufferSize) {
this(
reader,
charset
.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE),
bufferSize);
}
内容来源于网络,如有侵权,请联系作者删除!