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:org.apache.torque.engine.database.model.ConstraintNameGenerator.java

/**
 * First element of <code>inputs</code> should be of type {@link
 * org.apache.torque.engine.database.model.Database}, second
 * should be a table name, third is the type identifier (spared if
 * trimming is necessary due to database type length constraints),
 * and the fourth is a <code>Integer</code> indicating the number
 * of this contraint.//from   w  w  w. j a  v  a 2s .c  om
 *
 * @see org.apache.torque.engine.database.model.NameGenerator
 */
public String generateName(List inputs) throws EngineException {
    StringBuffer name = new StringBuffer();
    Database db = (Database) inputs.get(0);
    name.append((String) inputs.get(1));
    String namePostfix = (String) inputs.get(2);
    String constraintNbr = inputs.get(3).toString();

    // Calculate maximum RDBMS-specific column character limit.
    int maxBodyLength = -1;
    try {
        int maxColumnNameLength = db.getPlatform().getMaxColumnNameLength();
        maxBodyLength = (maxColumnNameLength - namePostfix.length() - constraintNbr.length() - 2);

        if (log.isDebugEnabled()) {
            log.debug("maxColumnNameLength=" + maxColumnNameLength + " maxBodyLength=" + maxBodyLength);
        }
    } catch (NumberFormatException maxLengthUnknown) {
    }

    // Do any necessary trimming.
    if (maxBodyLength != -1 && name.length() > maxBodyLength) {
        name.setLength(maxBodyLength);
    }

    name.append(STD_SEPARATOR_CHAR).append(namePostfix).append(STD_SEPARATOR_CHAR).append(constraintNbr);

    return name.toString();
}

From source file:com.maverick.http.HttpResponse.java

protected void processHeaderFields(InputStream in) throws IOException {
    clearHeaderFields();// w w w  .  jav  a 2 s. c o m

    StringBuffer lineBuf = new StringBuffer();
    String lastHeaderName = null;
    int c;

    while (true) {
        c = in.read();

        if (c == -1) {
            throw new IOException(Messages.getString("HttpResponse.headerCorrupt")); //$NON-NLS-1$
        }

        if (c != '\n') {
            lineBuf.append((char) c);
        } else {
            String line = lineBuf.toString().trim();
            lineBuf.setLength(0);
            if (line.length() != 0) {
                lastHeaderName = processNextLine(line, lastHeaderName);
            } else {
                break;
            }
        }
    }
}

From source file:com.brienwheeler.svc.monitor.telemetry.impl.GraphiteTelemetryInfoProcessor.java

@Override
public void process(TelemetryInfo telemetryInfo) {
    ReconnectingSocket socket = reconnectingSocket.get();
    if (socket == null || !socket.isConnected())
        return;/*w ww. java2  s  .  co  m*/

    String name = telemetryInfo.getName();
    if (globalRedact != null) {
        name = name.replace(globalRedact, "");
    }
    if (globalPrefix != null) {
        name = (globalPrefix + ".") + name;
    }

    long createdAt = telemetryInfo.getCreatedAt() / 1000; // Graphite uses UNIX epoch

    StringBuffer data = new StringBuffer(256);
    for (String attrName : telemetryInfo.getAttributeNames()) {
        // we never care about NAME or CREATED_AT
        if (attrName.equals(TelemetryInfo.ATTR_NAME) || attrName.equals(TelemetryInfo.ATTR_CREATED_AT))
            continue;

        // allow a chance to filter out based on attrName too (FilteringTelemetryInfoProcessor
        // can only filter on telemetryInfo.name)
        String statisticName = name + "." + attrName;
        if (!telemetryNameFilter.process(statisticName))
            continue;

        Object attrValue = telemetryInfo.get(attrName);
        if (attrValue instanceof Double || attrValue instanceof Float || attrValue instanceof Integer
                || attrValue instanceof Long) {
            data.setLength(0);
            data.append(statisticName);
            data.append(" ").append(attrValue.toString());
            data.append(" ").append(createdAt).append("\n");
            String dataStr = data.toString();
            socket.write(dataStr);
            log.debug(dataStr);
        }
    }
}

From source file:com.mgmtp.jfunk.data.generator.util.GeneratingExpression.java

private Range parseNextNumberExpression(final CharacterPushbackReader in) throws IOException {
    char c = in.readChar();
    int[] ar = new int[2];
    ar[0] = -1;/*w  w w . j  a  v a2  s . c  om*/
    ar[1] = -1;
    if ('{' == c) {
        StringBuffer sb = new StringBuffer();
        c = in.readChar();
        // after { now so min number is coming
        while (c != '}' && in.ready()) {
            if (',' == c) {
                try {
                    ar[0] = Integer.parseInt(sb.toString());
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException(
                            "if giving number of occurrences with {n,m} you must specify a value for n", e);
                }
                sb.setLength(0);
            } else {
                sb.append(c);
            }
            c = in.readChar();
        }
        if (sb.length() > 0) {
            ar[1] = Integer.parseInt(sb.toString());
        }
        if (ar[0] == -1) {
            ar[0] = ar[1];
        }
    } else if ('?' == c) {
        ar[0] = 0;
        ar[1] = 1;
    } else if ('+' == c) {
        ar[0] = 1;
    } else if ('*' == c) {
        ar[0] = 0;
    } else {
        ar[0] = 1;
        ar[1] = 1;
        in.unread(c);
    }
    return new Range(ar[0], ar[1]);
}

From source file:org.apache.nutch.protocol.htmlunit.HttpResponse.java

private void parseHeaders(PushbackInputStream in, StringBuffer line) throws IOException, HttpException {

    while (readLine(in, line, true) != 0) {

        // handle HTTP responses with missing blank line after headers
        int pos;//from  w ww  .  j  ava 2  s .co  m
        if (((pos = line.indexOf("<!DOCTYPE")) != -1) || ((pos = line.indexOf("<HTML")) != -1)
                || ((pos = line.indexOf("<html")) != -1)) {

            in.unread(line.substring(pos).getBytes("UTF-8"));
            line.setLength(pos);

            try {
                //TODO: (CM) We don't know the header names here
                //since we're just handling them generically. It would
                //be nice to provide some sort of mapping function here
                //for the returned header names to the standard metadata
                //names in the ParseData class
                processHeaderLine(line);
            } catch (Exception e) {
                // fixme:
                Http.LOG.warn("Error: ", e);
            }
            return;
        }

        processHeaderLine(line);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.search.solr.AddAllText.java

@Override
public void modifyDocument(Individual ind, SolrInputDocument doc, StringBuffer arg2)
        throws SkipIndividualException {

    String t = null;//  w  ww  .ja v a  2  s  . c om
    StringBuffer allTextValue = new StringBuffer();

    //collecting data property statements
    List<DataPropertyStatement> dataPropertyStatements = ind.getDataPropertyStatements();
    if (dataPropertyStatements != null) {
        Iterator<DataPropertyStatement> dataPropertyStmtIter = dataPropertyStatements.iterator();
        while (dataPropertyStmtIter.hasNext()) {
            DataPropertyStatement dataPropertyStmt = dataPropertyStmtIter.next();

            // we don't want label to be added to alltext
            if (RDFS.label.getURI().equals(dataPropertyStmt.getDatapropURI())) {
                continue;
            }

            allTextValue.append(" ");
            allTextValue.append(((t = dataPropertyStmt.getData()) == null) ? "" : t);
        }
    }

    try {
        String stripped = Jsoup.parse(allTextValue.toString()).text();
        allTextValue.setLength(0);
        allTextValue.append(stripped);
    } catch (Exception e) {
        log.debug("Could not strip HTML during search indexing. ", e);
    }

    String alltext = allTextValue.toString();

    doc.addField(multiSiteTerm.alltext, alltext);
}

From source file:de.codesourcery.springmass.springmass.SimulationParamsBuilder.java

private String extractParameterName(Method m) {
    Method methodWithLabel = null;
    if (isParameterGetter(m)) {
        // look for @Label on corresponding setter
        if (m.getName().startsWith("get") || m.getName().startsWith("is")) {
            final int charsToRemove = m.getName().startsWith("get") ? 3 : 2;
            final String setterName = "set" + m.getName().substring(charsToRemove);
            try {
                methodWithLabel = m.getDeclaringClass().getMethod(setterName,
                        new Class<?>[] { m.getReturnType() });
            } catch (NoSuchMethodException | SecurityException e) {
                /* ok */ }
        }/*  w w w  .j  a  va 2s  .  co  m*/
    } else {
        methodWithLabel = m;
    }

    if (methodWithLabel != null) {
        final Label label = methodWithLabel.getAnnotation(Label.class);
        if (label != null && !StringUtils.isBlank(label.value())) {
            return label.value();
        }
    }

    String name;
    if (m.getName().startsWith("set") || m.getName().startsWith("get")) {
        name = m.getName().substring(3);
    } else if (m.getName().startsWith("is")) {
        name = m.getName().substring(2);
    } else {
        throw new IllegalArgumentException("Invalid method name: " + m);
    }
    name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
    final List<String> split = new ArrayList<>();
    final StringBuffer buffer = new StringBuffer();
    for (char s : name.toCharArray()) {
        if (Character.isUpperCase(s)) {
            if (buffer.length() > 1) {
                split.add(buffer.toString());
                buffer.setLength(0);
                buffer.append(Character.toLowerCase(s));
                continue;
            }
        }
        if (split.isEmpty()) {
            buffer.append(s);
        } else {
            buffer.append(Character.toLowerCase(s));
        }
    }
    if (buffer.length() > 0) {
        split.add(buffer.toString());
    }

    return StringUtils.join(split, " ");
}

From source file:org.eclipse.birt.report.engine.emitter.docx.writer.BasicComponent.java

private void buildForeignStyles(IForeignContent foreignContent, StringBuffer foreignStyles, int display) {
    IStyle style = foreignContent.getComputedStyle();
    foreignStyles.setLength(0);
    buildTextAlign(foreignStyles, style);
    style = getElementStyle(foreignContent);
    if (style == null) {
        return;//from   w  w  w.  ja  va2s.c o m
    }
    buildFont(foreignStyles, style);
    buildBox(foreignStyles, style);
    buildText(foreignStyles, style);
    buildVisual(foreignStyles, style);
    buildTextDecoration(foreignStyles, style);
}

From source file:com.pureinfo.common.namedvalue.view.function.ShowNamedValueOptionsNewFunctionHandler.java

/**
 * //from   www  .  j a v  a 2 s .c om
 * @param _sValue
 * @param _bMultiple
 * @param _nValueType
 * @param _bShowHead
 * @param _sStyle
 * @param _sName
 * @return
 */
private String doPerform(String _sValue, boolean _bMultiple, List _nvs, Object _oShowWhat, boolean _bShowHead,
        String _sName) throws PureException {
    String[] arrValues = _sValue == null ? null : _sValue.split(NamedValueHelper.VALUE_SEPARATOR);
    StringBuffer sbuff = new StringBuffer();
    try {
        if (!_bMultiple && _bShowHead) {
            sbuff.append("<option value=\"\"> --  --\n");
        }

        for (Iterator iter = _nvs.iterator(); iter.hasNext();) {
            INamedValue element = (INamedValue) iter.next();
            String sShow = rendShow(_oShowWhat, element);

            String sCode = element.getValue();

            sbuff.append("<option ").append("value='").append(sCode).append('\'');
            if (arrValues != null && ArrayUtils.contains(arrValues, sCode)) {
                sbuff.append(" selected");
            }
            sbuff.append('>').append(sShow).append("</option>\n");
        }
        return sbuff.toString();
    } finally {
        sbuff.setLength(0);
        if (_nvs != null)
            _nvs.clear();
    }
}

From source file:com.adito.boot.Util.java

/**
 * Split a string into an array taking into account delimiters, quotes and
 * escapes/*from  w w  w .  j a va 2  s  .  co m*/
 * 
 * @param str string to split
 * @param delim delimiter
 * @param quote quote character
 * @param escape escape character
 * @return array
 */

public static String[] splitString(String str, char delim, char quote, char escape) {
    Vector v = new Vector();
    StringBuffer str1 = new StringBuffer();
    char ch = ' ';
    boolean inQuote = false;
    boolean escaped = false;

    for (int i = 0; i < str.length(); i++) {
        ch = str.charAt(i);

        if ((escape != -1) && (ch == escape) && !escaped) {
            escaped = true;
        } else {
            if ((quote != -1) && (ch == quote) && !escaped) {
                inQuote = !inQuote;
            } else if (!inQuote && (ch == delim && !escaped)) {
                v.addElement(str1.toString());
                str1.setLength(0);
            } else {
                str1.append(ch);
            }
            if (escaped) {
                escaped = false;
            }
        }
    }

    if (str.length() > 0) {
        v.addElement(str1.toString());

    }
    String[] array;
    array = new String[v.size()];
    v.copyInto(array);

    return array;
}