List of usage examples for java.lang StringBuilder length
int length();
From source file:net.duckling.ddl.util.StringUtil.java
License:asdf
public static String getSQLInFromInt(int[] ints) { StringBuilder sb = new StringBuilder(); sb.append("("); for (int i : ints) { sb.append(i).append(","); }//w w w.j av a2 s. c o m sb.delete(sb.length() - 1, sb.length()); sb.append(")"); return sb.toString(); }
From source file:Main.java
/** * Convert bundle to readable string//from w w w . j ava 2 s . c o m * * @param bundle The bundle to convert * @return String representation of bundle */ public static String toString(Bundle bundle) { if (bundle == null) { return null; } StringBuilder stringBuilder = new StringBuilder(); for (String key : bundle.keySet()) { Object value = bundle.get(key); stringBuilder.append( String.format("%s %s (%s)\n", key, value, value == null ? "null" : value.getClass().getName())); } return stringBuilder.substring(0, stringBuilder.length() - 1); }
From source file:com.conwet.xjsp.json.JSONUtil.java
/** * Extracts from the buffer a whole stanza or an stream finalizer "]}". * The recognized text is removed from the buffer. * //w w w .ja va 2 s .c o m * Stanza recognition is implemented as a FSM. States: * <ul> * <li>0. starting</li> * <li>1. accepting text</li> * <li>2. within an string</li> * <li>3. finalizer</li> * </ul> * * <img src="../../../../../resources/fsm.png"/> * * @return Recognized text or <code>null</code> */ public static String extractStanza(StringBuilder buffer) throws ParseException { discardSpaces(buffer); int state = 0; int pos = 0; int level = 1; while (pos < buffer.length()) { char c = buffer.charAt(pos); switch (state) { case 0: switch (c) { case '{': state = 1; break; case ']': state = 3; break; default: throw new ParseException(ParseException.ERROR_UNEXPECTED_CHAR); } break; case 1: switch (c) { case '{': level++; break; case '}': level--; if (level == 0) { String stanza = buffer.substring(0, pos + 1); buffer.replace(0, pos + 1, ""); return stanza; } break; case '"': state = 2; break; default: // nothing } break; case 2: switch (c) { case '\\': pos++; break; case '"': state = 1; break; default: // nothing } break; case 3: if (isSpace(c)) { pos++; } else if (c == '}') { buffer.replace(0, pos + 1, ""); return "]}"; } default: throw new IllegalStateException(); } pos++; } return null; }
From source file:Main.java
public static String textToString(Reader xquery) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(xquery); String NL = System.getProperty("line.separator"); String line = null;/*from w ww . j a v a2 s .c om*/ try { while ((line = br.readLine()) != null) { sb.append(line).append(NL); } sb.deleteCharAt(sb.length() - 1); } finally { br.close(); } return sb.toString(); }
From source file:edu.caltech.ipac.firefly.server.network.HttpServices.java
/** * Execute the given HTTP method with the given parameters. * @param method the function or method to perform * @param cookies optional, sent with request if present. * @return true is the request was successfully received, understood, and accepted (code 2xx). *//*from ww w .jav a 2s .com*/ public static boolean executeMethod(HttpMethod method, String userId, String password, Map<String, String> cookies) { try { if (!StringUtils.isEmpty(userId)) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password); httpClient.getState().setCredentials(AuthScope.ANY, credentials); } else { // check to see if the userId and password is in the url userId = URLDownload.getUserFromUrl(method.toString()); if (userId != null) { password = URLDownload.getPasswordFromUrl(method.toString()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password); httpClient.getState().setCredentials(AuthScope.ANY, credentials); } } if (cookies != null) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : cookies.entrySet()) { if (sb.length() > 0) sb.append("; "); sb.append(entry.getKey()); sb.append("="); sb.append(entry.getValue()); } if (sb.length() > 0) { method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); method.setRequestHeader("Cookie", sb.toString()); } } int status = httpClient.executeMethod(method); boolean isSuccess = status >= 200 && status < 300; String reqDesc = "URL:" + method.getURI(); if (isSuccess) { LOG.info(reqDesc); } else { reqDesc = reqDesc + "\nREQUEST HEADERS: " + CollectionUtil.toString(method.getRequestHeaders()).replaceAll("\\r|\\n", "") + "\nPARAMETERS:\n " + getDesc(method.getParams()) + "\nRESPONSE HEADERS: " + CollectionUtil.toString(method.getResponseHeaders()).replaceAll("\\r|\\n", ""); LOG.error("HTTP request failed with status:" + status + "\n" + reqDesc); } return isSuccess; } catch (Exception e) { LOG.error(e, "Unable to connect to:" + method.toString()); } return false; }
From source file:Main.java
/** * Returns the ASCII characters up to but not including the next "\r\n", or * "\n"./*from ww w. j a v a 2 s . c om*/ * * @throws java.io.EOFException if the stream is exhausted before the next newline * character. */ public static String readAsciiLine(InputStream in) throws IOException { // TODO: support UTF-8 here instead StringBuilder result = new StringBuilder(80); while (true) { int c = in.read(); if (c == -1) { throw new EOFException(); } else if (c == '\n') { break; } result.append((char) c); } int length = result.length(); if (length > 0 && result.charAt(length - 1) == '\r') { result.setLength(length - 1); } return result.toString(); }
From source file:org.psikeds.resolutionengine.datalayer.vo.ValueObject.java
protected static String composeId(final String... ids) { final StringBuilder sb = new StringBuilder(); if (ids != null) { for (final String vid : ids) { if (!StringUtils.isEmpty(vid)) { if (sb.length() > 0) { sb.append(COMPOSE_ID_SEPARATOR); }/*from w w w . j av a 2s .c o m*/ sb.append(vid.trim()); } } } return sb.toString(); }
From source file:net.duckling.ddl.util.StringUtil.java
License:asdf
public static String getSQLInFromInt(Collection<Integer> ints) { StringBuilder sb = new StringBuilder(); sb.append("("); for (Integer i : ints) { sb.append(i).append(","); }//w ww .ja va 2 s . c o m sb.delete(sb.length() - 1, sb.length()); sb.append(")"); return sb.toString(); }
From source file:com.google.cloud.genomics.dockerflow.util.FileUtils.java
/** Read complete stream into a string. */ public static String readAll(InputStream is) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line;/*from ww w . j av a2 s.c om*/ while ((line = in.readLine()) != null) { if (sb.length() > 0) { sb.append("\n"); } sb.append(line); } in.close(); return sb.toString(); }
From source file:com.jayway.restassured.internal.print.ResponsePrinter.java
private static String toString(Headers headers) { if (!headers.exist()) { return ""; }//from w ww. j a va 2 s. c o m final StringBuilder builder = new StringBuilder(); for (Header header : headers) { builder.append(header.getName()).append(HEADER_NAME_AND_VALUE_SEPARATOR).append(header.getValue()) .append("\n"); } builder.deleteCharAt(builder.length() - 1); return builder.toString(); }