本文整理汇总了Java中java.nio.charset.CharsetEncoder.reset方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetEncoder.reset方法的具体用法?Java CharsetEncoder.reset怎么用?Java CharsetEncoder.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.charset.CharsetEncoder
的用法示例。
在下文中一共展示了CharsetEncoder.reset方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
ByteBuffer out = ByteBuffer.allocate(length);
encoder.reset();
CoderResult flushResult = null;
while (flushResult != CoderResult.UNDERFLOW) {
CoderResult encodeResult = encoder.encode(in, out, true);
if (encodeResult == CoderResult.OVERFLOW) {
out = allocateMore(out);
continue;
}
flushResult = encoder.flush(out);
if (flushResult == CoderResult.OVERFLOW) {
out = allocateMore(out);
}
}
out.flip();
return out;
}
示例2: fromString
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
public ByteBuffer fromString(String source)
{
// the encoder must be reset each time it's used, hence the thread-local storage
CharsetEncoder theEncoder = encoder.get();
theEncoder.reset();
try
{
return theEncoder.encode(CharBuffer.wrap(source));
}
catch (CharacterCodingException exc)
{
throw new MarshalException(String.format("Invalid ASCII character in string literal: %s", exc));
}
}
示例3: TextMessageSendHandler
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
public TextMessageSendHandler(SendHandler handler, CharBuffer message,
boolean isLast, CharsetEncoder encoder,
ByteBuffer encoderBuffer, WsRemoteEndpointImplBase endpoint) {
this.handler = handler;
this.message = message;
this.isLast = isLast;
this.encoder = encoder.reset();
this.buffer = encoderBuffer;
this.endpoint = endpoint;
}
示例4: ReaderInputStream
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
/**
* Creates a new input stream that will encode the characters from {@code reader} into bytes using
* the given character set encoder.
*
* @param reader input source
* @param encoder character set encoder 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, CharsetEncoder encoder, int bufferSize) {
this.reader = checkNotNull(reader);
this.encoder = checkNotNull(encoder);
checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize);
encoder.reset();
charBuffer = CharBuffer.allocate(bufferSize);
charBuffer.flip();
byteBuffer = ByteBuffer.allocate(bufferSize);
}
示例5: getSubstringByte
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
/**
* 指定したバイト数で文字列をカットする
*
* @param obj 対象オブジェクト
* @param capacity カットするバイト数
* @return String
* @throws CharacterCodingException
* @throws UnsupportedEncodingException
*/
private String getSubstringByte(final Object obj, final int capacity) throws CharacterCodingException,
UnsupportedEncodingException {
String str = obj == null ? "null" : obj.toString();
if (capacity < 1) {
return str;
}
CharsetEncoder ce = Charset.forName(ENCODING_SHIFT_JIS).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
if (capacity >= ce.maxBytesPerChar() * str.length()) {
return str;
}
CharBuffer cb = CharBuffer.wrap(new char[Math.min(str.length(), capacity)]);
str.getChars(0, Math.min(str.length(), cb.length()), cb.array(), 0);
if (capacity >= ce.maxBytesPerChar() * cb.limit()) {
return cb.toString();
}
ByteBuffer out = ByteBuffer.allocate(capacity);
ce.reset();
CoderResult cr = null;
if (cb.hasRemaining()) {
cr = ce.encode(cb, out, true);
} else {
cr = CoderResult.UNDERFLOW;
}
if (cr.isUnderflow()) {
cr = ce.flush(out);
}
return cb.flip().toString();
}
示例6: ReaderInputStream
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
/**
* Creates a new input stream that will encode the characters from {@code reader} into bytes using
* the given character set encoder.
*
* @param reader input source
* @param encoder character set encoder 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, CharsetEncoder encoder, int bufferSize) {
Assert.notNull(reader, "不能为空");
Assert.notNull(encoder, "不能为空");
this.reader = reader;
this.encoder = encoder;
encoder.reset();
charBuffer = CharBuffer.allocate(bufferSize);
charBuffer.flip();
byteBuffer = ByteBuffer.allocate(bufferSize);
}
示例7: TextMessageSendHandler
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
public TextMessageSendHandler(SendHandler handler, CharBuffer message, boolean isLast, CharsetEncoder encoder,
ByteBuffer encoderBuffer, WsRemoteEndpointImplBase endpoint) {
this.handler = handler;
this.message = message;
this.isLast = isLast;
this.encoder = encoder.reset();
this.buffer = encoderBuffer;
this.endpoint = endpoint;
}
示例8: putString
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public IoBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException {
if (val.length() == 0) {
return this;
}
CharBuffer in = CharBuffer.wrap(val);
encoder.reset();
int expandedState = 0;
for (;;) {
CoderResult cr;
if (in.hasRemaining()) {
cr = encoder.encode(in, buf(), true);
} else {
cr = encoder.flush(buf());
}
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
if (isAutoExpand()) {
switch (expandedState) {
case 0:
autoExpand((int) Math.ceil(in.remaining() * encoder.averageBytesPerChar()));
expandedState++;
break;
case 1:
autoExpand((int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()));
expandedState++;
break;
default:
throw new RuntimeException("Expanded by "
+ (int) Math.ceil(in.remaining() * encoder.maxBytesPerChar())
+ " but that wasn't enough for '" + val + "'");
}
continue;
}
} else {
expandedState = 0;
}
cr.throwException();
}
return this;
}