Example usage for java.nio CharBuffer toString

List of usage examples for java.nio CharBuffer toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representing the current remaining chars of this buffer.

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 ww.  j  ava 2  s  .  c  o m*/

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

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put('j').put('a').put('v').put('a').put('2').put('s');
    ;//w ww  .  j a  va  2  s . co  m
    cb1.rewind();

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

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(new char[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm', });
    cb1.rewind();/*from   www. j a v a  2s.  c  o m*/

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

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    CharBuffer cb = CharBuffer.allocate(100);

    Reader reader = new StringReader(s);

    try {/*from   w ww  . j  a v a 2s .  c  o m*/
        reader.read(cb);

        cb.flip();

        // print the char buffer
        System.out.println(cb.toString());

        reader.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String bytesToStringUTFNIO(byte[] bytes) {
    CharBuffer cBuffer = ByteBuffer.wrap(bytes).asCharBuffer();
    return cBuffer.toString();
}

From source file:Main.java

public static boolean isPureAscii(String v) {
    byte bytearray[] = v.getBytes();
    CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
    try {/*  w  ww.j  av a  2 s  . c  o  m*/
        CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
        r.toString();
    } catch (CharacterCodingException e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static String stripNonASCII(String ret) {
    char[] chars = ret.toCharArray();
    CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < chars.length; i++) {
        try {/*from w  ww  . j  av  a 2s  . c om*/
            char c = chars[i];
            String letter = new String(new char[] { c });
            byte bytearray[] = letter.getBytes();
            CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
            r.toString();
            sb.append(c);
        } catch (CharacterCodingException e) {

        }
    }
    return sb.toString();
}

From source file:Main.java

private static String getBytesAsString(byte[] bytes) {
    if (bytes == null)
        return "null";
    try {/*  ww w  .j  a  v  a 2 s . c  o  m*/
        CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
        CharBuffer r = d.decode(ByteBuffer.wrap(bytes));
        return r.toString();
    } catch (Exception e) {
        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }
}

From source file:Main.java

public static String processRead(SelectionKey key) throws Exception {
    SocketChannel sChannel = (SocketChannel) key.channel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    sChannel.read(buffer);//from  w w w  .java 2s .  c  o  m
    buffer.flip();
    Charset charset = Charset.forName("UTF-8");
    CharsetDecoder decoder = charset.newDecoder();
    CharBuffer charBuffer = decoder.decode(buffer);
    String msg = charBuffer.toString();
    return msg;
}

From source file:org.wso2.carbon.identity.oauth.endpoint.user.impl.UserInforRequestDefaultValidator.java

public static boolean isPureAscii(String requestBody) {
    byte bytearray[] = requestBody.getBytes();
    CharsetDecoder charsetDecoder = Charset.forName(US_ASCII).newDecoder();
    try {/* w  w w.  j  av  a 2s  . co m*/
        CharBuffer charBuffer = charsetDecoder.decode(ByteBuffer.wrap(bytearray));
        charBuffer.toString();
    } catch (CharacterCodingException e) {
        return false;
    }
    return true;
}