Example usage for java.nio CharBuffer hasArray

List of usage examples for java.nio CharBuffer hasArray

Introduction

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

Prototype

public final boolean hasArray() 

Source Link

Document

Indicates whether this buffer is based on a char array and is read/write.

Usage

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(50);
    cb1.append("java2s.com");
    cb1.rewind();/*w w  w.ja  v a  2 s. c o  m*/

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

From source file:Main.java

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

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

    cb.flip();/*ww w  .  j a va  2  s.  c  o  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]);
    }

}

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 .c  o  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:com.netscape.cmsutil.crypto.CryptoUtil.java

public static char[] bytesToChars(byte[] bytes) {
    if (bytes == null)
        return null;

    Charset charset = Charset.forName("UTF-8");
    CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(bytes));
    char[] result = Arrays.copyOf(charBuffer.array(), charBuffer.limit());

    //Clear up the CharBuffer we just created
    if (charBuffer.hasArray()) {
        char[] contentsToBeErased = charBuffer.array();
        CryptoUtil.obscureChars(contentsToBeErased);
    }//from   w  w w. j  av  a  2 s  .  co m
    return result;
}

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;//from  ww w .  jav  a  2s.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;
}