Package java.nio.charset

Examples of java.nio.charset.CoderResult


        // Add each byte one at a time. The decoder should fail as soon as
        // an invalid sequence has been provided
        for (int i = 0; i < len; i++) {
            bb.put((byte) testCase.input[i]);
            bb.flip();
            CoderResult cr = decoder.decode(bb, cb, false);
            if (cr.isError()) {
                int expected =  testCase.invalidIndex;
                if ((flags & ERROR_POS_PLUS1) != 0) {
                    expected += 1;
                }
                if ((flags & ERROR_POS_PLUS2) != 0) {
                    expected += 2;
                }
                if ((flags & ERROR_POS_PLUS4) != 0) {
                    expected += 4;
                }
                Assert.assertEquals(testCase.description, expected, i);
                break;
            }
            bb.compact();
        }

        // Configure decoder to replace on an error
        decoder.reset();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);

        // Add each byte one at a time.
        bb.clear();
        cb.clear();
        for (int i = 0; i < len; i++) {
            bb.put((byte) testCase.input[i]);
            bb.flip();
            CoderResult cr = decoder.decode(bb, cb, false);
            if (cr.isError()) {
                Assert.fail(testCase.description);
            }
            bb.compact();
        }
        // For incomplete sequences at the end of the input need to tell
        // the decoder the input has ended
        bb.flip();
        CoderResult cr = decoder.decode(bb, cb, true);
        if (cr.isError()) {
            Assert.fail(testCase.description);
        }
        cb.flip();

        String expected = testCase.outputReplaced;
View Full Code Here


                this.charbuffer = CharBuffer.allocate(this.lineBuffersize);
            }
            this.chardecoder.reset();

            for (;;) {
                final CoderResult result = this.chardecoder.decode(
                        this.buffer,
                        this.charbuffer,
                        true);
                if (result.isError()) {
                    result.throwException();
                }
                if (result.isOverflow()) {
                    this.charbuffer.flip();
                    linebuffer.append(
                            this.charbuffer.array(),
                            this.charbuffer.position(),
                            this.charbuffer.remaining());
                    this.charbuffer.clear();
                }
                if (result.isUnderflow()) {
                    break;
                }
            }

            // flush the decoder
View Full Code Here

        int expectedLength = (int)( remaining() * decoder.averageCharsPerByte() ) + 1;
        CharBuffer out = CharBuffer.allocate( expectedLength );
        for( ; ; )
        {
            CoderResult cr;
            if( hasRemaining() )
            {
                cr = decoder.decode( buf(), out, true );
            }
            else
            {
                cr = decoder.flush( out );
            }

            if( cr.isUnderflow() )
            {
                break;
            }

            if( cr.isOverflow() )
            {
                CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
                out.flip();
                o.put( out );
                out = o;
                continue;
            }

            if( cr.isError() )
            {
              // Revert the buffer back to the previous state.
              limit( oldLimit );
              position( oldPos );
              cr.throwException();
            }
        }

        limit( oldLimit );
        position( end );
View Full Code Here

        int expectedLength = (int)( remaining() * decoder.averageCharsPerByte() ) + 1;
        CharBuffer out = CharBuffer.allocate( expectedLength );
        for( ; ; )
        {
            CoderResult cr;
            if( hasRemaining() )
            {
                cr = decoder.decode( buf(), out, true );
            }
            else
            {
                cr = decoder.flush( out );
            }

            if( cr.isUnderflow() )
            {
                break;
            }

            if( cr.isOverflow() )
            {
                CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
                out.flip();
                o.put( out );
                out = o;
                continue;
            }

            if( cr.isError() )
            {
              // Revert the buffer back to the previous state.
              limit( oldLimit );
              position( oldPos );
              cr.throwException();
            }
        }

        limit( oldLimit );
        position( end );
View Full Code Here

        int expandedState = 0;

        for( ; ; )
        {
            CoderResult cr;
            if( in.hasRemaining() )
            {
                cr = encoder.encode( in, buf(), true );
            }
            else
            {
                cr = encoder.flush( buf() );
            }

            if( cr.isUnderflow() )
            {
                break;
            }
            if( cr.isOverflow() )
            {
                if( isAutoExpand() )
                {
                    switch( expandedState )
                    {
                        case 0:
                            autoExpand( (int)Math.ceil( in.remaining() * encoder.averageBytesPerChar() ) );
                            expandedState ++;
                            break;
                        case 1:
                            autoExpand( (int)Math.ceil( in.remaining() * encoder.maxBytesPerChar() ) );
                            expandedState ++;
                            break;
                        default:
                            throw new RuntimeException( "Expanded by " +
                                                        (int)Math.ceil( in.remaining() * encoder.maxBytesPerChar() ) +
                                                        " but that wasn't enough for '" + val + "'" );
                    }
                    continue;
                }
            }
            else
            {
                expandedState = 0;
            }
            cr.throwException();
        }
        return this;
    }
View Full Code Here

        limit( end );
        encoder.reset();

        for( ; ; )
        {
            CoderResult cr;
            if( in.hasRemaining() )
            {
                cr = encoder.encode( in, buf(), true );
            }
            else
            {
                cr = encoder.flush( buf() );
            }

            if( cr.isUnderflow() || cr.isOverflow() )
            {
                break;
            }
            cr.throwException();
        }

        limit( oldLimit );

        if( position() < end )
View Full Code Here

        int expectedLength = (int)( remaining() * decoder.averageCharsPerByte() ) + 1;
        CharBuffer out = CharBuffer.allocate( expectedLength );
        for( ; ; )
        {
            CoderResult cr;
            if( hasRemaining() )
            {
                cr = decoder.decode( buf(), out, true );
            }
            else
            {
                cr = decoder.flush( out );
            }

            if( cr.isUnderflow() )
            {
                break;
            }

            if( cr.isOverflow() )
            {
                CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
                out.flip();
                o.put( out );
                out = o;
                continue;
            }

            cr.throwException();
        }

        limit( oldLimit );
        position( end );
        return out.flip().toString();
View Full Code Here

        int oldPos = position();
        encoder.reset();

        for( ; ; )
        {
            CoderResult cr;
            if( in.hasRemaining() )
            {
                cr = encoder.encode( in, buf(), true );
            }
            else
            {
                cr = encoder.flush( buf() );
            }

            if( position() - oldPos > maxLength )
            {
                throw new IllegalArgumentException( "The specified string is too long." );
            }

            if( cr.isUnderflow() )
            {
                break;
            }
            if( cr.isOverflow() && isAutoExpand() )
            {
                autoExpand( expectedLength );
                continue;
            }
            cr.throwException();
        }

        // Write the length field
        fill( padValue, padding - ( ( position() - oldPos ) & padMask ) );
        int length = position() - oldPos;
View Full Code Here

                this.charbuffer = CharBuffer.allocate(this.lineBuffersize);
            }
            this.chardecoder.reset();

            for (;;) {
                final CoderResult result = this.chardecoder.decode(
                        this.buffer,
                        this.charbuffer,
                        true);
                if (result.isError()) {
                    result.throwException();
                }
                if (result.isOverflow()) {
                    this.charbuffer.flip();
                    linebuffer.append(
                            this.charbuffer.array(),
                            this.charbuffer.position(),
                            this.charbuffer.remaining());
                    this.charbuffer.clear();
                }
                if (result.isUnderflow()) {
                    break;
                }
            }

            // flush the decoder
View Full Code Here

     *      if true, tell the decoder that all the input bytes are ready.
     */
    private void decode(boolean last) throws IOException {
        buf.flip();
        while(true) {
            CoderResult r = decoder.decode(buf, out, last);
            if(r==CoderResult.OVERFLOW) {
                flushOutput();
                continue;
            }
            if(r==CoderResult.UNDERFLOW) {
                buf.compact();
                return;
            }
            // otherwise treat it as an error
            r.throwException();
        }
    }
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.