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 final CharBuffer put(String str) 

Source Link

Document

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

Usage

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer cb = CharBuffer.allocate(100);

    cb.put("This is a test String");

    cb.flip();/* w w  w .  j  av a 2 s .co  m*/

    System.out.println("hasArray() = " + cb.hasArray());

    char[] carray = cb.array();

    System.out.print("array=");

    for (int i = 0; i < carray.length; i++) {
        System.out.print(carray[i]);
    }

    System.out.println("");
    System.out.flush();
}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5), cb2 = CharBuffer.allocate(5);
    cb1.put('B').put('u').put('f').put('f').put('A');
    cb2.put('B').put('u').put('f').put('f').put('B');
    cb1.rewind();/*  ww  w. ja v  a2 s. c  o  m*/
    cb2.rewind();
    System.out.println(cb1.limit(4).equals(cb2.limit(4)));
}

From source file:BufferEquality.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5), cb2 = CharBuffer.allocate(5);
    cb1.put('B').put('u').put('f').put('f').put('A');
    cb2.put('B').put('u').put('f').put('f').put('B');
    cb1.rewind();// ww  w . ja  va2s  .  c  o  m
    cb2.rewind();
    System.out.println(cb1.limit(4).equals(cb2.limit(4)));
    // Should be "true"
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    char[] chars = new char[60];

    CharBuffer cb = CharBuffer.wrap(chars, 12, 42);

    println(cb);//from   w  w w.ja  v  a 2 s  . c o m

    cb.put("This is a test String");

    cb.flip();

    println(cb);

    cb.clear();

    cb.put("Foobar");

    println(cb);

    for (int i = 0; i < 20; i++) {
        System.out.println("[" + i + "] = " + chars[i]);
    }
}

From source file:MainClass.java

License:asdf

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.allocate(100);

    String string = "asdf";

    for (int i = 0; i < string.length(); i++) {
        buffer.put(string.charAt(i));
    }/*w  w w  .ja v  a  2 s .  co m*/

    buffer.flip();
    drainBuffer(buffer);
    buffer.clear();
}

From source file:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.txt");
    FileOutputStream outputFile = null;
    try {//from   ww  w. j  ava2 s  . c o m
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    DoubleBuffer doubleBuf = buf.asDoubleBuffer();
    buf.position(8);
    CharBuffer charBuf = buf.asCharBuffer();
    for (long prime : primes) {
        String primeStr = "prime = " + prime;
        doubleBuf.put(0, (double) primeStr.length());
        charBuf.put(primeStr);
        buf.position(2 * charBuf.position() + 8);
        LongBuffer longBuf = buf.asLongBuffer();
        longBuf.put(prime);
        buf.position(buf.position() + 8);
        buf.flip();
        try {
            file.write(buf);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        buf.clear();
        doubleBuf.clear();
        charBuf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static byte[] char2byte(String encode, char... chars) {
    Charset cs = Charset.forName(encode);
    CharBuffer cb = CharBuffer.allocate(chars.length);
    cb.put(chars);
    cb.flip();//www  .  j  a  v a  2  s . c o  m
    ByteBuffer bb = cs.encode(cb);
    return bb.array();
}

From source file:MainClass.java

private static void symmetricScramble(CharBuffer buffer) {
    while (buffer.hasRemaining()) {
        buffer.mark();//from   w  ww.j  a  v a2s  .c  om
        char c1 = buffer.get();
        char c2 = buffer.get();
        buffer.reset();
        buffer.put(c2).put(c1);
    }
}

From source file:fi.johannes.kata.ocr.utils.files.CFileOperations.java

public static void createChunksByRowsChars(String inputFile, String outputFolder, int bufferSize, int rows)
        throws FileNotFoundException, IOException {
    File f = new File(inputFile);
    String filename = f.getName();
    BufferedReader bw = new BufferedReader(new FileReader(f));
    CharBuffer buffer = CharBuffer.allocate(bufferSize);
    int j = 0;//from  w  ww . ja  v  a  2  s  . com
    for (int i = 0; i <= rows; i++) {
        String lineStr = bw.readLine();
        if (lineStr != null) {
            char[] line = lineStr.toCharArray();
            if (i == rows) {
                String outputfile = outputFolder + j + "-" + filename;
                writeChunk(buffer, outputfile);
                buffer.clear();
                j++;
                i = 0;
            }
            buffer.put(line);
            buffer.put(System.getProperty("line.separator"));
        } else {
            break;
        }
    }
}

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  ww  .  j a v  a 2s.c  o m*/
 * @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;

    }
}