List of usage examples for java.lang StringBuffer ensureCapacity
@Override public synchronized void ensureCapacity(int minimumCapacity)
From source file:Main.java
public static void main(String args[]) { StringBuffer sb = new StringBuffer(); sb.ensureCapacity(100); sb.append("java2s.com"); System.out.println(sb.capacity()); System.out.println(sb.length()); System.out.println(sb.toString()); }
From source file:MainClass.java
public static void main(String[] arg) { StringBuffer newString = new StringBuffer("abcde"); System.out.println(newString.capacity()); newString.ensureCapacity(40); System.out.println(newString.capacity()); }
From source file:com.slimsmart.common.util.code.EncodeUtil.java
public static String escape(String src) { int i;//from w ww . j ava2 s .c om char j; StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length() * 6); for (i = 0; i < src.length(); i++) { j = src.charAt(i); if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) tmp.append(j); else if (j < 256) { tmp.append("%"); if (j < 16) tmp.append("0"); tmp.append(Integer.toString(j, 16)); } else { tmp.append("%u"); tmp.append(Integer.toString(j, 16)); } } return tmp.toString(); }
From source file:com.slimsmart.common.util.code.EncodeUtil.java
public static String unescape(String src) { StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length()); int lastPos = 0, pos = 0; char ch;/* ww w . j a v a 2s . c o m*/ while (lastPos < src.length()) { pos = src.indexOf("%", lastPos); if (pos == lastPos) { if (src.charAt(pos + 1) == 'u') { ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16); tmp.append(ch); lastPos = pos + 6; } else { ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16); tmp.append(ch); lastPos = pos + 3; } } else { if (pos == -1) { tmp.append(src.substring(lastPos)); lastPos = src.length(); } else { tmp.append(src.substring(lastPos, pos)); lastPos = pos; } } } return tmp.toString(); }
From source file:com.oakhole.utils.EncodeUtils.java
/** * jsescape.//www . j a v a 2 s . c o m * * @param src * String * @return String */ public static String escapeJS(String src) { int i; char j; StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length() * UNICODE_LENGTH); for (i = 0; i < src.length(); i++) { j = src.charAt(i); if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) { tmp.append(j); } else if (j < ANSI_CHAR_CODE) { tmp.append("%"); if (j < UNPRINTABLE_CHAR_CODE) { tmp.append("0"); } tmp.append(Integer.toString(j, HEX)); } else { tmp.append("%u"); tmp.append(Integer.toString(j, HEX)); } } return tmp.toString(); }
From source file:com.oakhole.utils.EncodeUtils.java
/** * jsunescape.//from www. j a va 2s . co m * * @param src * String * @return String */ public static String unescapeJS(String src) { StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length()); int lastPos = 0; int pos = 0; char ch; while (lastPos < src.length()) { pos = src.indexOf('%', lastPos); if (pos == lastPos) { if (src.charAt(pos + 1) == 'u') { ch = (char) Integer.parseInt(src.substring(pos + 2, pos + UNICODE_LENGTH), HEX); tmp.append(ch); lastPos = pos + UNICODE_LENGTH; } else { ch = (char) Integer.parseInt(src.substring(pos + 1, pos + ANSI_LENGTH), HEX); tmp.append(ch); lastPos = pos + ANSI_LENGTH; } } else { if (pos == -1) { tmp.append(src.substring(lastPos)); lastPos = src.length(); } else { tmp.append(src.substring(lastPos, pos)); lastPos = pos; } } } return tmp.toString(); }
From source file:eap.util.EDcodeUtil.java
public static String unescape(String src) { StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length()); int lastPos = 0, pos = 0; char ch;/* www .j a v a 2 s . c om*/ while (lastPos < src.length()) { pos = src.indexOf("%", lastPos); if (pos == lastPos) { if (src.charAt(pos + 1) == 'u') { ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16); tmp.append(ch); lastPos = pos + 6; } else { ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16); tmp.append(ch); lastPos = pos + 3; } } else { if (pos == -1) { tmp.append(src.substring(lastPos)); lastPos = src.length(); } else { tmp.append(src.substring(lastPos, pos)); lastPos = pos; } } } return tmp.toString(); }
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/*from w w w. j av a 2 s .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:com.adito.boot.ReplacementEngine.java
private void replaceInto(Pattern pattern, String replacementPattern, Replacer replacer, StringBuffer input, StringBuffer work) { work.ensureCapacity(input.length()); work.setLength(0);//from www . j av a 2s. co m if (log.isDebugEnabled()) log.debug("Getting matcher for " + pattern.pattern()); Matcher m = pattern.matcher(input); log.debug("Got matcher, finding first occurence."); while (m.find()) { if (log.isDebugEnabled()) log.debug("Found occurence '" + m.group() + "'"); String repl = replacer.getReplacement(pattern, m, replacementPattern); if (repl != null) { if (log.isDebugEnabled()) log.debug("Found replacement, appending '" + repl + "'"); if (encoder == null) { m.appendReplacement(work, Util.escapeForRegexpReplacement(repl)); } else { m.appendReplacement(work, encoder.encode(Util.escapeForRegexpReplacement(repl))); } } } if (log.isDebugEnabled()) log.debug("Processed matches, appending replacement."); m.appendTail(work); }
From source file:org.regenstrief.util.Util.java
/** * Appends part of the given CharSequence to the given StringBuffer, allocating the StringBuffer * if necessary//from w w w . ja va2 s.co m * * @param sb the StringBuffer * @param s the String * @param offset the offset of the CharSequence at which to start copying * @param len the number of characters to copy * @return the StringBuffer **/ public final static StringBuffer append(StringBuffer sb, final CharSequence s, int offset, int len) { if (sb == null) { sb = new StringBuffer(len); } sb.ensureCapacity(sb.length() + len); for (len += offset; offset < len; offset++) { sb.append(s.charAt(offset)); } return sb; }