Java Code Examples for java.nio.charset.CoderResult#toString()

The following examples show how to use java.nio.charset.CoderResult#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastXmlSerializer.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
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;
    }
}
 
Example 2
Source File: ZipCoder.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 3
Source File: ZipCoder.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 4
Source File: ZipCoder.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 5
Source File: ZipCoder.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 6
Source File: IonReaderBinaryRawX.java    From ion-java with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 7
Source File: ZipCoder.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 8
Source File: ZipCoder.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 9
Source File: ZipCoder.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 10
Source File: ZipCoder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 11
Source File: FastXmlSerializer.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example 12
Source File: FastXmlSerializer.java    From WeexOne with MIT License 6 votes vote down vote up
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;
  }
}
 
Example 13
Source File: WsRemoteEndpointImplBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: ZipCoder.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 15
Source File: WriterOutputStream.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 16
Source File: ZipCoder.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 17
Source File: WsRemoteEndpointImplBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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);
}
 
Example 18
Source File: ZipCoder.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 19
Source File: ZipCoder.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 20
Source File: WsRemoteEndpointImplBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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);
}