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 int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

From source file:GrepSun.java

private static void grep(File f, CharBuffer cb) {
    Matcher lm = linePattern.matcher(cb); // Line matcher
    Matcher pm = null; // Pattern matcher
    int lines = 0;
    while (lm.find()) {
        lines++;/* w  ww . j av a  2s  . c o m*/
        CharSequence cs = lm.group(); // The current line
        if (pm == null)
            pm = pattern.matcher(cs);
        else
            pm.reset(cs);
        if (pm.find())
            System.out.print(f + ":" + lines + ":" + cs);
        if (lm.end() == cb.limit())
            break;
    }
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Extends the size of <code>buf</code> to at least meet <code>minCap</code>.
 * If <code>buf</code> is too small, then a new buffer is allocated and
 * any existing contents in <code>buf</code> will be transfered.  The position
 * of the new buffer will be that of the old buffer if it was not <code>null</code>, and
 * the previous mark will be discarded if one was set.
 * //from w w w.ja va  2 s  . co  m
 * @param buf the input <code>ByteBuffer</code>
 * @param minCap the minimum capacity
 * @return a <code>CharBuffer</code> that can meet <code>minCap</code>
 */
public static CharBuffer growBuffer(CharBuffer buf, int minCap) {
    int myLimit = buf != null ? buf.limit() : 0;
    // limit can accomidate capacity requirements
    if (buf != null && myLimit >= minCap)
        return buf;
    int myCap = buf != null ? buf.capacity() : 0;
    // capacity can accomidate but limit is too small
    if (buf != null && myCap >= minCap) {
        buf.limit(myCap);
        return buf;
    } else //if(myCap < minCap)
    {
        CharBuffer newBuffer = null;
        if (myCap == 0)
            myCap = 1;
        while (myCap < minCap)
            myCap <<= 1;
        //         if(buf != null && buf.isDirect())
        //            newBuffer = CharBuffer.allocateDirect(myCap);
        //         else
        newBuffer = CharBuffer.allocate(myCap);
        // copy contents of original buffer
        if (buf != null) {
            int pos = buf.position();
            buf.clear();
            newBuffer.put(buf);
            newBuffer.position(pos);
        }
        return newBuffer;

    }
}

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 . c om
    return result;
}

From source file:org.nuxeo.common.codec.Crypto.java

/**
 * Utility method to get {@code char[]} from {@code bytes[]} since it is recommended to store passwords in
 * {@code char[]} rather than in {@code String}.<br>
 * The default charset of this Java virtual machine is used. There can be conversion issue with unmappable
 * characters: they will be replaced with the charset's default replacement string.
 *
 * @param bytes byte array to convert/* w  w w  .j av  a 2 s .  c  om*/
 * @return the char array converted from {@code bytes} using the default charset.
 */
public static char[] getChars(byte[] bytes) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    CharBuffer charBuffer = Charset.defaultCharset().decode(byteBuffer);
    return Arrays.copyOfRange(charBuffer.array(), 0, charBuffer.limit());
}