Example usage for java.lang StringBuffer setLength

List of usage examples for java.lang StringBuffer setLength

Introduction

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

Prototype

@Override
public synchronized void setLength(int newLength) 

Source Link

Usage

From source file:Main.java

/**
 * If necessary, adds zeros to the beginning of a value so that the total
 * length matches the given precision, otherwise trims the right digits.
 * Then if maxSize is smaller than precision, trims the right digits to
 * maxSize. Negative values are treated as positive
 *///  w  w  w  .  j  a v  a 2s .  c om
public static String toZeroPaddedString(long value, int precision, int maxSize) {

    StringBuffer sb = new StringBuffer();

    if (value < 0) {
        value = -value;
    }

    String s = Long.toString(value);

    if (s.length() > precision) {
        s = s.substring(precision);
    }

    for (int i = s.length(); i < precision; i++) {
        sb.append('0');
    }

    sb.append(s);

    if (maxSize < precision) {
        sb.setLength(maxSize);
    }

    return sb.toString();
}

From source file:corner.util.VectorUtils.java

/**
 * ??.//from  ww w  .  j av a  2s. co  m
 * 
 * @param list
 *            ??.
 * @return ???.
 * @TODO ????.
 */
public static <T> Vector<Double> sumList(List<Vector<T>> list) {
    Vector<Double> r = new Vector<Double>();
    if (list.size() == 0) {
        return r;
    }
    StringBuffer sb = new StringBuffer();

    int width = list.get(0).size();
    for (int i = 0; i < width; i++) {
        for (Vector<T> v : list) {
            sb.append(v.get(i));
            sb.append("+");
        }
        sb.append("0");

        r.add(i, expr(sb.toString()));

        sb.setLength(0);
    }

    return r;
}

From source file:com.bstek.dorado.util.PathUtils.java

public static String concatPath(String... paths) {
    StringBuffer result = new StringBuffer();
    for (String path : paths) {
        if (StringUtils.isEmpty(path)) {
            continue;
        }/*from w ww .j a  va2 s  . c  o  m*/
        if (result.length() > 0) {
            boolean endsWithDelim = (result.charAt(result.length() - 1) == PATH_DELIM);
            boolean startsWithDelim = (path.charAt(0) == PATH_DELIM);
            if (endsWithDelim) {
                if (startsWithDelim) {
                    result.setLength(result.length() - 1);
                }
            } else if (!startsWithDelim)
                result.append(PATH_DELIM);
        }
        result.append(path);
    }
    return result.toString();
}

From source file:opennlp.tools.util.Span.java

  public static String[] spansToStrings(Span[] spans, String[] tokens) {
  String[] chunks = new String[spans.length];
  StringBuffer cb = new StringBuffer();
  for (int si = 0, sl = spans.length; si < sl; si++) {
    cb.setLength(0);
    for (int ti=spans[si].getStart();ti<spans[si].getEnd();ti++) {
      cb.append(tokens[ti]).append(" ");
    }/*from w w w. j a va  2s . c om*/
    chunks[si]=cb.substring(0, cb.length()-1);
  }
  return chunks;
}

From source file:com.digitalpebble.storm.crawler.protocol.http.HttpResponse.java

private static int readLine(PushbackInputStream in, StringBuffer line, boolean allowContinuedLine)
        throws IOException {
    line.setLength(0);
    for (int c = in.read(); c != -1; c = in.read()) {
        switch (c) {
        case '\r':
            if (peek(in) == '\n') {
                in.read();/*from  w  w w .  j a va 2 s .  c  o  m*/
            }
        case '\n':
            if (line.length() > 0) {
                // at EOL -- check for continued line if the current
                // (possibly continued) line wasn't blank
                if (allowContinuedLine)
                    switch (peek(in)) {
                    case ' ':
                    case '\t': // line is continued
                        in.read();
                        continue;
                    }
            }
            return line.length(); // else complete
        default:
            line.append((char) c);
        }
    }
    throw new EOFException();
}

From source file:Main.java

public static String unescapeCode(String str) throws IOException {
    StringBuffer sb = new StringBuffer(str.length());
    int sz = str.length();
    StringBuffer unicode = new StringBuffer(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            unicode.append(ch);/*ww  w .j  av a 2  s . co  m*/
            if (unicode.length() == 4) {
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    sb.append((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    // ignore
                }
            }
            continue;
        }
        if (hadSlash) {
            hadSlash = false;
            switch (ch) {
            case '\\':
                sb.append('\\');
                break;
            case '\'':
                sb.append('\'');
                break;
            case '\"':
                sb.append('"');
                break;
            case 'r':
                sb.append('\r');
                break;
            case 'f':
                sb.append('\f');
                break;
            case 't':
                sb.append('\t');
                break;
            case 'n':
                sb.append('\n');
                break;
            case 'b':
                sb.append('\b');
                break;
            case 'u': {
                inUnicode = true;
                break;
            }
            default:
                sb.append(ch);
                break;
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        sb.append(ch);
    }
    if (hadSlash) {
        sb.append('\\');
    }
    return sb.toString();
}

From source file:azkaban.utils.Utils.java

public static String flattenToString(Collection<?> collection, String delimiter) {
    StringBuffer buffer = new StringBuffer();
    for (Object obj : collection) {
        buffer.append(obj.toString());/*from ww w  .ja v  a  2s.  c om*/
        buffer.append(',');
    }

    if (buffer.length() > 0) {
        buffer.setLength(buffer.length() - 1);
    }
    return buffer.toString();
}

From source file:com.stratuscom.harvester.codebase.ClassServer.java

/**
 * Read the request/response and return the initial line.
 */// w  ww .j  a  v  a 2  s. c o m
private static String getInput(Socket sock, boolean isRequest) throws IOException {
    BufferedInputStream in = new BufferedInputStream(sock.getInputStream(), 256);
    StringBuffer buf = new StringBuffer(80);
    do {
        if (!readLine(in, buf)) {
            return null;
        }
    } while (isRequest && buf.length() == 0);
    String initial = buf.toString();
    do {
        buf.setLength(0);
    } while (readLine(in, buf) && buf.length() > 0);
    return initial;
}

From source file:com.cenrise.test.azkaban.Utils.java

public static String flattenToString(final Collection<?> collection, final String delimiter) {
    final StringBuffer buffer = new StringBuffer();
    for (final Object obj : collection) {
        buffer.append(obj.toString());/*ww  w .ja v  a 2s .com*/
        buffer.append(delimiter);
    }

    if (buffer.length() > 0) {
        buffer.setLength(buffer.length() - 1);
    }
    return buffer.toString();
}

From source file:com.pureinfo.srm.auth.action.UserAuthListAction.java

private static String makeActionButton(String _sAction, String _sText, String _sImage) {
    // <BUTTON
    // onClick=\"mainForm.action='../test.jsp?action='+'adee';mainForm.submit();\">
    StringBuffer sbuff = new StringBuffer();
    sbuff.append(//from www .jav a 2s .c  om
            "<BUTTON class=\"img_button\" onClick=\"if(count()<1)alert('');else{if(confirm('")
            .append(_sText).append("')){mainForm.action='../auth/UserManage.do?action='+'").append(_sAction)
            .append("';mainForm.submit();}}\"><img src=\"../images/").append(_sImage).append(".gif\">")
            .append(_sText);
    try {
        return sbuff.toString();
    } finally {
        sbuff.setLength(0);
    }

}