Example usage for java.nio CharBuffer position

List of usage examples for java.nio CharBuffer position

Introduction

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

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:ChannelToWriter.java

/**
 * Read bytes from the specified channel, decode them using the specified
 * Charset, and write the resulting characters to the specified writer
 *//*from  www .j  a v  a 2 s .co m*/
public static void copy(ReadableByteChannel channel, Writer writer, Charset charset) throws IOException {
    // Get and configure the CharsetDecoder we'll use
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    // Get the buffers we'll use, and the backing array for the CharBuffer.
    ByteBuffer bytes = ByteBuffer.allocateDirect(2 * 1024);
    CharBuffer chars = CharBuffer.allocate(2 * 1024);
    char[] array = chars.array();

    while (channel.read(bytes) != -1) { // Read from channel until EOF
        bytes.flip(); // Switch to drain mode for decoding
        // Decode the byte buffer into the char buffer.
        // Pass false to indicate that we're not done.
        decoder.decode(bytes, chars, false);

        // Put the char buffer into drain mode, and write its contents
        // to the Writer, reading them from the backing array.
        chars.flip();
        writer.write(array, chars.position(), chars.remaining());

        // Discard all bytes we decoded, and put the byte buffer back into
        // fill mode. Since all characters were output, clear that buffer.
        bytes.compact(); // Discard decoded bytes
        chars.clear(); // Clear the character buffer
    }

    // At this point there may still be some bytes in the buffer to decode
    // So put the buffer into drain mode call decode() a final time, and
    // finish with a flush().
    bytes.flip();
    decoder.decode(bytes, chars, true); // True means final call
    decoder.flush(chars); // Flush any buffered chars
    // Write these final chars (if any) to the writer.
    chars.flip();
    writer.write(array, chars.position(), chars.remaining());
    writer.flush();
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Extends the size of <code>buf</code> to at least meet <code>minCap</code>.
 * If <code>buf</code> is too small, then a new buffer is allocated and
 * any existing contents in <code>buf</code> will be transfered.  The position
 * of the new buffer will be that of the old buffer if it was not <code>null</code>, and
 * the previous mark will be discarded if one was set.
 * /* w  w  w  .j  a v  a  2 s  . c  om*/
 * @param buf the input <code>ByteBuffer</code>
 * @param minCap the minimum capacity
 * @return a <code>CharBuffer</code> that can meet <code>minCap</code>
 */
public static CharBuffer growBuffer(CharBuffer buf, int minCap) {
    int myLimit = buf != null ? buf.limit() : 0;
    // limit can accomidate capacity requirements
    if (buf != null && myLimit >= minCap)
        return buf;
    int myCap = buf != null ? buf.capacity() : 0;
    // capacity can accomidate but limit is too small
    if (buf != null && myCap >= minCap) {
        buf.limit(myCap);
        return buf;
    } else //if(myCap < minCap)
    {
        CharBuffer newBuffer = null;
        if (myCap == 0)
            myCap = 1;
        while (myCap < minCap)
            myCap <<= 1;
        //         if(buf != null && buf.isDirect())
        //            newBuffer = CharBuffer.allocateDirect(myCap);
        //         else
        newBuffer = CharBuffer.allocate(myCap);
        // copy contents of original buffer
        if (buf != null) {
            int pos = buf.position();
            buf.clear();
            newBuffer.put(buf);
            newBuffer.position(pos);
        }
        return newBuffer;

    }
}

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

private void rewindCharacter() {
    CharBuffer buf = readerBuffer;
    assert buf.position() > 0;
    buf.position(buf.position() - 1);
}

From source file:org.apache.tajo.util.StringUtils.java

public static char[] convertBytesToChars(byte[] src, Charset charset) {
    CharsetDecoder decoder = charset.newDecoder();
    char[] resultArray = new char[(int) (src.length * decoder.maxCharsPerByte())];

    if (src.length != 0) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(src);
        CharBuffer charBuffer = CharBuffer.wrap(resultArray);

        decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        decoder.reset();//from   ww  w.  j a  va2  s.  co  m

        CoderResult coderResult = decoder.decode(byteBuffer, charBuffer, true);
        if (coderResult.isUnderflow()) {
            coderResult = decoder.flush(charBuffer);

            if (coderResult.isUnderflow()) {
                if (resultArray.length != charBuffer.position()) {
                    resultArray = Arrays.copyOf(resultArray, charBuffer.position());
                }
            }
        }
    }

    return resultArray;
}

From source file:uk.ac.cam.caret.sakai.rwiki.utils.NameHelper.java

private static String normalize(final String nameToNormalize, final boolean isPageName) {
    char[] chars = nameToNormalize.toCharArray();
    int charBufferLength = chars.length + 1 + (isPageName ? DEFAULT_PAGE.length() : 0);
    CharBuffer name = CharBuffer.allocate(charBufferLength);

    int wordStart = 0;

    boolean addSeparator = true;
    boolean addWhiteSpaceOrSeparator = true;
    int numberOfSeparators = 0;

    for (int i = 0; i < chars.length; i++) {
        char c = chars[i];

        if (c == SPACE_SEPARATOR) {
            if (!addWhiteSpaceOrSeparator) {
                name.put(chars, wordStart, i - wordStart);
            }/*  w  ww  . j  ava 2 s  . c  om*/
            addSeparator = true;
            addWhiteSpaceOrSeparator = true;
        } else if (Character.isWhitespace(c)) {
            if (!addWhiteSpaceOrSeparator) {
                name.put(chars, wordStart, i - wordStart);

            }
            addWhiteSpaceOrSeparator = true;
        } else if (addSeparator) {
            name.put(SPACE_SEPARATOR);
            if (++numberOfSeparators > 2) {
                chars[i] = Character.toLowerCase(c);
            }
            wordStart = i;
            addSeparator = false;
            addWhiteSpaceOrSeparator = false;
        } else if (addWhiteSpaceOrSeparator) {
            addWhiteSpaceOrSeparator = false;
            wordStart = i;
            name.put(' ');
            if (numberOfSeparators > 2) {
                chars[i] = Character.toLowerCase(c);
            }
        } else {
            if (numberOfSeparators > 2) {
                chars[i] = Character.toLowerCase(c);
            }
        }

    }

    if (addSeparator && isPageName) {
        name.put(SPACE_SEPARATOR);
        name.put(DEFAULT_PAGE);
    } else if (!addWhiteSpaceOrSeparator) {
        name.put(chars, wordStart, chars.length - wordStart);
    }

    int position = name.position();
    name.position(0);
    name.limit(position);

    return name.toString();
}