Java 8 CoderResult - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 CoderResult class.

This code example shows how to use the following methods:isError, isOverflow, toString
 
    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();
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:isError, isOverflow, toString
This code example shows how to use the following methods:isMalformed, length, toString
        // malformed: low surrogate first is detected as an error. 
        Charset cs = Charset.forName("UTF-32BE");
        CharsetEncoder e = cs.newEncoder();
        ByteBuffer bb = ByteBuffer.allocate(128);
        CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '\udf9f' }), bb, false);
        assertTrue(cr.toString(), cr.isMalformed());
        assertEquals(1, cr.length());
    } 
 
    public void testCharsetEncoderSplitSurrogates_IGNORE() throws Exception {
        testCharsetEncoderSplitSurrogates(CodingErrorAction.IGNORE);
    } 
 
    public void testCharsetEncoderSplitSurrogates_REPORT() throws Exception {
        testCharsetEncoderSplitSurrogates(CodingErrorAction.REPORT);
    } 
 
    public void testCharsetEncoderSplitSurrogates_REPLACE() throws Exception {
        testCharsetEncoderSplitSurrogates(CodingErrorAction.REPLACE);
    } 
6
CodeRank
This code example shows how to use the following methods:isUnderflow
     * Fills the internal char buffer from the reader. 
     * 
     * @throws java.io.IOException If an I/O error occurs 
     */ 
    private void fillBuffer() throws IOException {
        if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) {
            encoderIn.compact();
            int position = encoderIn.position();
            // We don't use Reader#read(CharBuffer) here because it is more efficient 
            // to write directly to the underlying char array (the default implementation 
            // copies data to a temporary char array). 
            int c = reader.read(encoderIn.array(), position, encoderIn.remaining());
            if (c == -1) {
                endOfInput = true;
            } else { 
                encoderIn.position(position + c);
            } 
            encoderIn.flip();
        } 
        encoderOut.compact();
6
CodeRank
This code example shows how to use the following methods:isError, isMalformed, isUnmappable
 
    private void decodeNextCharBuffer() throws CharConversionException {
        CoderResult coderResult = DECODER.decode(byteBuffer, mboxCharBuffer, endOfInputFlag);
        updateEndOfInputFlag(); 
        mboxCharBuffer.flip();
        if (coderResult.isError()) {
            if (coderResult.isMalformed()) {
                throw new CharConversionException("Malformed input!");
            } else if (coderResult.isUnmappable()) {
                throw new CharConversionException("Unmappable character!");
            } 
        } 
    } 
 
    private void updateEndOfInputFlag() { 
        if (byteBuffer.remaining() <= maxMessageSize) {
            endOfInputFlag = true;
        } 
    } 
 
6
CodeRank
This code example shows how to use the following methods:isError, toString
        byte[] arr = getEncodedByteArrayFixture();
        ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice();
        CharBuffer outBuffer = CharBuffer.allocate(arr.length);
        decoder.reset();
        CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
        assertFalse(coderResult.toString(), coderResult.isError());
        decoder.flush(outBuffer);
        outBuffer.flip();
        assertEquals(SAMPLE_STRING, outBuffer.toString().trim());
    } 
 
    // http://code.google.com/p/android/issues/detail?id=4237 
    public void test_ByteArray_decode_with_offset() throws Exception { 
        CharsetDecoder decoder = getCharsetDecoderUnderTest();
        byte[] arr = getEncodedByteArrayFixture();
        arr = prependByteToByteArray(arr, new Integer(1).byteValue());
        int offset = 1;
        ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
        CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
        decoder.reset();
6
CodeRank
This code example shows how to use the following methods:isUnderflow, throwException
		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);
		} 
	} 
 
	/** 
	 *  
	 * build valid http request querystrings from a hashmap that contains name value pairs 
	 *  
	 * Copied from Play Router class 
5
CodeRank
This code example shows how to use the following methods:isError, toString
            return; 
        } 
        bytes.limit(read);
        boolean endOfInput = read < DEFAULT_BUFFER_SIZE;
        CoderResult result = decoder.decode(bytes, chars, endOfInput);
        if (result.isError()) {
            throw new IOException(result.toString());
        } 
        bytes.clear();
        chars.flip();
    } 
 
    /* 
     * Write method for OutputStreamWriter and Channels. 
     */ 
    public static void writeOutputStreamWriter(String str, int offset,
            int count, OutputStream out, ByteBuffer bytes,
            CharsetEncoder encoder, Object lock) throws IOException {
        assertArrayIndex(str.length(), offset, count);
        CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
5
CodeRank
This code example shows how to use the following methods:isUnderflow, throwException
            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);
        } 
        return dst.flip().toString();
    } 
 
    /** 
     * Returns a cached thread-local {@link java.nio.charset.CharsetDecoder} for the specified <tt>charset</tt>. 
     */ 
    private static CharsetDecoder getDecoder(Charset charset)
    { 
5
CodeRank
This code example shows how to use the following methods:isUnderflow, throwException
                cr.throwException();
            } 
 
            cr = charsetDecoder.flush(charByte);
 
            if (!cr.isUnderflow()) {
                cr.throwException();
            } 
        } catch (CharacterCodingException x) {
            // Substitution is always enabled, 
            // so this shouldn't happen 
            // TODO 
        } 
    } 
} 
5
CodeRank

Related Java 8 CoderResult Questions & Answers:

See Code Examples for Java 8 CoderResult Methods: