Example usage for java.nio CharBuffer put

List of usage examples for java.nio CharBuffer put

Introduction

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

Prototype

public CharBuffer put(String str, int start, int end) 

Source Link

Document

Writes chars of the given string to the current position of this buffer, and increases the position by the number of chars written.

Usage

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put("java2s.com", 0, 2);
    cb1.rewind();//from  w w  w.j a  v  a  2  s  .  c o m

    System.out.println(cb1.toString());
}

From source file:Main.java

public static byte[] getBytes(char[] buffer, int offset, int length) {

    CharBuffer cb = CharBuffer.allocate(length);
    cb.put(buffer, offset, length);
    cb.flip();/*ww  w  .j a  v  a2s  .c o m*/

    return Charset.defaultCharset().encode(cb).array();
}

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);
            }/*from ww  w .  java2 s.co  m*/
            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();
}

From source file:org.apache.hadoop.sqoop.util.ResultSetPrinter.java

/**
 * Takes the contents of the StringBuilder and prints it on the OutputStream
 *///from   www. ja v  a 2s .  c om
private void sendToStream(StringBuilder sb, OutputStream os) throws IOException {

    int pos = 0; // current pos in the string builder
    int len = sb.length(); // total length (in characters) to send to os.
    CharBuffer charbuf = bytebuf.asCharBuffer();

    while (pos < len) {
        int copyLen = Math.min(sb.length(), MAX_CHARS);
        sb.getChars(pos, copyLen, charArray, 0);

        charbuf.put(charArray, 0, copyLen);
        os.write(bytebuf.array());

        pos += copyLen;
    }

}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

public void write(OutputStream output) throws IOException {

    baseBuffer.rewind();//  ww w  . j a  v a2  s.  c o m
    checkBuffer.rewind();
    tailBuffer.rewind();

    int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
    int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());

    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));

    dataOutput.writeBoolean(compact);
    dataOutput.writeInt(baseCheckSize);
    dataOutput.writeInt(tailSize);

    WritableByteChannel channel = Channels.newChannel(dataOutput);

    ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    tmpBuffer = ByteBuffer.allocate(tailSize * 2);
    CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
    tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    dataOutput.flush();
}

From source file:org.apache.noggit.JSONParser.java

public int read(CharBuffer cb) throws IOException {
        // empty the buffer and then read direct
        int sz = size();
        if (sz > 0)
            cb.put(buf, start, end);
        int sz2 = in.read(cb);
        if (sz2 >= 0)
            return sz + sz2;
        return sz > 0 ? sz : -1;
    }//from  w w  w.  ja va  2s  .c  o  m

From source file:org.apache.noggit.JSONParser.java

public int read(CharBuffer cb) throws IOException {

        /***************************************************************************
         * int sz = size(); if (sz<=0) return -1; if (sz>0) cb.put(buf, start, sz);
         * return -1;/*  w ww .  ja va  2 s . co m*/
         **************************************************************************/

        int sz = size();
        if (sz > 0)
            cb.put(buf, start, sz);
        start = end;
        while (true) {
            fill();
            int s = size();
            if (s == 0)
                return sz == 0 ? -1 : sz;
            sz += s;
            cb.put(buf, start, s);
        }
    }