Code example for CharBuffer

Methods: clear, flip, limit

0
    /* 
     * Refill the buffer from wrapped InputStream. 
     */ 
    private static void fillBuf(InputStream in, ByteBuffer bytes,
            CharBuffer chars, CharsetDecoder decoder) throws IOException {
        chars.clear();
        int read = 0;
        try { 
            read = in.read(bytes.array());
        } catch (IOException e) {
            chars.limit(0);
            throw e;
        } 
        if (read == -1) {
            chars.limit(0);
            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. 
     */