下面列出了java.nio.charset.CharsetEncoder 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
CharsetDecoder chardecoder = null;
CharsetEncoder charencoder = null;
final Charset charset = cconfig.getCharset();
final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ? cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ? cconfig.getUnmappableInputAction()
: CodingErrorAction.REPORT;
if (charset != null) {
chardecoder = charset.newDecoder();
chardecoder.onMalformedInput(malformedInputAction);
chardecoder.onUnmappableCharacter(unmappableInputAction);
charencoder = charset.newEncoder();
charencoder.onMalformedInput(malformedInputAction);
charencoder.onUnmappableCharacter(unmappableInputAction);
}
final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
return new TracingManagedHttpClientConnection(id, cconfig.getBufferSize(), cconfig.getFragmentSizeHint(), chardecoder, charencoder,
cconfig.getMessageConstraints(), incomingContentStrategy, outgoingContentStrategy, requestWriterFactory, responseParserFactory, logFunc);
}
/**
* Create a text response with known length.
*/
public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) {
ContentType contentType = new ContentType(mimeType);
if (txt == null) {
return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0);
} else {
byte[] bytes;
try {
CharsetEncoder newEncoder = Charset.forName(contentType.getEncoding()).newEncoder();
if (!newEncoder.canEncode(txt)) {
contentType = contentType.tryUTF8();
}
bytes = txt.getBytes(contentType.getEncoding());
} catch (UnsupportedEncodingException e) {
NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e);
bytes = new byte[0];
}
return newFixedLengthResponse(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length);
}
}
public AbstractSqlPatternMatcher(String patternString) {
this.patternString = patternString;
final CharsetEncoder charsetEncoder = Charsets.UTF_8.newEncoder();
final CharBuffer patternCharBuffer = CharBuffer.wrap(patternString);
try {
patternByteBuffer = charsetEncoder.encode(patternCharBuffer);
} catch (CharacterCodingException e) {
throw UserException.validationError(e)
.message("Failure to encode pattern %s using UTF-8", patternString)
.addContext("Message: ", e.getMessage())
.build(logger);
}
patternLength = patternByteBuffer.limit();
}
private static void work(MessageDigest digest, String s, CharsetEncoder encoder) {
try {
CharBuffer cbuf = CharBuffer.allocate(s.length());
cbuf.put(s);
cbuf.flip();
ByteBuffer buf = encoder.encode(cbuf);
// System.out.println("pos="+buf.position() +",limit=" +
// buf.limit());
int nbytes = buf.limit();
byte[] encodedBytes = new byte[nbytes];
buf.get(encodedBytes);
digest.update(encodedBytes);
} catch (CharacterCodingException e) {
// This should never happen, since we're encoding to UTF-8.
}
}
/**
* Returns a new byte array containing the characters of the specified
* string encoded using the given charset.
*
* It is equivalent to <code>input.getBytes(charset)</code> except it has
* workaround for the bug ID 61917.
*
* @see https://code.google.com/p/android/issues/detail?id=61917
*/
//@formatter:off
/*
* The original code is available from
* https://android.googlesource.com/platform/libcore/+/android-4.4_r1.2/libdvm/src/main/java/java/lang/String.java
*/
//@formatter:on
public static byte[] getBytes(String input, Charset charset) {
CharBuffer chars = CharBuffer.wrap(input.toCharArray());
// @formatter:off
CharsetEncoder encoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
// @formatter:on
ByteBuffer buffer;
buffer = encode(chars.asReadOnlyBuffer(), encoder);
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes);
return bytes;
}
private void encodeBrowseUpdate ( final IoSession session, final Object message, final IoBuffer data ) throws ProtocolCodecException
{
// length
data.putUnsignedShort ( ( (BrowseAdded)message ).getEntries ().size () );
final CharsetEncoder encoder = Sessions.getCharsetEncoder ( session );
// data
for ( final BrowseAdded.Entry entry : ( (BrowseAdded)message ).getEntries () )
{
data.putUnsignedShort ( entry.getRegister () );
data.put ( entry.getDataType ().getDataType () );
data.putEnumSet ( entry.getFlags () );
try
{
data.putPrefixedString ( entry.getName (), encoder );
data.putPrefixedString ( entry.getDescription (), encoder );
data.putPrefixedString ( entry.getUnit (), encoder );
}
catch ( final CharacterCodingException e )
{
throw new ProtocolCodecException ( e );
}
}
}
public void testEncoderOutputBuffer() {
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
byte[] buffer = new byte[10];
ByteBuffer out = ByteBuffer.wrap(buffer);
assertTrue(out.hasArray());
encoder.encode(CharBuffer.wrap("ab"), out, false);
assertEquals('a', buffer[0]);
assertEquals('b', buffer[1]);
assertEquals(0, buffer[2]);
out = ByteBuffer.allocateDirect(10);
// It's no longer possible to get a byte buffer without a backing byte[] on Android.
// This test is useless on Android, unless that changes again. (You can't even
// subclass ByteBuffer because -- although it's non-final -- both the RI and Android
// have [different] package-private abstract methods you'd need to implement but can't.)
//assertFalse(out.hasArray());
encoder.encode(CharBuffer.wrap("x"), out, true);
// check whether the second decode corrupted the first buffer
assertEquals('a', buffer[0]);
assertEquals('b', buffer[1]);
assertEquals(0, buffer[2]);
}
static byte[] encode(Charset cs, char[] ca, int off, int len) {
CharsetEncoder ce = cs.newEncoder();
int en = scale(len, ce.maxBytesPerChar());
byte[] ba = new byte[en];
if (len == 0)
return ba;
boolean isTrusted = false;
if (System.getSecurityManager() != null) {
if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
ca = Arrays.copyOfRange(ca, off, off + len);
off = 0;
}
}
ce.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.reset();
if (ce instanceof ArrayEncoder) {
int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
return safeTrim(ba, blen, cs, isTrusted);
} else {
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca, off, len);
try {
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = ce.flush(bb);
if (!cr.isUnderflow())
cr.throwException();
} catch (CharacterCodingException x) {
throw new Error(x);
}
return safeTrim(ba, bb.position(), cs, isTrusted);
}
}
public static CharsetEncoder createEncoder( String encodin ) {
Charset cs = Charset.forName(System.getProperty("file.encoding"));
CharsetEncoder encoder = cs.newEncoder();
if( cs.getClass().getName().equals("sun.nio.cs.MS1252") ) {
try {
// at least JDK1.4.2_01 has a bug in MS1252 encoder.
// specifically, it returns true for any character.
// return a correct encoder to workaround this problem
// statically binding to MS1252Encoder will cause a Link error
// (at least in IBM JDK1.4.1)
@SuppressWarnings("unchecked")
Class<? extends CharsetEncoder> ms1252encoder = (Class<? extends CharsetEncoder>) Class.forName("com.sun.codemodel.internal.util.MS1252Encoder");
Constructor<? extends CharsetEncoder> c = ms1252encoder.getConstructor(new Class[]{
Charset.class
});
return c.newInstance(new Object[]{cs});
} catch( Throwable t ) {
// if something funny happens, ignore it and fall back to
// a broken MS1252 encoder. It's probably still better
// than choking here.
return encoder;
}
}
return encoder;
}
/**
* Adds a session property.
*/
public void setSessionProperty(String name, String value)
{
requireNonNull(name, "name is null");
requireNonNull(value, "value is null");
checkArgument(!name.isEmpty(), "name is empty");
CharsetEncoder charsetEncoder = US_ASCII.newEncoder();
checkArgument(name.indexOf('=') < 0, "Session property name must not contain '=': %s", name);
checkArgument(charsetEncoder.canEncode(name), "Session property name is not US_ASCII: %s", name);
checkArgument(charsetEncoder.canEncode(value), "Session property value is not US_ASCII: %s", value);
sessionProperties.put(name, value);
}
/**
* Write the given {@code CharSequence} using the given {@code Charset},
* starting at the current writing position.
* @param charSequence the char sequence to write into this buffer
* @param charset the charset to encode the char sequence with
* @return this buffer
* @since 5.1.4
*/
default DataBuffer write(CharSequence charSequence, Charset charset) {
Assert.notNull(charSequence, "CharSequence must not be null");
Assert.notNull(charset, "Charset must not be null");
if (charSequence.length() != 0) {
CharsetEncoder charsetEncoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer inBuffer = CharBuffer.wrap(charSequence);
int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar());
ByteBuffer outBuffer = ensureCapacity(estimatedSize)
.asByteBuffer(writePosition(), writableByteCount());
while (true) {
CoderResult cr = (inBuffer.hasRemaining() ?
charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW);
if (cr.isUnderflow()) {
cr = charsetEncoder.flush(outBuffer);
}
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
writePosition(writePosition() + outBuffer.position());
int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
ensureCapacity(maximumSize);
outBuffer = asByteBuffer(writePosition(), writableByteCount());
}
}
writePosition(writePosition() + outBuffer.position());
}
return this;
}
@Override
public void beginResponse(OutputStream stream) {
Charset cs = Charset.forName(getRequestedEncoding(getResult().getQuery()));
CharsetEncoder encoder = cs.newEncoder();
writer = wrapWriter(new ByteWriter(stream, encoder));
header(writer, getResult());
if (getResult().hits().getError() != null || getResult().hits().getQuery().errors().size() > 0)
error(writer, getResult());
if (getResult().getContext(false) != null)
queryContext(writer, getResult().getQuery());
}
public FontDescriptor(String nativeName, CharsetEncoder encoder,
int[] exclusionRanges){
this.nativeName = nativeName;
this.encoder = encoder;
this.exclusionRanges = exclusionRanges;
this.useUnicode = false;
Charset cs = encoder.charset();
if (cs instanceof HistoricallyNamedCharset)
this.charsetName = ((HistoricallyNamedCharset)cs).historicalName();
else
this.charsetName = cs.name();
}
public CharsetEncoder newEncoder() {
// Need to force the replacement byte to 0x3f
// because JIS_X_0208_Encoder defines its own
// alternative 2 byte substitution to permit it
// to exist as a self-standing Encoder
byte[] replacementBytes = { (byte)0x3f };
return new Encoder(this).replaceWith(replacementBytes);
}
private CharsetEncoder encoder() {
CharsetEncoder enc = encTL.get();
if (enc == null) {
enc = cs.newEncoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
encTL.set(enc);
}
return enc;
}
public static byte[] bytesFor(String content, Charset charset) throws CharacterCodingException {
CharBuffer chars = CharBuffer.wrap(content);
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buffer = encoder.encode(chars);
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return bytes;
}
private void validateDynamicTagsMod(Mailing mailing, Set<EncodingError> unencodeableDynamicTags, CharsetEncoder charsetEncoder) {
// No mailing? Nothing to validate!
if (mailing == null) {
return;
}
Collection<DynamicTag> dynTags = mailing.getDynTags().values();
for (DynamicTag dynTag : dynTags) {
unencodeableDynamicTags.addAll(validateContent(dynTag, charsetEncoder));
}
}
static byte[] encode(Charset cs, char[] ca, int off, int len) {
CharsetEncoder ce = cs.newEncoder();
int en = scale(len, ce.maxBytesPerChar());
byte[] ba = new byte[en];
if (len == 0)
return ba;
boolean isTrusted = false;
if (System.getSecurityManager() != null) {
if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
ca = Arrays.copyOfRange(ca, off, off + len);
off = 0;
}
}
ce.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.reset();
if (ce instanceof ArrayEncoder) {
int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
return safeTrim(ba, blen, cs, isTrusted);
} else {
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca, off, len);
try {
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = ce.flush(bb);
if (!cr.isUnderflow())
cr.throwException();
} catch (CharacterCodingException x) {
throw new Error(x);
}
return safeTrim(ba, bb.position(), cs, isTrusted);
}
}
public CharsetEncoder newEncoder() {
// Need to force the replacement byte to 0x3f
// because JIS_X_0208_Encoder defines its own
// alternative 2 byte substitution to permit it
// to exist as a self-standing Encoder
byte[] replacementBytes = { (byte)0x3f };
return new Encoder(this).replaceWith(replacementBytes);
}
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
try {
String s = toString();
if (s.length() > maxLength) {
s = s.substring(0, maxLength);
}
dataBuffer.put(encoder.encode(CharBuffer.wrap(s)));
return s.length();
} catch (BufferOverflowException e) {
throw new RuntimeException("The size of data buffer is only " + dataBuffer.limit() + ". Set appropriate parameter in defaultProperties file.", e);
}
}
/**
* If the binary format is set, stores the data accordingly.
*
* Call super otherwise.
*/
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
if (binaryFormat != null) {
switch (binaryFormat) {
case DOUBLE_BIG_ENDIAN:
case DOUBLE_LITTLE_ENDIAN:
case FLOAT_BIG_ENDIAN:
case FLOAT_LITTLE_ENDIAN:
ByteOrder originalByteOrder = dataBuffer.order();
dataBuffer.order(binaryFormat.byteOrder); // set the field's byte order
try {
if (binaryFormat.size == 4) {
dataBuffer.putFloat((float) this.value);
} else if(binaryFormat.size == 8) {
dataBuffer.putDouble(this.value);
}
} catch (BufferOverflowException boe) {
throw new BadDataFormatException("Failed to store the data, the buffer is full", boe);
} finally {
dataBuffer.order(originalByteOrder); // restore the original byte order
}
break;
default:
throw new JetelRuntimeException("Invalid binary format: " + binaryFormat);
}
return 0;
} else {
return super.toByteBuffer(dataBuffer, encoder, maxLength);
}
}
public CharsetEncoder newEncoder() {
// Need to force the replacement byte to 0x3f
// because JIS_X_0208_Encoder defines its own
// alternative 2 byte substitution to permit it
// to exist as a self-standing Encoder
byte[] replacementBytes = { (byte)0x3f };
return new Encoder(this).replaceWith(replacementBytes);
}
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
try {
if (!isNull()) {
dataBuffer.put(getByteArray());
} else {
dataBuffer.put(encoder.encode(CharBuffer.wrap(metadata.getNullValue())));
}
return 0;
} catch (BufferOverflowException e) {
throw new RuntimeException("The size of data buffer is only " + dataBuffer.limit() + ". Set appropriate parameter in defaultProperties file.", e);
}
}
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder) throws CharacterCodingException {
return toByteBuffer(dataBuffer, encoder, Integer.MAX_VALUE);
}
public CharsetEncoder newEncoder() {
return new Encoder(this);
}
public CharsetEncoder newEncoder() {
return new Encoder(this);
}
public CharsetEncoder newEncoder() {
return new Encoder(this);
}
public CharsetEncoder newEncoder() {
return new UTF_32Coder.Encoder(this, UTF_32Coder.BIG, false);
}
public CharsetEncoder newEncoder() {
return new Encoder(this);
}
public CharsetEncoder newEncoder() {
return new Encoder(this);
}