Example usage for org.apache.hadoop.util StringUtils findNext

List of usage examples for org.apache.hadoop.util StringUtils findNext

Introduction

In this page you can find the example usage for org.apache.hadoop.util StringUtils findNext.

Prototype

public static int findNext(String str, char separator, char escapeChar, int start, StringBuilder split) 

Source Link

Document

Finds the first occurrence of the separator character ignoring the escaped separators starting from the index.

Usage

From source file:com.chinamobile.bcbsp.bspcontroller.Counters.java

License:Apache License

/**Extracts a block (data enclosed within delimeters) ignoring escape
 * sequences. Throws ParseException if an incomplete block is found else
 * returns null.//from  w w w . j av a2s. com
 * @param str
 *        split address to find
 * @param open
 *        if the split is open
 * @param close
 *        if the split is close
 * @param index
 *        block write index.
 * @return blocks has got
 */
private static String getBlock(String str, char open, char close, IntWritable index) throws ParseException {
    StringBuilder split = new StringBuilder();
    int next = StringUtils.findNext(str, open, StringUtils.ESCAPE_CHAR, index.get(), split);
    split.setLength(0); // clear the buffer
    if (next >= 0) {
        ++next; // move over '('
        next = StringUtils.findNext(str, close, StringUtils.ESCAPE_CHAR, next, split);
        if (next >= 0) {
            ++next; // move over ')'
            index.set(next);
            return split.toString(); // found a block
        } else {
            throw new ParseException("Unexpected end of block", next);
        }
    }
    return null; // found nothing
}

From source file:org.apache.hama.bsp.Counters.java

License:Apache License

private static String getBlock(String str, char open, char close, IntWritable index) throws ParseException {
    StringBuilder split = new StringBuilder();
    int next = StringUtils.findNext(str, open, StringUtils.ESCAPE_CHAR, index.get(), split);
    split.setLength(0); // clear the buffer
    if (next >= 0) {
        ++next; // move over '('

        next = StringUtils.findNext(str, close, StringUtils.ESCAPE_CHAR, next, split);
        if (next >= 0) {
            ++next; // move over ')'
            index.set(next);//from  w  ww .j a va 2s.  c  o  m
            return split.toString(); // found a block
        } else {
            throw new ParseException("Unexpected end of block", next);
        }
    }
    return null; // found nothing
}

From source file:org.huahinframework.core.util.StringUtil.java

License:Apache License

/**
 * splits this string around matches of the given separator.
 * @param str strings//from   w w  w  . ja v  a  2  s  .c  o  m
 * @param separator separator
 * @param trim include the last separator
 * @return the array of strings computed by splitting this string around matches of the given separator
 */
public static String[] split(String str, String separator, boolean trim) {
    if (str == null) {
        return null;
    }

    char sep = separator.charAt(0);

    ArrayList<String> strList = new ArrayList<String>();
    StringBuilder split = new StringBuilder();
    int index = 0;
    while ((index = StringUtils.findNext(str, sep, StringUtils.ESCAPE_CHAR, index, split)) >= 0) {
        ++index; // move over the separator for next search
        strList.add(split.toString());
        split.setLength(0); // reset the buffer
    }

    strList.add(split.toString());
    // remove trailing empty split(s)
    if (trim) {
        int last = strList.size(); // last split
        while (--last >= 0 && "".equals(strList.get(last))) {
            strList.remove(last);
        }
    }

    return strList.toArray(new String[strList.size()]);
}