Package java.nio.charset

Examples of java.nio.charset.CoderResult


        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        int delim = delimiter&0xff;
        int rc;
        int offset = 0;
        StringBuilder sb = null;
        CoderResult res;
        while ((rc = read())!=-1) {
            if (rc == delim) {
                break;
            }
            barray[offset++] = (byte)rc;
            if (barray.length == offset) {
                bbuff.position(0);
                bbuff.limit(barray.length);
                cbuff.position(0);
                cbuff.limit(carray.length);
                res = decoder.decode(bbuff, cbuff, false);
                if (res.isError()) {
                    throw new IOException("Decoding error: " + res.toString());
                }
                offset = bbuff.remaining();
                switch (offset) {
                default:
                    System.arraycopy(barray, bbuff.position(), barray, 0, bbuff
                            .remaining());
                    break;
                case 2:
                    barray[1] = barray[barray.length - 1];
                    barray[0] = barray[barray.length - 2];
                    break;
                case 1:
                    barray[0] = barray[barray.length - 1];
                    break;
                case 0:
                }
                if (sb == null) {
                    sb = new StringBuilder(cbuff.position());
                }
                sb.append(carray, 0, cbuff.position());
            }
        }
        if (sb == null) {
            if (rc == -1 && offset == 0) {
                // We are at EOF with nothing read
                return null;
            }
            sb = new StringBuilder();
        }
        bbuff.position(0);
        bbuff.limit(offset);
        cbuff.position(0);
        cbuff.limit(carray.length);
        res = decoder.decode(bbuff, cbuff, true);
        if (res.isError()) {
            System.out.println("Error");
        }
        sb.append(carray, 0, cbuff.position());
        cbuff.position(0);
        res = decoder.flush(cbuff);
        if (res.isError()) {
            System.out.println("Error");
        }
        sb.append(carray, 0, cbuff.position());
        return sb.toString();
    }
View Full Code Here


            return;
        }
        bb.flip();
        cd.reset();
        if (bb.hasRemaining()) {
            CoderResult cr = cd.decode(bb, cb, true);
            cd.flush(cb);
            if (cr.isMalformed()) {
                // Move the tail bytes to the head
                int tailLength = bb.remaining();
                if (tailLength >= MALFORMED_INPUT_MAX_LENGTH) {
                    // We only expect the bytes of one character to be broken
                    throw new MalformedInputException(tailLength);
View Full Code Here

            this.cbuf = CharBuffer.allocate(1024);
        }
        this.decoder.reset();
        int len = 0;
        while (bbuf.hasRemaining()) {
            CoderResult result = this.decoder.decode(bbuf, this.cbuf, true);
            len += handleDecodingResult(result, charbuffer, bbuf);
        }
        CoderResult result = this.decoder.flush(this.cbuf);
        len += handleDecodingResult(result, charbuffer, bbuf);
        this.cbuf.clear();
        return len;
    }
View Full Code Here

        if (this.bbuf == null) {
            this.bbuf = ByteBuffer.allocate(1024);
        }
        this.encoder.reset();
        while (cbuf.hasRemaining()) {
            CoderResult result = this.encoder.encode(cbuf, this.bbuf, true);
            handleEncodingResult(result);
        }
        CoderResult result = this.encoder.flush(this.bbuf);
        handleEncodingResult(result);
        this.bbuf.clear();
    }
View Full Code Here

    buffer.position(stringPos);
    buffer.limit(stringPos + maxStrLen);

    // encode the string
    CharBuffer input = CharBuffer.wrap(s);
    CoderResult res = encoder.encode(input, buffer, true);
    if (SanityManager.DEBUG) {
      // UNDERFLOW is returned if the entire string was encoded, OVERFLOW
      // is returned if the string was truncated at LONGVARCHAR_MAX_LEN
      SanityManager.ASSERT(
        res == CoderResult.UNDERFLOW || res == CoderResult.OVERFLOW,
View Full Code Here

   */
  protected void writeString(String s) throws DRDAProtocolException
  {
    ensureLength(maxEncodedLength(s));
    CharBuffer input = CharBuffer.wrap(s);
    CoderResult res = encoder.encode(input, buffer, true);
    if (SanityManager.DEBUG) {
      SanityManager.ASSERT(res == CoderResult.UNDERFLOW,
                 "CharBuffer was not exhausted: res = " + res);
    }
  }
View Full Code Here

    {
        final CharsetEncoder encoder = getEncoder(charset);
        final ByteBuffer dst = ByteBuffer.allocate(
                (int) ((double) src.remaining() * encoder.maxBytesPerChar()));
        try {
            CoderResult cr = encoder.encode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
            cr = encoder.flush(dst);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
        }
        catch (CharacterCodingException x) {
            throw new IllegalStateException(x);
        }
View Full Code Here

    {
        final CharsetDecoder decoder = getDecoder(charset);
        final CharBuffer dst = CharBuffer.allocate(
                (int) ((double) src.remaining() * decoder.maxCharsPerByte()));
        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);
        }
View Full Code Here

            if (!buffer.hasRemaining()) {
                outputStream.flushInternal();
            }
            while (cb.hasRemaining()) {
                int remaining = buffer.remaining();
                CoderResult result = charsetEncoder.encode(cb, buffer, false);
                outputStream.updateWritten(remaining - buffer.remaining());
                if(result.isOverflow() || !buffer.hasRemaining()) {
                    outputStream.flushInternal();
                }
                if(result.isUnmappable()) {
                    //not much we can do here, but I think writing question marks instead of just erroring
                    //out is more user friendly.
                    cb.get();
                    buffer.put((byte)'?');
                } else if(result.isError() || result.isMalformed()) {
                    error = true;
                    return;
                }
            }
        } catch (IOException e) {
View Full Code Here

    private synchronized int flush(boolean all) throws IOException {
        if (bytes.position() > 0) {
            bytes.flip();
            chars.clear();
            CoderResult cr = decoder.decode(bytes, chars, all);
            int count = chars.position();
            if (count > 0) {
                int pos = chars.arrayOffset();
                writer.write(chars.array(), pos, count);
            }
            if (cr.isError() || (all && cr == CoderResult.UNDERFLOW)) {
                cr.throwException();
            }
            if (bytes.remaining() > 0) {
                byte[] tmp = new byte[bytes.remaining()];
                bytes.get(tmp);
                bytes.clear();
View Full Code Here

TOP

Related Classes of java.nio.charset.CoderResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.