Examples of CoderResult


Examples of java.nio.charset.CoderResult

    boolean isEndOfInput = false;
    if (position >= fileSize) {
      isEndOfInput = true;
    }

    CoderResult res = decoder.decode(buf, charBuf, isEndOfInput);
    if (res.isMalformed() || res.isUnmappable()) {
      res.throwException();
    }

    int delta = buf.position() - start;

    charBuf.flip();
View Full Code Here

Examples of java.nio.charset.CoderResult

    public static String decodeString(ByteBuffer src, Charset charset)
    {
        CharsetDecoder decoder = getDecoder(charset);
        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

Examples of java.nio.charset.CoderResult

    private static String decode(ByteBuffer bb, CharsetDecoder decoder)
    {
        CharBuffer cb = CharBuffer.allocate(128);

        CoderResult result = decoder.decode((ByteBuffer) bb.flip(), cb, true /* endOfInput */);
        if (result.isError())
        {
            throw new IllegalArgumentException("Malformed UTF-8!");
        }

        return ((CharBuffer) cb.flip()).toString();
View Full Code Here

Examples of java.nio.charset.CoderResult

    ce.reset();
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(src, off, len);
    try {
      CoderResult cr = ce.encode(cb, bb, true);
      if (!cr.isUnderflow())
        cr.throwException();
      cr = ce.flush(bb);
      if (!cr.isUnderflow())
        cr.throwException();
      return bb;
    } catch (CharacterCodingException x) {
      // Substitution is always enabled,
      // so this shouldn't happen
      throw new Error(x);
View Full Code Here

Examples of java.nio.charset.CoderResult

        public  int encode(String source) {
            wrapper.clear();
            encoder.reset();

            CharBuffer in = CharBuffer.wrap(source);
            CoderResult result = encoder.encode(in, wrapper, true);
            assert result == CoderResult.UNDERFLOW;
            result = encoder.flush(wrapper);
            assert result == CoderResult.UNDERFLOW;

            return wrapper.position();
View Full Code Here

Examples of java.nio.charset.CoderResult

                tempWrapper.clear();
                tempWrapper.limit(readLength);
                readOffset += readLength;

                done = readOffset == source.length();
                CoderResult result = encoder.encode(tempWrapper, wrapper, done);
                assert result == CoderResult.UNDERFLOW;
            }
            CoderResult result = encoder.flush(wrapper);
            assert result == CoderResult.UNDERFLOW;

            return wrapper.position();
        }
View Full Code Here

Examples of java.nio.charset.CoderResult

        CharBuffer cb = CharBuffer.wrap(name);
        ByteBuffer out = ByteBuffer.allocate(name.length()
                                             + (name.length() + 1) / 2);

        while (cb.remaining() > 0) {
            CoderResult res = enc.encode(cb, out,true);

            if (res.isUnmappable() || res.isMalformed()) {

                // write the unmappable characters in utf-16
                // pseudo-URL encoding style to ByteBuffer.
                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()) {

                out = ZipEncodingHelper.growBuffer(out, 0);

            } else if (res.isUnderflow()) {

                enc.flush(out);
                break;

            }
View Full Code Here

Examples of java.nio.charset.CoderResult

        Text text = target.get();
        encoder.reset();
        encodeBuffer.clear();
        while (true) {
            CoderResult result = encoder.encode(source, encodeBuffer, true);
            if (result.isError()) {
                throw new RecordFormatException(MessageFormat.format(
                        "Cannot process a character string (\"{0}\")",
                        result));
            }
            if (result.isUnderflow()) {
                consumeEncoded(text);
                break;
            }
            if (result.isOverflow()) {
                consumeEncoded(text);
            }
        }
        while (true) {
            CoderResult result = encoder.flush(encodeBuffer);
            if (result.isError()) {
                throw new RecordFormatException(MessageFormat.format(
                        "Cannot process a character string (\"{0}\")",
                        result));
            }
            if (result.isUnderflow()) {
                consumeEncoded(text);
                break;
            }
            if (result.isOverflow()) {
                consumeEncoded(text);
            }
        }
    }
View Full Code Here

Examples of java.nio.charset.CoderResult

        byte[] bytes = text.getBytes();
        ByteBuffer source = ByteBuffer.wrap(bytes, 0, text.getLength());
        decoder.reset();
        decodeBuffer.clear();
        while (true) {
            CoderResult result = decoder.decode(source, decodeBuffer, true);
            if (result.isError()) {
                throw new RecordFormatException(MessageFormat.format(
                        "Cannot process a character string (\"{0}\")",
                        result));
            }
            if (result.isUnderflow()) {
                consumeDecoded();
                break;
            }
            if (result.isOverflow()) {
                consumeDecoded();
            }
        }
        while (true) {
            CoderResult result = decoder.flush(decodeBuffer);
            if (result.isError()) {
                throw new RecordFormatException(MessageFormat.format(
                        "Cannot process a character string (\"{0}\")",
                        result));
            }
            if (result.isUnderflow()) {
                consumeDecoded();
                break;
            }
            if (result.isOverflow()) {
                consumeDecoded();
            }
        }
    }
View Full Code Here

Examples of java.nio.charset.CoderResult

            this.endpoint = endpoint;
        }

        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);
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.