Example usage for java.lang StringBuilder deleteCharAt

List of usage examples for java.lang StringBuilder deleteCharAt

Introduction

In this page you can find the example usage for java.lang StringBuilder deleteCharAt.

Prototype

@Override
public StringBuilder deleteCharAt(int index) 

Source Link

Usage

From source file:com.funambol.foundation.items.dao.PIMNoteDAO.java

private static String truncateStringList(String categoriesField, String separatorsString, int truncationSize) {

    if (categoriesField == null) {
        return null;
    }/*  ww  w.j  av a2 s  . c o  m*/

    StringTokenizer st = new StringTokenizer(categoriesField, separatorsString, true);
    StringBuilder sb = new StringBuilder("");

    while (st.hasMoreTokens()) {

        String token = st.nextToken();

        if (sb.length() + token.length() > truncationSize) {
            break;
        }

        sb.append(token);
    }

    if (sb.length() > 0) {
        char[] separators = separatorsString.toCharArray();
        for (char separator : separators) {
            if (sb.charAt(sb.length() - 1) == separator) {
                sb.deleteCharAt(sb.length() - 1);
                break;
            }
        }
    }

    return sb.toString();
}

From source file:com.dalabs.droop.util.OptionsFileUtil.java

/**
 * Expands any options file that may be present in the given set of arguments.
 *
 * @param args the given arguments/*w ww  .  j a  va2 s  .  c  o  m*/
 * @return a new string array that contains the expanded arguments.
 * @throws Exception
 */
public static String[] expandArguments(String[] args) throws Exception {
    List<String> options = new ArrayList<String>();

    for (int i = 0; i < args.length; i++) {
        if (args[i].equals(Droop.DROOP_OPTIONS_FILE_SPECIFIER)) {
            if (i == args.length - 1) {
                throw new Exception("Missing options file");
            }

            String fileName = args[++i];
            File optionsFile = new File(fileName);
            BufferedReader reader = null;
            StringBuilder buffer = new StringBuilder();
            try {
                reader = new BufferedReader(new FileReader(optionsFile));
                String nextLine = null;
                while ((nextLine = reader.readLine()) != null) {
                    nextLine = nextLine.trim();
                    if (nextLine.length() == 0 || nextLine.startsWith("#")) {
                        // empty line or comment
                        continue;
                    }

                    buffer.append(nextLine);
                    if (nextLine.endsWith("\\")) {
                        if (buffer.charAt(0) == '\'' || buffer.charAt(0) == '"') {
                            throw new Exception("Multiline quoted strings not supported in file(" + fileName
                                    + "): " + buffer.toString());
                        }
                        // Remove the trailing back-slash and continue
                        buffer.deleteCharAt(buffer.length() - 1);
                    } else {
                        // The buffer contains a full option
                        options.add(removeQuotesEncolosingOption(fileName, buffer.toString()));
                        buffer.delete(0, buffer.length());
                    }
                }

                // Assert that the buffer is empty
                if (buffer.length() != 0) {
                    throw new Exception(
                            "Malformed option in options file(" + fileName + "): " + buffer.toString());
                }
            } catch (IOException ex) {
                throw new Exception("Unable to read options file: " + fileName, ex);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ex) {
                        LOG.info("Exception while closing reader", ex);
                    }
                }
            }
        } else {
            // Regular option. Parse it and put it on the appropriate list
            options.add(args[i]);
        }
    }

    return options.toArray(new String[options.size()]);
}

From source file:com.cloudera.sqoop.util.OptionsFileUtil.java

/**
 * Expands any options file that may be present in the given set of arguments.
 *
 * @param args the given arguments/*w w w.j  av  a  2 s.  c  om*/
 * @return a new string array that contains the expanded arguments.
 * @throws Exception
 */
public static String[] expandArguments(String[] args) throws Exception {
    List<String> options = new ArrayList<String>();

    for (int i = 0; i < args.length; i++) {
        if (args[i].equals(Sqoop.SQOOP_OPTIONS_FILE_SPECIFIER)) {
            if (i == args.length - 1) {
                throw new Exception("Missing options file");
            }

            String fileName = args[++i];
            File optionsFile = new File(fileName);
            BufferedReader reader = null;
            StringBuilder buffer = new StringBuilder();
            try {
                reader = new BufferedReader(new FileReader(optionsFile));
                String nextLine = null;
                while ((nextLine = reader.readLine()) != null) {
                    nextLine = nextLine.trim();
                    if (nextLine.length() == 0 || nextLine.startsWith("#")) {
                        // empty line or comment
                        continue;
                    }

                    buffer.append(nextLine);
                    if (nextLine.endsWith("\\")) {
                        if (buffer.charAt(0) == '\'' || buffer.charAt(0) == '"') {
                            throw new Exception("Multiline quoted strings not supported in file(" + fileName
                                    + "): " + buffer.toString());
                        }
                        // Remove the trailing back-slash and continue
                        buffer.deleteCharAt(buffer.length() - 1);
                    } else {
                        // The buffer contains a full option
                        options.add(removeQuotesEncolosingOption(fileName, buffer.toString()));
                        buffer.delete(0, buffer.length());
                    }
                }

                // Assert that the buffer is empty
                if (buffer.length() != 0) {
                    throw new Exception(
                            "Malformed option in options file(" + fileName + "): " + buffer.toString());
                }
            } catch (IOException ex) {
                throw new Exception("Unable to read options file: " + fileName, ex);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ex) {
                        LOG.info("Exception while closing reader", ex);
                    }
                }
            }
        } else {
            // Regular option. Parse it and put it on the appropriate list
            options.add(args[i]);
        }
    }

    return options.toArray(new String[options.size()]);
}

From source file:org.apache.olingo.client.core.uri.URIUtils.java

public static URI buildFunctionInvokeURI(final URI uri, final Map<String, ClientValue> parameters) {
    final String rawQuery = uri.getRawQuery();
    String baseURI = StringUtils.substringBefore(uri.toASCIIString(), "?" + rawQuery);
    if (baseURI.endsWith("()")) {
        baseURI = baseURI.substring(0, baseURI.length() - 2);
    }//from   w  w  w. ja  v a2  s .com

    final StringBuilder inlineParams = new StringBuilder();
    for (Map.Entry<String, ClientValue> param : parameters.entrySet()) {
        inlineParams.append(param.getKey()).append("=");

        Object value = null;
        if (param.getValue().isPrimitive()) {
            value = param.getValue().asPrimitive().toValue();
        } else if (param.getValue().isComplex()) {
            value = param.getValue().asComplex().asJavaMap();
        } else if (param.getValue().isCollection()) {
            value = param.getValue().asCollection().asJavaCollection();
        } else if (param.getValue().isEnum()) {
            value = param.getValue().asEnum().toString();
        }

        inlineParams.append(URIUtils.escape(value)).append(',');
    }

    if (inlineParams.length() > 0) {
        inlineParams.deleteCharAt(inlineParams.length() - 1);
    }

    try {
        return URI.create(baseURI + "(" + URLEncoder.encode(inlineParams.toString(), Constants.UTF8) + ")"
                + (StringUtils.isNotBlank(rawQuery) ? "?" + rawQuery : StringUtils.EMPTY));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("While adding GET parameters", e);
    }

}

From source file:net.jofm.format.Format.java

private String trimLeftPad(String data) {
    if (data == null || data.length() == 0) {
        return data;
    }/*  w  w w .  j a va2  s .co m*/
    StringBuilder builder = new StringBuilder(data);
    while (builder.length() > 0 && builder.charAt(0) == padWith) {
        builder.deleteCharAt(0);
    }
    return builder.toString();
}

From source file:com.alibaba.jstorm.ui.controller.ZookeeperController.java

@RequestMapping(value = "/zookeeper", method = RequestMethod.GET)
public String show(@RequestParam(value = "name", required = true) String name, ModelMap model) {
    name = StringEscapeUtils.escapeHtml(name);
    long start = System.currentTimeMillis();
    try {/*from  w  w w  .j ava 2  s.  c  om*/
        ClusterConfig config = UIUtils.clusterConfig.get(name);
        StringBuilder builder = new StringBuilder("");
        for (String ip : config.getZkServers()) {
            builder.append(ip).append(",");
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append(":");
        builder.append(config.getZkPort());
        model.addAttribute("zkRoot", config.getZkRoot());
        model.addAttribute("zkServers", builder.toString());
        model.addAttribute("clusterName", name);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        UIUtils.addErrorAttribute(model, e);
    }

    LOG.info("zookeeper page show cost:{}ms", System.currentTimeMillis() - start);
    return "zookeeper";
}

From source file:com.eucalyptus.blockstorage.HttpTransfer.java

/**
 * Calculates the canonical and signed header strings in a single pass, done in one pass for efficiency
 * @param httpBaseRequest// w  w w.j a  va2s. c o  m
 * @return Array of 2 elements, first element is the canonicalHeader string, second element is the signedHeaders string
 */
private static String[] getCanonicalAndSignedHeaders(HttpMethodBase httpBaseRequest) {
    /*
     * The host header is required for EucaV2 signing, but it is not constructed by the HttpClient until the method is executed.
     * So, here we add a header with the same value and name so that we can do the proper sigining, but know that this value will
     * be overwritten when HttpMethodBase is executed to send the request.
     * 
     * This code is specific to the jakarta commons httpclient because that client will set the host header to hostname:port rather than
     * just hostname.
     * 
     * Supposedly you can force the value of the Host header with: httpBaseRequest.getParams().setVirtualHost("hostname"), but that was not successful
     */
    try {
        httpBaseRequest.addRequestHeader("Host",
                httpBaseRequest.getURI().getHost() + ":" + httpBaseRequest.getURI().getPort());
    } catch (URIException e) {
        LOG.error(
                "Could not add Host header for canonical headers during authorization header creation in HTTP client: ",
                e);
        return null;
    }

    Header[] headers = httpBaseRequest.getRequestHeaders();
    StringBuilder signedHeaders = new StringBuilder();
    StringBuilder canonicalHeaders = new StringBuilder();

    if (headers != null) {
        Arrays.sort(headers, new Comparator<Header>() {
            @Override
            public int compare(Header arg0, Header arg1) {
                return arg0.getName().toLowerCase().compareTo(arg1.getName().toLowerCase());
            }

        });

        for (Header header : headers) {
            //Add to the signed headers
            signedHeaders.append(header.getName().toLowerCase()).append(';');
            //Add the name and value to the canonical header
            canonicalHeaders.append(header.getName().toLowerCase()).append(':').append(header.getValue().trim())
                    .append('\n');
        }

        if (signedHeaders.length() > 0) {
            signedHeaders.deleteCharAt(signedHeaders.length() - 1); //Delete the trailing semi-colon
        }

        if (canonicalHeaders.length() > 0) {
            canonicalHeaders.deleteCharAt(canonicalHeaders.length() - 1); //Delete the trialing '\n' just to make things clear and consistent
        }
    }
    String[] result = new String[2];
    result[0] = canonicalHeaders.toString();
    result[1] = signedHeaders.toString();
    return result;
}

From source file:net.jofm.format.Format.java

private String trimRightPad(String data) {
    if (data == null || data.length() == 0) {
        return data;
    }/*w  ww  .j  ava  2  s .c o m*/
    StringBuilder builder = new StringBuilder(data);
    while (builder.length() > 0 && builder.charAt(builder.length() - 1) == padWith) {
        builder.deleteCharAt(builder.length() - 1);
    }
    return builder.toString();
}

From source file:com.romeikat.datamessie.core.base.app.shared.RemoteFullTextSearcher.java

private String getRestUrl() {
    if (StringUtils.isBlank(fullTextProviderUrl)) {
        return null;
    }/* w w  w  . j  a va 2s  .  com*/

    final StringBuilder restUrl = new StringBuilder();

    // Application path
    restUrl.append(fullTextProviderUrl);

    // Query path
    if (fullTextProviderUrl.endsWith("/")) {
        restUrl.deleteCharAt(restUrl.length() - 1);
    }
    restUrl.append(QUERY_PATH);

    return restUrl.toString();
}

From source file:fr.esiea.windmeal.dao.elasticsearch.IndexationProviderDecoratorDao.java

private String getTypes(Set<Tag> tags) {
    //TODO Should be possible to optimize
    StringBuilder builder = new StringBuilder();
    for (Tag tag : tags) {
        builder.append(tag.getTag() + ",");
    }//from  ww  w. java  2s. c  o  m
    builder.deleteCharAt(builder.length() - 1);
    LOGGER.info("Builder type for elastic search " + builder);
    return builder.toString();
}