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:gov.nih.nci.cabig.ctms.lang.StringTools.java

/**
 * Normalizes the whitespace in the given buffer in-place.
 * This means that the whitespace is stripped from the head and the tail
 * and any contiguous stretches of whitespace are converted into a single
 * space.//from   w w w  . j a v a2  s  .  c  om
 *
 * @param toNormalize
 * @return the passed-in buffer
 */
public static StringBuffer normalizeWhitespace(StringBuffer toNormalize) {
    if (toNormalize == null)
        return null;
    // start with this value == true to completely remove leading whitespace
    boolean prevIsWhitespace = true;
    for (int i = 0; i < toNormalize.length(); i++) {
        if (Character.isWhitespace(toNormalize.charAt(i))) {
            if (prevIsWhitespace) {
                toNormalize.deleteCharAt(i);
                i--;
            } else {
                toNormalize.setCharAt(i, ' ');
                prevIsWhitespace = true;
            }
        } else {
            prevIsWhitespace = false;
        }
    }

    // remove (at most) one trailing ' '
    if (toNormalize.length() > 0 && toNormalize.charAt(toNormalize.length() - 1) == ' ')
        toNormalize.deleteCharAt(toNormalize.length() - 1);
    return toNormalize;
}

From source file:au.csiro.casda.sodalint.SodaLinter.java

/**
 * Returns a list of startup announcements with which the taplint application introduces itself.
 *
 * @return announcement lines/*from w  ww. ja va 2  s .  c o m*/
 */
private static List<String> getAnnouncements() {

    /* Version report. */
    String version = getSodaLintVersion();
    String versionLine = new StringBuilder().append("This is sodalint ").append(version)
            .append(" validating against ").append(SODA_VERSION).toString();

    /* Count by report type of known FixedCode instances. */
    Map<ReportType, int[]> codeMap = new LinkedHashMap<ReportType, int[]>();
    for (ReportType type : Arrays.asList(ReportType.values())) {
        codeMap.put(type, new int[1]);
    }
    for (SodaCode code : Arrays.asList(SodaCode.values())) {
        codeMap.get(code.getType())[0]++;
    }
    StringBuffer cbuf = new StringBuffer().append("Static report types: ");
    for (Map.Entry<ReportType, int[]> entry : codeMap.entrySet()) {
        cbuf.append(entry.getKey()).append("(").append(entry.getValue()[0]).append(")").append(", ");
    }
    cbuf.setLength(cbuf.length() - 2);
    String codesLine = cbuf.toString();

    /* Return lines. */
    List<String> announcements = new ArrayList<>();
    announcements.add(versionLine);
    announcements.add(codesLine);
    return announcements;
}

From source file:chen.android.toolkit.network.HttpConnection.java

/**
 * </br><b>title : </b>      ????POST??
 * </br><b>description :</b>????POST??
 * </br><b>time :</b>      2012-7-8 ?4:33:20
 * @param url            POSTURL// w w  w.j  a v  a 2  s  .c o m
 * @param params         POST?
 * @param data            ????
 * @return               ???
 * @throws MalformedURLException
 * @throws IOException
 */
public static InputStream post(String url, Map<String, String> params, byte[] data)
        throws MalformedURLException, IOException {
    StringBuffer sb = new StringBuffer();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        sb.append(entry.getKey()).append("=").append(entry.getValue());
        sb.append("&");
    }
    String param = sb.deleteCharAt(sb.length() - 1).toString();
    return connect("POST", new URL(url), param.getBytes(), data);
}

From source file:chen.android.toolkit.network.HttpConnection.java

/**
 * </br><b>title : </b>      ????GET??
 * </br><b>description :</b>????GET??
 * </br><b>time :</b>      2012-7-8 ?4:35:23
 * @param url            GETURL/*from www  .j av a 2 s . c o m*/
 * @param params         ????
 * @return               ???
 * @throws MalformedURLException
 * @throws IOException
 */
public static InputStream get(String url, Map<String, String> params)
        throws MalformedURLException, IOException {
    StringBuffer sb = new StringBuffer();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        sb.append(entry.getKey()).append("=").append(entry.getValue());
        sb.append("&");
    }
    String param = sb.deleteCharAt(sb.length() - 1).toString();
    return connect("GET", new URL(url), param.getBytes());
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAOTest.java

private static void dump(String sql) {
    logger.fine("\n" + sql);
    SqlRowSet rs = dao.getJdbcTemplate().queryForRowSet(sql);
    try {/*  www.  j av a2  s  .  c o  m*/
        SqlRowSetMetaData rsmd = rs.getMetaData();
        int c = rsmd.getColumnCount();
        StringBuffer buff = new StringBuffer();
        for (int i = 1; i <= c; i++) {
            buff.append(rsmd.getColumnLabel(i));
            buff.append(i == c ? "\n" : "\t");
        }
        logger.fine(buff.toString());
        buff.delete(0, buff.length());
        while (rs.next()) {
            for (int i = 1; i <= c; i++) {
                buff.append(rs.getObject(i));
                buff.append(i == c ? "\n" : "\t");
            }
            logger.fine(buff.toString());
            buff.delete(0, buff.length());
        }
        logger.fine("================\n\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:CertStreamCallback.java

public static void toJustepBiz(HttpServletRequest request, HttpServletResponse response, String url)
        throws FileUploadException, IOException {
    Callback callback = new CertStreamCallback(request, response);
    String accept = NetUtils.getAccept(request);
    String contentType = NetUtils.getContentType(request);
    String language = NetUtils.getLanguage(request);
    String sessionID = null;/*from  w ww.  ja v a 2s  . c om*/
    StringBuffer params = new StringBuffer();
    for (Object n : request.getParameterMap().keySet()) {
        String p = URLEncoder.encode(request.getParameter((String) n), "UTF-8");
        if (0 != params.length()) {
            params.append("&" + n + "=" + p);
        } else {
            params.append(n + "=" + p);
        }
    }
    if (NetUtils.isRequestMultipart(request)) {
        Part[] parts = NetUtils.generateParts(request);
        invokeActions(url, params.toString(), null, parts, accept, contentType, sessionID, language, "post",
                callback);
    } else {
        String postData = JavaServer.getPostData(request);
        invokeActions(url, params.toString(), postData.getBytes("UTF-8"), null, accept, contentType, sessionID,
                language, "post", callback);
    }
}

From source file:ch.entwine.weblounge.common.url.UrlUtils.java

/**
 * Returns <code>null</code> if the url is valid, that is, if it contains only
 * allowed characters. otherwise, the invalid character is returned.
 * /*w ww .j  a v a 2s.  c  o m*/
 * @return <code>null</code> or the invalid character
 */
private static Character checkUrl(String url) {
    StringBuffer original = new StringBuffer(url);
    for (int i = 0; i < original.length(); i++) {
        int value = original.charAt(i);
        // a-z
        if (value >= 'a' && value <= 'z') {
            continue;
        }
        // A-Z
        if (value >= 'A' && value <= 'Z') {
            continue;
        }
        // 0-9
        if (value >= '0' && value <= '9') {
            continue;
        }
        // Special characters
        if (value == '-' || value == '_' || value == '.' || value == ',' || value == ';') {
            continue;
        }
        return original.charAt(i);
    }
    return null;
}

From source file:com.roncoo.pay.trade.utils.MerchantApiUtil.java

/**
 * ??/* w  w  w . ja  v a2 s  .  co  m*/
 * @param paramMap
 * @return
 */
public static String getParamStr(Map<String, Object> paramMap) {
    SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
    StringBuffer stringBuffer = new StringBuffer();
    for (Map.Entry<String, Object> m : smap.entrySet()) {
        Object value = m.getValue();
        if (value != null && StringUtils.isNotBlank(String.valueOf(value))) {
            stringBuffer.append(m.getKey()).append("=").append(value).append("&");
        }
    }
    stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

    return stringBuffer.toString();
}

From source file:com.roncoo.pay.trade.utils.MerchantApiUtil.java

/**
 * ????/* w w  w.  ja  v a2  s  .  com*/
 * @param paramMap  ???
 * @param paySecret ??
 * @return
 */
public static String getSign(Map<String, Object> paramMap, String paySecret) {
    SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
    StringBuffer stringBuffer = new StringBuffer();
    for (Map.Entry<String, Object> m : smap.entrySet()) {
        Object value = m.getValue();
        if (value != null && StringUtils.isNotBlank(String.valueOf(value))) {
            stringBuffer.append(m.getKey()).append("=").append(value).append("&");
        }
    }
    stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

    String argPreSign = stringBuffer.append("&paySecret=").append(paySecret).toString();
    String signStr = MD5Util.encode(argPreSign).toUpperCase();

    return signStr;
}

From source file:Main.java

/**
 * Gets the column letter corresponding to the 0-based column number
 * //from   w w w.  jav a 2 s . co m
 * @param column the column number
 * @param buf the string buffer in which to write the column letter
 */
public static void getColumnReference(int column, StringBuffer buf) {
    int v = column / 26;
    int r = column % 26;

    StringBuffer tmp = new StringBuffer();
    while (v != 0) {
        char col = (char) ('A' + r);

        tmp.append(col);

        r = v % 26 - 1; // subtract one because only rows >26 preceded by A
        v = v / 26;
    }

    char col = (char) ('A' + r);
    tmp.append(col);

    // Insert into the proper string buffer in reverse order
    for (int i = tmp.length() - 1; i >= 0; i--) {
        buf.append(tmp.charAt(i));
    }
}