Example usage for java.nio CharBuffer remaining

List of usage examples for java.nio CharBuffer remaining

Introduction

In this page you can find the example usage for java.nio CharBuffer remaining.

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:com.asakusafw.runtime.io.csv.CsvParser.java

private void emit(int c) throws IOException {
    assert c >= 0;
    CharBuffer buf = lineBuffer;
    if (buf.remaining() == 0) {
        if (buf.capacity() == BUFFER_LIMIT) {
            throw new IOException(
                    MessageFormat.format("Line is too large (near {0}:{1}, size={2}, record-number={3})", path,
                            currentPhysicalHeadLine, BUFFER_LIMIT, currentRecordNumber));
        }//from   ww  w  .  j  ava  2s.  c om
        CharBuffer newBuf = CharBuffer.allocate(Math.min(buf.capacity() * 2, BUFFER_LIMIT));
        newBuf.clear();
        buf.flip();
        newBuf.put(buf);
        buf = newBuf;
        lineBuffer = newBuf;
    }
    buf.put((char) c);
}

From source file:org.codehaus.groovy.grails.web.util.StreamByteBuffer.java

public String readAsString(Charset charset) throws CharacterCodingException {
    int unreadSize = totalBytesUnread();
    if (unreadSize > 0) {
        CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE)
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
        CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
        ByteBuffer buf = null;//w w  w  .  j  a v a2 s.  c o  m
        while (prepareRead() != -1) {
            buf = currentReadChunk.readToNioBuffer();
            boolean endOfInput = (prepareRead() == -1);
            CoderResult result = decoder.decode(buf, charbuffer, endOfInput);
            if (endOfInput) {
                if (!result.isUnderflow()) {
                    result.throwException();
                }
            }
        }
        CoderResult result = decoder.flush(charbuffer);
        if (buf.hasRemaining()) {
            throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
        }
        if (!result.isUnderflow()) {
            result.throwException();
        }
        charbuffer.flip();
        String str;
        if (charbuffer.hasArray()) {
            int len = charbuffer.remaining();
            char[] ch = charbuffer.array();
            if (len != ch.length) {
                ch = ArrayUtils.subarray(ch, 0, len);
            }
            str = StringCharArrayAccessor.createString(ch);
        } else {
            str = charbuffer.toString();
        }
        return str;
    }
    return null;
}

From source file:ubic.basecode.io.ByteArrayConverter.java

/**
 * @param barray// w  ww  . j  a  va2  s. c  om
 * @return char[]
 */
public char[] byteArrayToChars(byte[] barray) {
    if (barray == null)
        return null;

    CharBuffer buf = ByteBuffer.wrap(barray).asCharBuffer();
    char[] array = new char[buf.remaining()];
    buf.get(array);
    return array;

}