Example usage for java.nio CharBuffer limit

List of usage examples for java.nio CharBuffer limit

Introduction

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

Prototype

public final Buffer limit(int newLimit) 

Source Link

Document

Sets the limit of this buffer.

Usage

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();//  w  w w .  j  av  a  2s . 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();/*from www .  j  av a 2s .  c o m*/
    cb2.rewind();
    System.out.println(cb1.limit(4).equals(cb2.limit(4)));
    // Should be "true"
}

From source file:uk.ac.cam.caret.sakai.rwiki.utils.NameHelper.java

private static String normalize(final String nameToNormalize, final boolean isPageName) {
    char[] chars = nameToNormalize.toCharArray();
    int charBufferLength = chars.length + 1 + (isPageName ? DEFAULT_PAGE.length() : 0);
    CharBuffer name = CharBuffer.allocate(charBufferLength);

    int wordStart = 0;

    boolean addSeparator = true;
    boolean addWhiteSpaceOrSeparator = true;
    int numberOfSeparators = 0;

    for (int i = 0; i < chars.length; i++) {
        char c = chars[i];

        if (c == SPACE_SEPARATOR) {
            if (!addWhiteSpaceOrSeparator) {
                name.put(chars, wordStart, i - wordStart);
            }/*from   ww w .ja va  2 s  .  c  o m*/
            addSeparator = true;
            addWhiteSpaceOrSeparator = true;
        } else if (Character.isWhitespace(c)) {
            if (!addWhiteSpaceOrSeparator) {
                name.put(chars, wordStart, i - wordStart);

            }
            addWhiteSpaceOrSeparator = true;
        } else if (addSeparator) {
            name.put(SPACE_SEPARATOR);
            if (++numberOfSeparators > 2) {
                chars[i] = Character.toLowerCase(c);
            }
            wordStart = i;
            addSeparator = false;
            addWhiteSpaceOrSeparator = false;
        } else if (addWhiteSpaceOrSeparator) {
            addWhiteSpaceOrSeparator = false;
            wordStart = i;
            name.put(' ');
            if (numberOfSeparators > 2) {
                chars[i] = Character.toLowerCase(c);
            }
        } else {
            if (numberOfSeparators > 2) {
                chars[i] = Character.toLowerCase(c);
            }
        }

    }

    if (addSeparator && isPageName) {
        name.put(SPACE_SEPARATOR);
        name.put(DEFAULT_PAGE);
    } else if (!addWhiteSpaceOrSeparator) {
        name.put(chars, wordStart, chars.length - wordStart);
    }

    int position = name.position();
    name.position(0);
    name.limit(position);

    return name.toString();
}