Example usage for java.nio CharBuffer get

List of usage examples for java.nio CharBuffer get

Introduction

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

Prototype

public abstract char get();

Source Link

Document

Returns the char at the current position and increases the position by 1.

Usage

From source file:Main.java

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

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

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();//from ww w.j  a va 2  s.c  o m
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();//from  w  w  w  .j a va 2  s.  c  om
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
        System.out.println(bb.position() + " -> " + bb.get());
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
    FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
    System.out.println("Float Buffer");
    while (fb.hasRemaining())
        System.out.println(fb.position() + " -> " + fb.get());
    IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
    System.out.println("Int Buffer");
    while (ib.hasRemaining())
        System.out.println(ib.position() + " -> " + ib.get());
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
        System.out.println(lb.position() + " -> " + lb.get());
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
        System.out.println(sb.position() + " -> " + sb.get());
    DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
    System.out.println("Double Buffer");
    while (db.hasRemaining())
        System.out.println(db.position() + " -> " + db.get());
}

From source file:BufferConverter.java

public static void main(String[] arguments) {
    try {//  w  ww  . jav  a 2s.c o  m
        String data = "friends.dat";
        FileInputStream inData = new FileInputStream(data);
        FileChannel inChannel = inData.getChannel();
        long inSize = inChannel.size();
        ByteBuffer source = ByteBuffer.allocate((int) inSize);
        inChannel.read(source, 0);
        source.position(0);
        for (int i = 0; source.remaining() > 0; i++)
            System.out.print(source.get() + " ");

        source.position(0);
        Charset ascii = Charset.forName("US-ASCII");
        CharsetDecoder toAscii = ascii.newDecoder();
        CharBuffer destination = toAscii.decode(source);
        destination.position(0);
        System.out.println("\n\nNew character data:");
        for (int i = 0; destination.remaining() > 0; i++)
            System.out.print(destination.get());
    } catch (Exception ioe) {
        System.out.println(ioe.getMessage());
    }
}

From source file:MainClass.java

License:asdf

private static void drainBuffer(CharBuffer buffer) {
    while (buffer.hasRemaining()) {
        System.out.print(buffer.get());
    }//from   w w  w  .ja va 2 s  .  c  o  m

    System.out.println("");
}

From source file:MainClass.java

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

From source file:Main.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string//from   www. jav  a 2 s. c  om
 */
private static String urldecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:com.gistlabs.mechanize.util.apache.URLEncodedUtils.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 * //from www . j a  va 2 s.com
 * @param content the portion to decode
 * @param charset the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return
 */
private static String urldecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null)
        return null;
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1)
                bb.put((byte) ((u << 4) + l));
            else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+')
            bb.put((byte) ' ');
        else
            bb.put((byte) c);
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:com.mcxiaoke.next.http.util.URLUtils.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string//w  w w . j  a v  a2  s. c om
 */
private static String urlDecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    final ByteBuffer bb = ByteBuffer.allocate(content.length());
    final CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        final char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            final char uc = cb.get();
            final char lc = cb.get();
            final int u = Character.digit(uc, 16);
            final int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:com.odiago.flumebase.io.CharBufferUtils.java

/**
 * Parses a CharSequence into an integer in base 10.
 *//*from   w w w  .j  a  v a 2  s. co  m*/
public static int parseInt(CharBuffer chars) throws ColumnParseException {
    int result = 0;

    final int limit = chars.limit();
    final int start = chars.position();
    if (0 == limit - start) {
        // The empty string can not be parsed as an integer.
        throw new ColumnParseException("No value provided");
    }

    boolean isNegative = false;
    for (int pos = start; pos < limit; pos++) {
        char cur = chars.get();
        if (pos == start && cur == '-') {
            isNegative = true;
            if (limit - start == 1) {
                // "-" is not an integer we accept.
                throw new ColumnParseException("No integer part provided");
            }
        } else if (Character.isDigit(cur)) {
            byte digitVal = (byte) (cur - '0');
            result = result * 10 - digitVal;
            // TODO: Detect over/underflow and signal exception?
        } else {
            throw new ColumnParseException("Invalid character in number");
        }
    }

    // We built up the value as a negative, to use the larger "half" of the
    // integer range. If it's not negative, flip it on return.
    return isNegative ? result : -result;
}