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 abstract CharBuffer put(int index, char c);

Source Link

Document

Writes a char to the specified index of this buffer; the position is not changed.

Usage

From source file:Main.java

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

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

From source file:Main.java

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

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

From source file:Main.java

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

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

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();//w  ww . j  a  va2s  . com

    System.out.println(Arrays.toString(cb1.array()));

}

From source file:Main.java

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

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

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();/*from   w ww .  ja  va2  s  . co m*/

    char[] charArray = new char[5];
    cb1.get(charArray, 0, 2);
    System.out.println(Arrays.toString(charArray));

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();/*from w  w w .  ja v a2 s  .  co  m*/

    char[] charArray = new char[5];
    cb1.get(charArray);
    System.out.println(Arrays.toString(charArray));

}

From source file:jenkins.plugins.publish_over_cifs.CifsHostConfiguration.java

@SuppressWarnings("PMD.EmptyCatchBlock")
private static String encode(final String raw) {
    if (raw == null)
        return null;
    final StringBuilder encoded = new StringBuilder(raw.length() * ESCAPED_BUILDER_SIZE_MULTIPLIER);
    final CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
    final CharBuffer buffer = CharBuffer.allocate(1);
    for (final char c : raw.toCharArray()) {
        if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            encoded.append(c);//from w ww.j av a2s . c o m
        } else {
            buffer.put(0, c);
            buffer.rewind();
            try {
                final ByteBuffer bytes = encoder.encode(buffer);
                while (bytes.hasRemaining()) {
                    final byte oneByte = bytes.get();
                    encoded.append('%');
                    encoded.append(toDigit((oneByte >> HI_TO_LOW_NIBBLE_BIT_SHIFT) & LOW_NIBBLE_BIT_MASK));
                    encoded.append(toDigit(oneByte & LOW_NIBBLE_BIT_MASK));
                }
            } catch (final CharacterCodingException cce) {
                throw new BapPublisherException(Messages.exception_encode_cce(cce.getLocalizedMessage()), cce);
            }
        }
    }
    return encoded.toString();
}

From source file:hudson.Util.java

/**
 * Encode a single path component for use in an HTTP URL.
 * Escapes all non-ASCII, general unsafe (space and "#%<>[\]^`{|}~)
 * and HTTP special characters (/;:?) as specified in RFC1738,
 * plus backslash (Windows path separator).
 * Note that slash(/) is encoded, so the given string should be a
 * single path component used in constructing a URL.
 * Method name inspired by PHP's rawencode.
 *//* www  .j  a va2s.c o  m*/
public static String rawEncode(String s) {
    boolean escaped = false;
    StringBuilder out = null;
    CharsetEncoder enc = null;
    CharBuffer buf = null;
    char c;
    for (int i = 0, m = s.length(); i < m; i++) {
        c = s.charAt(i);
        if ((c < 64 || c > 90) && (c < 97 || c > 122) && (c < 38 || c > 57 || c == 47) && c != 61 && c != 36
                && c != 33 && c != 95) {
            if (!escaped) {
                out = new StringBuilder(i + (m - i) * 3);
                out.append(s.substring(0, i));
                enc = Charset.forName("UTF-8").newEncoder();
                buf = CharBuffer.allocate(1);
                escaped = true;
            }
            // 1 char -> UTF8
            buf.put(0, c);
            buf.rewind();
            try {
                for (byte b : enc.encode(buf).array()) {
                    out.append('%');
                    out.append(toDigit((b >> 4) & 0xF));
                    out.append(toDigit(b & 0xF));
                }
            } catch (CharacterCodingException ex) {
            }
        } else if (escaped) {
            out.append(c);
        }
    }
    return escaped ? out.toString() : s;
}

From source file:hudson.Util.java

/**
 * Encode a single path component for use in an HTTP URL.
 * Escapes all non-ASCII, general unsafe (space and "#%<>[\]^`{|}~)
 * and HTTP special characters (/;:?) as specified in RFC1738.
 * (so alphanumeric and !@$&*()-_=+',. are not encoded)
 * Note that slash(/) is encoded, so the given string should be a
 * single path component used in constructing a URL.
 * Method name inspired by PHP's rawurlencode.
 *//*from w w  w . j  a v  a 2 s.  c o m*/
public static String rawEncode(String s) {
    boolean escaped = false;
    StringBuilder out = null;
    CharsetEncoder enc = null;
    CharBuffer buf = null;
    char c;
    for (int i = 0, m = s.length(); i < m; i++) {
        c = s.charAt(i);
        if (c > 122 || uriMap[c]) {
            if (!escaped) {
                out = new StringBuilder(i + (m - i) * 3);
                out.append(s.substring(0, i));
                enc = Charset.forName("UTF-8").newEncoder();
                buf = CharBuffer.allocate(1);
                escaped = true;
            }
            // 1 char -> UTF8
            buf.put(0, c);
            buf.rewind();
            try {
                ByteBuffer bytes = enc.encode(buf);
                while (bytes.hasRemaining()) {
                    byte b = bytes.get();
                    out.append('%');
                    out.append(toDigit((b >> 4) & 0xF));
                    out.append(toDigit(b & 0xF));
                }
            } catch (CharacterCodingException ex) {
            }
        } else if (escaped) {
            out.append(c);
        }
    }
    return escaped ? out.toString() : s;
}