List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
/** * Reads text from given {@link BufferedReader} line-by-line. * * @return text from given {@link BufferedReader}. * @throws IOException//from w ww . j a v a2 s.com */ @NonNull public static String readTextFromBufferedReader(@NonNull BufferedReader bufferedReader) throws IOException { // Store all lines to string builder to // reduce memory using. final StringBuilder body = new StringBuilder(); String nextLine; try { while ((nextLine = bufferedReader.readLine()) != null) { body.append(nextLine); body.append('\n'); } int pos = body.length() - 1; if (pos >= 0) { body.deleteCharAt(pos); } } finally { bufferedReader.close(); } return body.toString(); }
From source file:Main.java
/** * Returns the given Collection as formatted String *///w w w . j a v a 2 s .co m public static String toString(Collection<?> c) { StringBuilder buffer = new StringBuilder(); Iterator it = c.iterator(); for (int i = 0; it.hasNext(); i++) { buffer.append(i).append(": ").append(it.next()).append('\n'); } // Delete the last \n if (buffer.length() > 1) { buffer.setLength(buffer.length() - 1); } return buffer.toString(); }
From source file:com.autentia.common.util.StringUtils.java
public static String trimBrackets(String stringToTrim) { final StringBuilder result = new StringBuilder(stringToTrim); while (result.toString().startsWith("[")) { result.deleteCharAt(0);//from w w w .j a v a2 s . c o m } while (result.toString().endsWith("]")) { result.deleteCharAt(result.length() - 1); } return result.toString(); }
From source file:org.killbill.billing.plugin.simpletax.config.http.TaxCodeController.java
private static String joinTaxCodes(Set<TaxCodeRsc> taxCodes) { StringBuilder names = new StringBuilder(); for (TaxCodeRsc taxCode : taxCodes) { if (names.length() > 0) { names.append(TAX_CODES_JOIN_SEPARATOR); }//from www . j a va 2 s. co m names.append(taxCode.name); } return names.toString(); }
From source file:eu.skillpro.ams.pscm.connector.amsservice.ui.RetrieveSEEHandler.java
/** * Returns a list of SEEs, retrieved by the server's retrieveSEEs method. * @param newOnly <code>true</code> to get only the newly registered SEEs, <code>false</code> to get all SEEs * @return a list of SEE transfer objects * @throws IOException if there were any problems with the sonnection to the server */// ww w . j a v a 2s .com public static List<SEETO> getSEETOs(boolean newOnly) throws IOException { String serviceName = "retrieveSEEs"; String parameters = "?newOnly=" + newOnly; HttpGet request = new HttpGet(AMSServiceUtility.serviceAddress + serviceName + parameters); request.setHeader("Content-type", "application/json"); HttpClient client = HttpClientBuilder.create().build(); ; HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } if (sb.length() == 0) { return new ArrayList<SEETO>(); } else { List<SEETO> result = JSONUtility.convertToList(sb.toString(), new TypeToken<List<SEETO>>() { }.getType()); System.out.println("Number of retrieved SEEs: " + result.size()); return result; } }
From source file:shootersubdownloader.Shootersubdownloader.java
static String computefilehash(File f) throws Exception { if (!f.exists() || f.length() < 8 * 1024) { return null; }/*from w w w.ja v a 2s. c o m*/ long l = f.length(); long[] offset = new long[4]; offset[3] = l - 8 * 1024; offset[2] = l / 3; offset[1] = l / 3 * 2; offset[0] = 4 * 1024; byte[] bBuf = new byte[1024 * 4]; RandomAccessFile raf = new RandomAccessFile(f, "r"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { raf.seek(offset[i]); int readlen = raf.read(bBuf, 0, 4 * 1024); String md5 = md5(bBuf); if (sb.length() != 0) { sb.append("%3B"); } sb.append(md5); } raf.close(); return sb.toString(); }
From source file:com.github.pjungermann.config.utils.NameUtils.java
/** * Returns the natural name representation for the given name, i.e. * {@code "MyName"} gets converted to {@code "My Name"}. * * @param name the name to create the natural name for. * @return the natural name.//from ww w . j av a 2s . c o m */ @NotNull public static String getNaturalName(@NotNull final String name) { if (name.isEmpty()) { return name; } final String[] nameParts = splitByCharacterTypeCamelCase(name); final StringBuilder builder = new StringBuilder(); for (final String part : nameParts) { builder.append(part).append(" "); } builder.setLength(builder.length() - 1); return builder.toString(); }
From source file:com.piketec.jenkins.plugins.tpt.Publish.java
/** * Publish the Junits results, it creates an XML file and write the results on it. * /*from w w w . jav a 2 s . c o m*/ * @param jenkinsConfig * The configuration to which the TPT test resuklt should be tranformed to JUnit * @param testDataDir * The directory where TPT test data should be searched * @param jUnitOutputDir * The directory where the transformed results should be written to. * @param logger * to display the information * @param logLevel * the threshold for the severity of the log messages * @return the number of testcases . * @throws IOException * if an error occured while parsing TPT test data or writing the JUnit xml files * @throws InterruptedException * If the job was interrupted */ public static int publishJUnitResults(JenkinsConfiguration jenkinsConfig, FilePath testDataDir, FilePath jUnitOutputDir, TptLogger logger, LogLevel logLevel) throws IOException, InterruptedException { XmlStreamWriter xmlPub = null; try { String classname = FilenameUtils.getBaseName(jenkinsConfig.getTptFile()); FilePath jUnitXMLFile = new FilePath(jUnitOutputDir, classname + "." + jenkinsConfig.getConfigurationWithUnderscore() + ".xml"); xmlPub = new XmlStreamWriter(); xmlPub.initalize(jUnitXMLFile); xmlPub.writeTestsuite(classname); List<Testcase> testdata = getTestcases(testDataDir, logger); logger.info("Found " + testdata.size() + " test results."); for (Testcase tc : testdata) { if (tc.getLogEntries(LogLevel.ERROR).isEmpty() && "SUCCESS".equals(tc.getResult())) { xmlPub.writeTestcase(classname, tc.getQualifiedName(), tc.getExecDuration()); } else { StringBuilder log = new StringBuilder(); for (LogEntry entry : tc.getLogEntries(logLevel)) { if (log.length() > 0) { log.append('\n'); } log.append('[').append(entry.level.name()).append("] ").append(entry.message); } xmlPub.writeTestcaseError(classname, tc.getQualifiedName(), tc.getExecDuration(), log.toString()); } } return testdata.size(); } catch (XMLStreamException e) { throw new IOException("XML stream error: " + e.getMessage()); } catch (FactoryConfigurationError e) { throw new IOException("XML configuration error: " + e.getMessage()); } finally { if (xmlPub != null) { xmlPub.close(); } } }
From source file:Main.java
/** * Returns text content of a given Node. */// w ww.j a v a 2 s . c om static String getText(Node node) { NodeList nodes = node.getChildNodes(); int len = nodes.getLength(); if (len == 0) { return null; } StringBuilder text = new StringBuilder(); for (int i = 0; i < len; i++) { Node child = nodes.item(i); if (child instanceof CharacterData) { text.append(((CharacterData) child).getData()); } } return text.length() == 0 ? null : text.toString(); }
From source file:Main.java
public static final String getComment(Element elem) { StringBuilder sb = new StringBuilder(); Node node = elem.getPreviousSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { break; }//w w w .ja v a2s . c om if (node.getNodeType() == Node.COMMENT_NODE) { if (sb.length() > 0) { sb.insert(0, '\n'); sb.insert(0, ((Comment) node).getData()); } else { sb.append(((Comment) node).getData()); } } node = node.getPreviousSibling(); } return sb.toString(); }