Example usage for java.lang StringBuffer length

List of usage examples for java.lang StringBuffer length

Introduction

In this page you can find the example usage for java.lang StringBuffer length.

Prototype

@Override
    public synchronized int length() 

Source Link

Usage

From source file:Unsigned.java

/**
 * Return the unsigned value of the number.
 * /*from  w  ww .j a  va  2  s  .c  om*/
 * @param number
 *            an int value
 * @return the unsigned value
 */
public static long unsigned(int number) {
    long result = 0;
    BinaryFormat format = new BinaryFormat();
    String binary = format.format(number);

    StringBuffer buffer = new StringBuffer(binary);
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }

    return (result);
}

From source file:com.aliyun.odps.local.common.utils.SchemaUtils.java

public static void generateSchemaFile(TableMeta table, List<Integer> indexes, File dir) {

    if (table == null || dir == null) {
        throw new IllegalArgumentException("Missing arguments: table|dir");
    }//from   w w  w.ja v  a  2s  .  c  o m

    if (StringUtils.isBlank(table.getProjName()) || StringUtils.isBlank(table.getTableName())) {
        throw new IllegalArgumentException(
                "Project|table is empty when table.getProjName()|table.getTableName()");
    }

    TableInfo tableInfo = TableInfo.builder().projectName(table.getProjName()).tableName(table.getTableName())
            .build();

    LOG.info("Start to write table scheme : " + tableInfo + "-->" + dir.getAbsolutePath());

    StringBuffer sb = new StringBuffer();
    sb.append("project=" + table.getProjName());
    sb.append("\n");
    sb.append("table=" + table.getTableName());
    sb.append("\n");

    StringBuffer sb1 = new StringBuffer();
    Column[] columns = table.getCols();
    int length = indexes == null ? table.getCols().length : indexes.size();
    for (int i = 0; i < length; i++) {
        int index = indexes == null ? i : indexes.get(i);
        Column col = columns[index];
        if (sb1.length() > 0) {
            sb1.append(",");
        }
        sb1.append(col.getName() + ":" + col.getType().toString());
    }

    sb.append("columns=" + sb1.toString());
    sb.append("\n");

    Column[] partitions = table.getPartitions();
    if (partitions != null && partitions.length > 0) {
        sb1 = new StringBuffer();
        for (int i = 0; i < partitions.length; i++) {
            if (sb1.length() > 0) {
                sb1.append(",");
            }
            sb1.append(partitions[i].getName() + ":" + partitions[i].getType().toString());
        }
        sb.append("partitions=" + sb1.toString());
        sb.append("\n");
    }

    dir.mkdirs();
    File schemaFile = new File(dir, Constants.SCHEMA_FILE);
    LOG.info("generate schema file: " + schemaFile.getAbsolutePath());
    OutputStream out = null;
    try {
        out = new FileOutputStream(schemaFile);
        String result = sb.toString();
        // ??\n
        result = result.substring(0, result.length() - 1);
        out.write(result.getBytes("utf-8"));
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    LOG.info("Finished to write table scheme : " + tableInfo + "-->" + dir.getAbsolutePath());
}

From source file:bioLockJ.Config.java

/**
 * Set a prop value for a list./* w w  w . jav a  2  s  .  c o  m*/
 * @param name
 * @param list
 */
public static void setProperty(final String name, final List<String> list) {
    final StringBuffer sb = new StringBuffer();
    for (final String val : list) {
        if (sb.length() > 0) // add to existing list string
        {
            sb.append(",");
        }
        sb.append(val);
    }

    props.setProperty(name, sb.toString());
}

From source file:com.googlecode.mycontainer.maven.plugin.ExecMojo.java

private static void addToClasspath(StringBuffer theClasspath, String toAdd) {
    if (theClasspath.length() > 0) {
        theClasspath.append(File.pathSeparator);
    }//from   w  w w  .j a v  a  2s.co m
    theClasspath.append(toAdd);
}

From source file:com.niki.normalizer.Metaphone.java

private static boolean isPreviousChar(StringBuffer string, int index, char c) {
    boolean matches = false;
    if (index > 0 && index < string.length()) {
        matches = string.charAt(index - 1) == c;
    }//  w  w w.  j a va2  s  .c  o  m
    return matches;
}

From source file:com.niki.normalizer.Metaphone.java

private static boolean isNextChar(StringBuffer string, int index, char c) {
    boolean matches = false;
    if (index >= 0 && index < string.length() - 1) {
        matches = string.charAt(index + 1) == c;
    }/*from   www . ja v a  2  s  .c o  m*/
    return matches;
}

From source file:com.jfaker.framework.flow.SnakerHelper.java

public static String getStateJson(ProcessModel model, List<Task> activeTasks, List<HistoryTask> historyTasks) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("{'activeRects':{'rects':[");
    if (activeTasks != null && activeTasks.size() > 0) {
        for (Task task : activeTasks) {
            buffer.append("{'paths':[],'name':'");
            buffer.append(task.getTaskName());
            buffer.append("'},");
        }//  w  w  w  .  jav a 2 s  .  c om
        buffer.deleteCharAt(buffer.length() - 1);
    }
    buffer.append("]}, 'historyRects':{'rects':[");
    if (historyTasks != null && historyTasks.size() > 0) {
        for (HistoryTask historyTask : historyTasks) {
            NodeModel parentModel = model.getNode(historyTask.getTaskName());
            if (parentModel == null)
                continue;
            buffer.append("{'name':'").append(parentModel.getName()).append("','paths':[");
            buffer.append("]},");
        }
        buffer.deleteCharAt(buffer.length() - 1);
    }
    buffer.append("]}}");
    return buffer.toString();
}

From source file:com.edgenius.wiki.render.RenderUtil.java

/**
 * Check if given string is end by a Blocked HTML tag (see RenderUtil.isBlockTag());
 * The "end" means the except spaces or tab mark (it won't be newline mark! please see code in NewLineFilter.replace()
 * , the last visible String must be some tag and tag must be blocked tag.
 * /*from  w  w  w  .  jav  a 2 s  . c  o m*/
 * @param buffer
 */
public static boolean isEndByBlockHtmlTag(StringBuffer buffer) {

    if (buffer != null && buffer.length() > 0) {
        String before = buffer.toString();

        int len = before.length();
        //0 start looking close tag '>'; 1 in last tag string; 2 need find paired tag to check
        int looking = 0;
        StringBuffer tag = null;
        HTMLNode node = null, pair = null;
        for (int idx = len - 1; idx >= 0; idx--) {
            char ch = before.charAt(idx);
            if (looking == 0) {
                if (ch == '>') {
                    looking = 1;
                    tag = new StringBuffer();
                    tag.append(ch);
                    continue;
                } else if (ch != ' ' && ch != '\t') {
                    break;
                }
            } else if (looking == 1) {
                tag.insert(0, ch);
                if (ch == '<') {
                    node = new HTMLNode(tag.toString(), false);
                    if (node.isCloseTag()) {
                        //have to continue to looking up its open tag to decide
                        looking = 2;
                    } else {
                        //it is not close tag, then I can decide if it is close tag now
                        return isBlockTag(node);
                    }
                }
            } else if (looking == 2) {
                if (ch == '>') {
                    looking = 3;
                    tag = new StringBuffer();
                    tag.append(ch);
                    continue;
                }
            } else if (looking == 3) {
                tag.insert(0, ch);
                if (ch == '<') {
                    pair = new HTMLNode(tag.toString(), false);
                    if (pair.isPaired(node)) {
                        return isBlockTag(pair);
                    } else {
                        //it is not paired open tag, have to continue;
                        looking = 2;
                        continue;
                    }
                }
            }

        }
    }
    return false;
}

From source file:ac.elements.parser.ExtendedFunctions.java

/**
 * Trim all occurences of the supplied character from the given String.
 * /*from ww  w  . j a  v a2s . co m*/
 * @param str
 *            the String to check
 * @param ter
 *            the character to be trimmed
 * @return the trimmed String
 */
public static String trimCharacter(String str, char character) {
    if (str == null || str.length() == 0) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(0) == character) {
        buf.deleteCharAt(0);
    }

    while (buf.length() > 0 && buf.charAt(buf.length() - 1) == character) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:com.seer.datacruncher.eventtrigger.DynamicClassLoader.java

private static String extractClasspath(ClassLoader classLoader) {

    StringBuffer classLoaderBuf = new StringBuffer();
    try {/*from   w  ww. java2s .c  o  m*/
        while (classLoader != null) {
            if (classLoader instanceof URLClassLoader) {
                URL urls[] = ((URLClassLoader) classLoader).getURLs();
                for (int i = 0; i < urls.length; i++) {
                    if (classLoaderBuf.length() > 0) {
                        classLoaderBuf.append(File.pathSeparatorChar);
                    }
                    classLoaderBuf.append(URLDecoder.decode(urls[i].getFile(), "UTF-8").toString());
                }
            }
            classLoader = classLoader.getParent();
        }
    } catch (UnsupportedEncodingException e) {
        logger.error("Exception:" + e.getMessage(), e);
    }
    return classLoaderBuf.toString();
}