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:com.concursive.connect.web.modules.wiki.utils.HTMLToWikiUtils.java

private static void startOnNewLine(StringBuffer sb, String appendToCRLF) {
    if (sb.length() > 0) {
        if (appendToCRLF.length() == 0) {
            while (!sb.toString().endsWith(CRLF + CRLF)) {
                LOG.trace("startOnNewLine CRLF");
                sb.append(CRLF);/*www  .j a v  a 2 s  .co  m*/
            }
        } else {
            // Something\n
            // !
            // !* Item 1
            // !* Item 2
            if (!sb.toString().endsWith("|")
                    && !sb.toString().endsWith(CRLF + appendToCRLF + CRLF + appendToCRLF)) {
                LOG.trace("startOnNewLine CRLF");
                sb.append(CRLF + appendToCRLF + CRLF + appendToCRLF);
            }
        }
    }
}

From source file:MinimalHTTPServer.java

public void handle(HttpExchange xchg) throws IOException {
    Headers headers = xchg.getRequestHeaders();
    Set<Map.Entry<String, List<String>>> entries = headers.entrySet();

    StringBuffer response = new StringBuffer();
    for (Map.Entry<String, List<String>> entry : entries)
        response.append(entry.toString() + "\n");

    xchg.sendResponseHeaders(200, response.length());
    OutputStream os = xchg.getResponseBody();
    os.write(response.toString().getBytes());
    os.close();//from  ww w.  j a  v  a  2  s. c  o  m
}

From source file:com.almarsoft.GroundhogReader.lib.MessageTextProcessor.java

private static String escapeHtmlWithLinks(String line) {

    StringBuffer buf = null;//from   w w w.j ava 2s . co m

    // Shortcut for most cases with line not having a link       
    int idx = line.toLowerCase().indexOf("http://");

    if (idx == -1) {
        return StringEscapeUtils.escapeHtml(line);
    } else {
        buf = new StringBuffer();
        buf.append(StringEscapeUtils.escapeHtml(line.substring(0, idx)));

        char c;
        String endLine;
        StringBuffer urlBuf = new StringBuffer();
        int lineLen = line.length();

        for (; idx < lineLen; idx++) {
            c = line.charAt(idx);
            if (Character.isSpace(c)) {
                break;
            }
            urlBuf.append(c);
        }

        if (urlBuf.length() > 0) {
            buf.append("<A HREF=\"");
            buf.append(urlBuf);
            buf.append("\" >");
            buf.append(urlBuf);
            buf.append("</A> ");
        }

        endLine = line.substring(idx);
        if (endLine.length() > 0)
            buf.append(StringEscapeUtils.escapeHtml(line.substring(idx)));
    }

    return buf.toString();
}

From source file:eionet.cr.util.FormatUtils.java

/**
 * Returns object values for the given predicate in given langauges.
 *
 * @param predicateUri predicate URI//from  ww w  .  j av  a 2s  .c  om
 * @param subjectDTO SubjectDTO data object for subject
 * @param languages Set<String> language codes
 * @return String
 */
public static String getObjectValuesForPredicate(String predicateUri, SubjectDTO subjectDTO,
        List<String> languages) {
    String result = "";

    if (subjectDTO.getPredicateCount() > 0) {

        Collection<ObjectDTO> objects = subjectDTO.getObjects(predicateUri);
        if (objects != null && !objects.isEmpty()) {

            LinkedHashSet<ObjectDTO> distinctObjects = new LinkedHashSet<ObjectDTO>(objects);
            StringBuffer bufLiterals = new StringBuffer();
            StringBuffer bufNonLiterals = new StringBuffer();

            String resultFromHitSource = null;
            for (ObjectDTO objectDTO : distinctObjects) {

                String objectString = objectDTO.getValue().trim();

                // if the source of the object matches the search-hit source of the subject then
                // remember the object value and break
                if (subjectDTO.getHitSource() > 0 && objectDTO.getSourceHashSmart() == subjectDTO.getHitSource()
                        && !StringUtils.isBlank(objectString) && objectDTO.isLiteral()) {

                    resultFromHitSource = objectString;
                    break;
                }

                if (objectString.length() > 0) {

                    if (objectDTO.isLiteral()) {
                        if (languages.isEmpty() || languages.contains(objectDTO.getLanguage())) {
                            bufLiterals.append(bufLiterals.length() > 0 ? ", " : "").append(objectString);
                        }
                    } else {
                        bufNonLiterals.append(bufNonLiterals.length() > 0 ? ", " : "").append(objectString);
                    }
                }
            }

            // if there was a value found that came from search-hit source then prefer that one as the result
            if (!StringUtils.isBlank(resultFromHitSource)) {
                result = resultFromHitSource;
            } else {
                result = bufLiterals.length() > 0 ? bufLiterals.toString() : bufNonLiterals.toString();
            }
        }
    }
    return result;
}

From source file:com.panet.imeta.core.util.UUID4Util.java

/**
 * Turn a byte array into a version four UUID string.
 * Adapted from org.apache.commons.id.uuid.UUID.java
 * @param raw//ww  w .j  a  va 2s .  c o  m
 * @return
 */
private String getUUIDString(byte[] raw) {
    StringBuffer buf = new StringBuffer(new String(encodeHex(raw)));
    while (buf.length() != 32) {
        buf.insert(0, "0");
    }
    buf.ensureCapacity(32);
    buf.insert(8, '-');
    buf.insert(13, '-');
    buf.insert(18, '-');
    buf.insert(23, '-');
    return buf.toString();
}

From source file:Main.java

/**
 * @param base//from w ww. j  a v a  2  s  .  c om
 * @param set
 * @param title
 */
private static void compareTitleWithBase(String base, boolean bracketed, HashSet<Integer> set, String title) {
    // Check to see it the name starts with the prefix
    if (title.toLowerCase().startsWith(base.toLowerCase())) {
        // with brackets add on is: space, (, #, )
        int minSizeNumAddOn = 4;
        if (!bracketed)
            // without brackets and space add on is just number
            minSizeNumAddOn = 1;
        // We found a possible auto-generated name
        // Determine number
        if (title.length() >= (base.length() + minSizeNumAddOn)) {
            String numPart;
            if (bracketed && title.charAt(base.length()) == ' ') {
                // We skipped the space since we already checked
                numPart = title.substring(base.length() + 1);
            } else if (!bracketed) {
                // without brackets, the numPart is everything after the prefix
                numPart = title.substring(base.length());
            } else {
                // We are using brackets and there was no space
                return;
            }
            if (bracketed) {
                if (numPart.charAt(0) == '(') {
                    // We are using brackets and confirmed that the open bracket exists
                    // move on to just the number part
                    numPart = numPart.substring(1);
                } else {
                    // We are using brackets and there is no opening bracket
                    return;
                }
            }
            // We found an auto-generated name
            StringBuffer buffer = new StringBuffer();
            // Parse the number between the brackets
            for (int j = 0; j < numPart.length(); j++) {
                char current = numPart.charAt(j);
                // Make sure its a digit
                if (Character.isDigit(current)) {
                    buffer.append(current);
                } else {
                    if (!bracketed || numPart.charAt(j) != ')' || j != numPart.length() - 1) {
                        // without brackets, a non digits means this will not conflict
                        // with brackets, anything other than a ')' means this will not conflict
                        // with brackets, if this is not the last character it will not conflict
                        return;
                    }
                    // if all conditions passed, this is the last loop, no need to break
                }
            }
            // Convert the number we found into an actual number
            if (buffer.length() > 0) {
                set.add(new Integer(buffer.toString()));
            }

        } else {
            // No number to parse
            // Assume it is just base
            set.add(new Integer(0));
        }
    }
}

From source file:cn.remex.core.util.StringUtils.java

/**
 * /* w  w  w. j a  va 2  s.c o  m*/
 * Trim all occurences of the supplied leading character from the given String.
 * @param str the String to check 
 * @param leadingCharacter the leading character to be trimmed ?
 * @return the trimmed String ??
 */
public static String trimLeadingCharacter(final String str, final char leadingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(0) == leadingCharacter) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:cn.remex.core.util.StringUtils.java

/**
 * ?//from   ww  w.j  a  va2s .  com
 * Trim leading whitespace from the given String.
 * @param str the String to check
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(final String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:cn.remex.core.util.StringUtils.java

/**
 * ?/*  w w w. ja v a  2  s .com*/
 * Trim all occurences of the supplied trailing character from the given String.
 * @param str the String to check 
 * @param trailingCharacter the trailing character to be trimmed ?
 * @return the trimmed String ??
 */
public static String trimTrailingCharacter(final String str, final char trailingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(buf.length() - 1) == trailingCharacter) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:cn.remex.core.util.StringUtils.java

/**
 *  /*from   www. j av  a2  s .c  o  m*/
 * Trim trailing whitespace from the given String.
 * @param str the String to check 
 * @return the trimmed String ??
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(final String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}