java.nio.charset.CoderResult# toString ( ) 源码实例Demo

下面列出了java.nio.charset.CoderResult# toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jdk8u-jdk   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
源代码2 项目: dragonwell8_jdk   文件: ZipCoder.java

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
源代码3 项目: openjdk-8   文件: ZipCoder.java

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode. ZipCoder uses
    // REPORT mode.
    if (isUTF8 && cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
        if (clen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
源代码4 项目: ion-java   文件: IonReaderBinaryRawX.java

private String readStringWithReusableBuffer(int numberOfBytes) throws IOException {
    int save_limit = NO_LIMIT;
    if (_local_remaining != NO_LIMIT) {
        save_limit = _local_remaining - numberOfBytes;
    }
    _local_remaining = numberOfBytes;
    readAll(utf8InputBuffer.array(), 0, numberOfBytes);
    _local_remaining = save_limit;

    utf8InputBuffer.position(0);
    utf8InputBuffer.limit(numberOfBytes);

    utf8DecodingBuffer.position(0);
    utf8DecodingBuffer.limit(utf8DecodingBuffer.capacity());

    utf8CharsetDecoder.reset();
    CoderResult coderResult = utf8CharsetDecoder.decode(utf8InputBuffer, utf8DecodingBuffer, true);
    if (coderResult.isError()) {
        throw new IonException("Illegal value encountered while validating UTF-8 data in input stream. " + coderResult.toString());
    }
    utf8DecodingBuffer.flip();
    return utf8DecodingBuffer.toString();
}
 
源代码5 项目: openjdk-8-source   文件: ZipCoder.java

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode. ZipCoder uses
    // REPORT mode.
    if (isUTF8 && cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
        if (clen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
源代码6 项目: jdk8u_jdk   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
源代码7 项目: openjdk-8-source   文件: ZipCoder.java

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
源代码8 项目: jdk8u-dev-jdk   文件: ZipCoder.java

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
源代码9 项目: AcDisplay   文件: FastXmlSerializer.java

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
 
源代码10 项目: WeexOne   文件: FastXmlSerializer.java

public void flush() throws IOException {
  //Log.i("PackageManager", "flush mPos=" + mPos);
  if (mPos > 0) {
    if (mOutputStream != null) {
      CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
      CoderResult result = mCharset.encode(charBuffer, mBytes, true);
      while (true) {
        if (result.isError()) {
          throw new IOException(result.toString());
        } else if (result.isOverflow()) {
          flushBytes();
          result = mCharset.encode(charBuffer, mBytes, true);
          continue;
        }
        break;
      }
      flushBytes();
      mOutputStream.flush();
    } else {
      mWriter.write(mText, 0, mPos);
      mWriter.flush();
    }
    mPos = 0;
  }
}
 
源代码11 项目: openjdk-jdk9   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
源代码12 项目: MyBookshelf   文件: FastXmlSerializer.java

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
 

void sendMessageBlock(CharBuffer part, boolean last) throws IOException {
    long timeoutExpiry = getTimeoutExpiry();
    boolean isDone = false;
    while (!isDone) {
        encoderBuffer.clear();
        CoderResult cr = encoder.encode(part, encoderBuffer, true);
        if (cr.isError()) {
            throw new IllegalArgumentException(cr.toString());
        }
        isDone = !cr.isOverflow();
        encoderBuffer.flip();
        sendMessageBlock(Constants.OPCODE_TEXT, encoderBuffer, last && isDone, timeoutExpiry);
    }
    stateMachine.complete(last);
}
 

public void write() {
    buffer.clear();
    CoderResult cr = encoder.encode(message, buffer, true);
    if (cr.isError()) {
        throw new IllegalArgumentException(cr.toString());
    }
    isDone = !cr.isOverflow();
    buffer.flip();
    endpoint.startMessage(Constants.OPCODE_TEXT, buffer,
            isDone && isLast, this);
}
 
源代码15 项目: jdk1.8-source-analysis   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode.
    if (isUTF8 && ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
        if (blen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return Arrays.copyOf(ba, blen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
源代码16 项目: jdk8u_jdk   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode.
    if (isUTF8 && ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
        if (blen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return Arrays.copyOf(ba, blen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 

public void write() {
    buffer.clear();
    CoderResult cr = encoder.encode(message, buffer, true);
    if (cr.isError()) {
        throw new IllegalArgumentException(cr.toString());
    }
    isDone = !cr.isOverflow();
    buffer.flip();
    endpoint.startMessage(Constants.OPCODE_TEXT, buffer,
            isDone && isLast, this);
}
 
源代码18 项目: hottub   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode.
    if (isUTF8 && ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
        if (blen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return Arrays.copyOf(ba, blen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
源代码19 项目: marathonv5   文件: WriterOutputStream.java

private void byte2char(ByteBuffer bytes, CharBuffer chars) throws IOException {
    decoder.reset();
    chars.clear();
    CoderResult result = decoder.decode(bytes, chars, true);
    if (result.isError() || result.isOverflow()) {
        throw new IOException(result.toString());
    } else if (result.isUnderflow()) {
        chars.flip();
    }
}
 
源代码20 项目: jdk8u-jdk   文件: ZipCoder.java

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode.
    if (isUTF8 && ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
        if (blen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return Arrays.copyOf(ba, blen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}