Java 8 CharBuffer - 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 CharBuffer class.

This code example shows how to use the following methods:array, compact, flip, position, remaining
     * 
     * @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();
        lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput);

7
CodeRank
This code example shows how to use the following methods:clear, limit, position, rewind, toString
                                         //   -> "   Fruit Titanesque, Inc." 
        test(cb, "   Fruit Titanesque, Inc.");
    } 
 
    private static void test(CharBuffer cb, String exp) {
        cb.limit(cb.position());
        cb.rewind();
        if (!cb.toString().equals(exp))
            throw new RuntimeException("expect: '" + exp + "'; got: '"
                                       + cb.toString() + "'");
        cb.clear();
    } 
} 
7
CodeRank
This code example shows how to use the following methods:capacity, limit, position, hasArray, toString
	public void readChars(char[] buf, int off, int n) throws IOException {
		fBuffer.get(buf, off, n);
	} 
 
	public void readChars(StringBuffer buf, int n) throws IOException {
		int pos = fBuffer.position();
		if (fBuffer.hasArray()) {
			buf.append(fBuffer.array(), fBuffer.arrayOffset() + pos, n);
		} else { 
			fBuffer.limit(pos+n);
			String str = fBuffer.toString();
			assert str.length() == n;
			buf.append(str);
			fBuffer.limit(fBuffer.capacity());
		} 
		fBuffer.position(pos + n);
	} 
 
	public int length() { 
		return fBuffer.length();
	} 
7
CodeRank
This code example shows how to use the following methods:limit, position, array, arrayOffset
            byte[] sa = src.array();
            int sp = src.arrayOffset() + src.position();
            int sl = src.arrayOffset() + src.limit();
            assert (sp <= sl);
            sp = (sp <= sl ? sp : sl);
            char[] da = dst.array();
            int dp = dst.arrayOffset() + dst.position();
            int dl = dst.arrayOffset() + dst.limit();
            assert (dp <= dl);
            dp = (dp <= dl ? dp : dl);
 
 
            try { 
                while (sp < sl) {
                    if ( sl - sp < 2) {
                        return CoderResult.UNDERFLOW;
                    } 
                    int b1 = sa[sp] & 0xFF | 0x80;
                    int b2 = sa[sp + 1] & 0xFF | 0x80;
                    char c = decodeDouble(b1, b2);

7
CodeRank
This code example shows how to use the following methods:limit, position, array, arrayOffset, hasArray
            return 0; 
        } 
    } 
 
    private final int getArray(CharBuffer in) {
        if(in.hasArray()){
            // BEGIN android-changed: take arrayOffset into account 
            input = in.array();
            inEnd = in.arrayOffset() + in.limit();
            return in.arrayOffset() + in.position() + savedInputHeldLen;/*exclude the number fo bytes held in previous conversion*/
            // END android-changed 
        }else{ 
            inEnd = in.remaining();
            // BEGIN android-added 
            if (allocatedInput == null || (inEnd > allocatedInput.length)) {
                allocatedInput = new char[inEnd];
            } 
            input = allocatedInput;
            // END android-added 
            // save the current position 

7
CodeRank
This code example shows how to use the following methods:capacity, position
                // Allocate and fill the index buffer. 
                gl11.glGenBuffers(1, buffer, 0);
                mIndexBufferIndex = buffer[0];
                gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex);
                // A char is 2 bytes. 
                final int indexSize = mIndexBuffer.capacity() * 2;
                mIndexBuffer.position(0);
                gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indexSize, mIndexBuffer, GL11.GL_STATIC_DRAW);
 
                // Unbind the element array buffer. 
                gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
            } 
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:clear, flip
        } 
    } 
 
    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:capacity, limit, position, remaining
    @Override 
    public String toString()
    { 
        StringBuilder str = new StringBuilder();
        str.append("Utf8CharBuffer@").append(hashCode());
        str.append("[p=").append(buffer.position());
        str.append(",l=").append(buffer.limit());
        str.append(",c=").append(buffer.capacity());
        str.append(",r=").append(buffer.remaining());
        str.append("]");
        return str.toString();
    } 
} 
7
CodeRank
This code example shows how to use the following methods:clear, limit, mark, position, reset
            buflen = len;
        } 
 
        //reset buffers, so we start from the beginning of them 
        buf.clear();
        charBuffer.clear();
 
        //mark the start so we can reset back after we have read in the string 
        charBuffer.mark();
 
        // Set the limit of the byte buffer, so we know where to stop the string. 
        // Or else you get characters from old strings that was longer then this one 
        buf.limit(len);
 
        //Read the string 
        stream.readFully(buf.array(), 0, len);
 
        //Decode it with correct encoding 
        try { 
            decoder.decode(buf, charBuffer, false);

7
CodeRank
This code example shows how to use the following methods:array, position, rewind
			} 
		} 
	} 
 
	private void _flushBuffer() throws IOException {
		if (_outputCharBuffer.position() > 0) {
			_writer.write(
				_outputCharBuffer.array(), 0, _outputCharBuffer.position());
 
			_outputCharBuffer.rewind();
		} 
	} 
 
	private static final int _DEFAULT_OUTPUT_BUFFER_SIZE = 8192;
 
	private boolean _autoFlush;
	private CharsetDecoder _charsetDecoder;
	private String _charsetName;
	private ByteBuffer _inputByteBuffer;
	private CharBuffer _outputCharBuffer;
7
CodeRank

Related Java 8 CharBuffer Questions & Answers:

See Code Examples for Java 8 CharBuffer Methods: