java.nio.charset.CoderResult类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(146)

本文整理了Java中java.nio.charset.CoderResult类的一些代码示例,展示了CoderResult类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CoderResult类的具体详情如下:
包路径:java.nio.charset.CoderResult
类名称:CoderResult

CoderResult介绍

[英]Used to indicate the result of encoding/decoding. There are four types of results:

  1. UNDERFLOW indicates that all input has been processed but more input is required. It is represented by the unique object CoderResult.UNDERFLOW.
  2. OVERFLOW indicates an insufficient output buffer size. It is represented by the unique object CoderResult.OVERFLOW.
  3. A malformed-input error indicates that an unrecognizable sequence of input units has been encountered. Get an instance of this type of result by calling CoderResult.malformedForLength(int) with the length of the malformed-input.
  4. An unmappable-character error indicates that a sequence of input units can not be mapped to the output charset. Get an instance of this type of result by calling CoderResult.unmappableForLength(int) with the input sequence size indicating the identity of the unmappable character.
    [中]用于指示编码/解码的结果。有四种类型的结果:
    1.下溢表示已处理所有输入,但需要更多输入。它由唯一对象CoderResult.UNDERFLOW表示。
    1.溢出表示输出缓冲区大小不足。它由唯一对象CoderResult.OVERFLOW表示。
    1.格式错误的输入错误表示遇到无法识别的输入单元序列。通过使用格式错误的输入的长度调用CoderResult.malformedForLength(int),获取此类型结果的实例。
    1.不可映射字符错误表示输入单元序列无法映射到输出字符集。通过调用CoderResult.unmappableForLength(int)获取此类结果的实例,输入序列大小指示不可映射字符的标识。

代码示例

代码示例来源:origin: commons-io/commons-io

/**
 * Fills the internal char buffer from the reader.
 *
 * @throws IOException
 *             If an I/O error occurs
 */
private void fillBuffer() throws IOException {
  if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) {
    encoderIn.compact();
    final int position = encoderIn.position();
    // We don't use Reader#read(CharBuffer) here because it is more efficient
    // to write directly to the underlying char array (the default implementation
    // copies data to a temporary char array).
    final int c = reader.read(encoderIn.array(), position, encoderIn.remaining());
    if (c == EOF) {
      endOfInput = true;
    } else {
      encoderIn.position(position+c);
    }
    encoderIn.flip();
  }
  encoderOut.compact();
  lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput);
  encoderOut.flip();
}

代码示例来源:origin: micronaut-projects/micronaut-core

CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuf = CharBuffer.allocate(decodedCapacity);
  charBuf.clear();
  CoderResult result = decoder.reset().decode(byteBuf, charBuf, true);
  try {
    if (!result.isUnderflow()) {
      result.throwException();
    result = decoder.flush(charBuf);
    if (!result.isUnderflow()) {
      result.throwException();
  strBuf.append(charBuf.flip());

代码示例来源:origin: wildfly/wildfly

try {
  while (byteBuffer.hasRemaining()) {
    while (charBuffer.hasRemaining()) {
      final CoderResult result = encoder.encode(charBuffer, byteBuffer, false);
      if (result.isOverflow()) {
        return true;
      if (result.isUnderflow()) {
        filled = true;
        break;
      if (result.isError()) {
        if (result.isMalformed()) {
          throw msg.malformedInput();
        if (result.isUnmappable()) {
          throw msg.unmappableCharacter();
    charBuffer.compact();
    try {
      final int cnt = reader.read(charBuffer);
      if (cnt == -1) {
        return filled;
      charBuffer.flip();

代码示例来源:origin: org.postgresql/postgresql

private void checkEncodeResult(CoderResult result) throws CharacterCodingException {
 if (result.isError()) {
  result.throwException();
 }
}

代码示例来源:origin: commons-io/commons-io

/**
 * Fills the byte output buffer from the input char buffer.
 *
 * @throws CharacterCodingException
 *             an error encoding data
 */
private void fillBuffer() throws CharacterCodingException {
  this.bbuf.compact();
  final CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true);
  if (result.isError()) {
    result.throwException();
  }
  this.bbuf.flip();
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private static void decode(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) {
 try {
  CoderResult cr = decoder.decode(src, dst, true);
  if (!cr.isUnderflow()) {
   cr.throwException();
  }
  cr = decoder.flush(dst);
  if (!cr.isUnderflow()) {
   cr.throwException();
  }
 } catch (CharacterCodingException x) {
  throw new IllegalStateException(x);
 }
}

代码示例来源:origin: com.marklogic/client-api-java

@Override
  public void write(OutputStream out) throws IOException {
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();

    CharBuffer charBuf = CharBuffer.allocate(BUFFER_SIZE);
    byte[] buf = new byte[BUFFER_SIZE * 2];
    ByteBuffer byteBuf = ByteBuffer.wrap(buf);

    while (content.read(charBuf) != -1) {
      encoder.reset();
      charBuf.flip();
      byteBuf.clear();
      CoderResult result = encoder.encode(charBuf, byteBuf, false);
      if (result.isError()) {
        throw new IOException(result.toString());
      }
      byteBuf.flip();
      out.write(buf, 0, byteBuf.limit());
      charBuf.clear();
    }

    out.flush();
  }
}

代码示例来源:origin: jwpttcg66/NettyGameServer

public ByteBuffer getBytes(String charsetName) {
  if (!isEnd) {
    throw new IllegalStateException("The conact has not been end.");
  }
  final CharsetEncoder encoder = Charset.forName(charsetName).newEncoder().onMalformedInput(
      CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
  final CharBuffer cb = CharBuffer.wrap(this.value, 0, this.value.length);
  try {
    encoder.reset();
    final int len = (int) (this.length * (double) encoder.maxBytesPerChar());
    final ByteBuffer bb = ByteBuffer.allocate(len);
    CoderResult cr = encoder.encode(cb, bb, true);
    if (!cr.isUnderflow()) {
      cr.throwException();
    }
    cr = encoder.flush(bb);
    if (!cr.isUnderflow()) {
      cr.throwException();
    }
    return bb;
  } catch (Exception x) {
    throw new Error(x);
  }
}

代码示例来源:origin: killme2008/xmemcached

boolean utf16 = decoder.charset().name().startsWith("UTF-16");
decoder.reset();
int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
CharBuffer out = CharBuffer.allocate(expectedLength);
for (;;) {
 CoderResult cr;
 if (cr.isUnderflow()) {
  break;
 if (cr.isOverflow()) {
  CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
  out.flip();
  o.put(out);
 if (cr.isError()) {
  cr.throwException();

代码示例来源:origin: i2p/i2p.i2p

if (_bb == null) {
  _bb = ByteBuffer.allocate(6);
  _cb = CharBuffer.allocate(1);
  _dc = Charset.forName("UTF-8").newDecoder();
} else {
  _bb.clear();
  _cb.clear();
  _bb.put((byte) b);
_dc.reset();
_bb.flip();
CoderResult result = _dc.decode(_bb, _cb, true);
_cb.flip();
if (result.isError() || !_cb.hasRemaining())
  return REPLACEMENT;

代码示例来源:origin: stackoverflow.com

byte[] byteArray = ...;
String charsetName = ...;

ByteBuffer buffer =
  ByteBuffer.wrap(byteArray, dstBegin, dstEnd - dstBegin);
CharsetEncoder encoder = Charset.forName(charsetName).newEncoder();
CoderResult result =
  encoder.encode(CharBuffer.wrap(string), buffer, true);
if (!result.isUnderflow()) {
  result.throwException();
}

代码示例来源:origin: org.jruby/jruby-complete

private void fillByteBuffer(Reader reader) throws IOException {
  CharBuffer cbuf = CharBuffer.allocate(DEFAULT_CHAR_BUFFER_SIZE);
  ByteBuffer bbuf = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
  List<byte[]> list = new ArrayList<byte[]>();
  while (true) {
    cbuf.clear();
    int size = reader.read(cbuf);
    if (size <= 0) {
      break;
    }
    cbuf.limit(cbuf.position());
    cbuf.rewind();
    boolean eof = false;
    while (!eof) {
      CoderResult cr = encoder.encode(cbuf, bbuf, eof);
      if (cr.isError()) {
        cr.throwException();
      } else if (cr.isUnderflow()) {
        appendBytes(list, bbuf);
        eof = true;
      } else if (cr.isOverflow()) {
        appendBytes(list, bbuf);
        bbuf.clear();
      }
    }
  }
  getByteArray(list);
}

代码示例来源:origin: apache/incubator-druid

private Map<String, Object> buildStringKeyMap(ByteBuffer input)
{
 int payloadSize = input.remaining();
 if (chars == null || chars.remaining() < payloadSize) {
  chars = CharBuffer.allocate(payloadSize);
 }
 final CoderResult coderResult = charset.newDecoder()
                     .onMalformedInput(CodingErrorAction.REPLACE)
                     .onUnmappableCharacter(CodingErrorAction.REPLACE)
                     .decode(input, chars, true);
 Map<String, Object> theMap;
 if (coderResult.isUnderflow()) {
  chars.flip();
  try {
   theMap = parseString(chars.toString());
  }
  finally {
   chars.clear();
  }
 } else {
  throw new ParseException("Failed with CoderResult[%s]", coderResult);
 }
 return theMap;
}

代码示例来源:origin: org.apache.ant/ant

final CharsetEncoder enc = this.charset.newEncoder();
enc.onMalformedInput(CodingErrorAction.REPORT);
enc.onUnmappableCharacter(CodingErrorAction.REPORT);
final CharBuffer cb = CharBuffer.wrap(name);
ByteBuffer out = ByteBuffer.allocate(name.length()
                   + (name.length() + 1) / 2);
while (cb.remaining() > 0) {
  final CoderResult res = enc.encode(cb, out, true);
  if (res.isUnmappable() || res.isMalformed()) {
    if (res.length() * 6 > out.remaining()) {
      out = ZipEncodingHelper.growBuffer(out, out.position()
                        + res.length() * 6);
    for (int i = 0; i < res.length(); ++i) {
      ZipEncodingHelper.appendSurrogate(out, cb.get());
  } else if (res.isOverflow()) {
  } else if (res.isUnderflow()) {
    enc.flush(out);

代码示例来源:origin: spring-projects/spring-framework

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(outBuffer.position());
      int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());

代码示例来源:origin: redisson/redisson

private String decode() {
  if (null == decoder) {
    decoder = UTF8.newDecoder();
  }
  snapshot();
  try {
    if (null == chars || chars.remaining() < buffer.remaining()) {
      chars = CharBuffer.allocate(buffer.remaining());
    } else {
      chars.rewind();
    }
    decoder.reset();
    CoderResult cr = decoder.decode(buffer, chars, true);
    if (cr.isUnderflow()) {
      decoder.flush(chars);
    }
    chars.flip();
    return chars.toString();
  } finally {
    reset();
  }
}

代码示例来源:origin: killme2008/xmemcached

boolean utf16 = encoder.charset().name().startsWith("UTF-16");
CharBuffer in = CharBuffer.wrap(val);
limit(end);
encoder.reset();
 if (in.hasRemaining()) {
  cr = encoder.encode(in, buf(), true);
 } else {
  cr = encoder.flush(buf());
 if (cr.isUnderflow() || cr.isOverflow()) {
  break;
 cr.throwException();

代码示例来源:origin: robovm/robovm

private void drainEncoder() throws IOException {
  // Strictly speaking, I think it's part of the CharsetEncoder contract that you call
  // encode with endOfInput true before flushing. Our ICU-based implementations don't
  // actually need this, and you'd hope that any reasonable implementation wouldn't either.
  // CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway!
  CharBuffer chars = CharBuffer.allocate(0);
  while (true) {
    CoderResult result = encoder.encode(chars, bytes, true);
    if (result.isError()) {
      result.throwException();
    } else if (result.isOverflow()) {
      flushBytes(false);
      continue;
    }
    break;
  }
  // Some encoders (such as ISO-2022-JP) have stuff to write out after all the
  // characters (such as shifting back into a default state). In our implementation,
  // this is actually the first time ICU is told that we've run out of input.
  CoderResult result = encoder.flush(bytes);
  while (!result.isUnderflow()) {
    if (result.isOverflow()) {
      flushBytes(false);
      result = encoder.flush(bytes);
    } else {
      result.throwException();
    }
  }
}

代码示例来源:origin: biz.aQute.bnd/bndlib

private void convert(byte[] buffer, Charset charset) throws IOException {
  CharBuffer decode = charset.decode(ByteBuffer.wrap(buffer));
  CharsetDecoder decoder = charset.newDecoder();
  ByteBuffer bb = ByteBuffer.wrap(buffer);
  CharBuffer cb = CharBuffer.allocate(buffer.length * 4);
  CoderResult result = decoder.decode(bb, cb, true);
  if (!result.isError()) {
    
    String s = new String(cb.array(), 0, cb.position());
    s = doBackslashEncoding(s);
    super.load(new StringReader(s));
    return;
  }
  throw new CharacterCodingException();
}

代码示例来源:origin: robovm/robovm

CharBuffer out = CharBuffer.wrap(buffer, offset, count);
CoderResult result = CoderResult.UNDERFLOW;
while (out.hasRemaining()) {
      if (in.available() == 0 && out.position() > offset) {
  result = decoder.decode(bytes, out, false);
  if (result.isUnderflow()) {
  result = decoder.decode(bytes, out, true);
  decoder.flush(out);
  decoder.reset();
if (result.isMalformed() || result.isUnmappable()) {
  result.throwException();

相关文章