List of usage examples for java.lang StringBuffer charAt
@Override public synchronized char charAt(int index)
From source file:org.ecoinformatics.datamanager.database.DelimitedReader.java
private static StringBuffer shiftBuffer(StringBuffer buffer, char newChar) { StringBuffer newBuffer = new StringBuffer(); if (buffer == null) { return newBuffer; }//from w w w .j a v a 2 s . c o m int size = buffer.length(); for (int i = 0; i < size; i++) { char oldChar = buffer.charAt(i); if (i > 0) { newBuffer.append(oldChar); } } newBuffer.append(newChar); return newBuffer; }
From source file:org.wso2.carbon.identity.core.persistence.IdentityDBInitializer.java
/** * Checks that a string buffer ends up with a given string. It may sound * trivial with the existing//from ww w .j a v a2 s . c om * JDK API but the various implementation among JDKs can make those * methods extremely resource intensive * and perform poorly due to massive memory allocation and copying. See * * @param buffer the buffer to perform the check on * @param suffix the suffix * @return <code>true</code> if the character sequence represented by the * argument is a suffix of the character sequence represented by * the StringBuffer object; <code>false</code> otherwise. Note that the * result will be <code>true</code> if the argument is the * empty string. */ public static boolean checkStringBufferEndsWith(StringBuffer buffer, String suffix) { if (suffix.length() > buffer.length()) { return false; } // this loop is done on purpose to avoid memory allocation performance // problems on various JDKs // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and // implementation is ok though does allocation/copying // StringBuffer.toString().endsWith() does massive memory // allocation/copying on JDK 1.5 // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169 int endIndex = suffix.length() - 1; int bufferIndex = buffer.length() - 1; while (endIndex >= 0) { if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) { return false; } bufferIndex--; endIndex--; } return true; }
From source file:gov.jgi.meta.MetaUtils.java
/** * generate a set (unique) of all sequences ({ATGC} only) from start sequence * to distance steps (Hamming distance)//from w ww .jav a 2 s .c o m * * @param start the start sequence * @param distance the number of steps to travel * @return s set containing all neighbors including itself. */ public static Set<String> generateAllNeighbors(StringBuffer start, int distance) { char[] bases = { 'a', 't', 'g', 'c' }; Set<String> s = new HashSet<String>(); if (distance == 0) { s.add(start.toString()); return (s); } for (int i = 0; i < start.length(); i++) { char old = start.charAt(i); for (char basePair : bases) { start.setCharAt(i, basePair); s.addAll(generateAllNeighbors(start, distance - 1)); } start.setCharAt(i, old); } return (s); }
From source file:org.kuali.rice.kns.lookup.LookupUtils.java
@Deprecated public static String getBaseInquiryUrl() { if (BASE_INQUIRY_ACTION_URL == null) { StringBuffer inquiryUrl = new StringBuffer(CoreApiServiceLocator.getKualiConfigurationService() .getPropertyValueAsString(KRADConstants.APPLICATION_URL_KEY)); if (inquiryUrl.charAt(inquiryUrl.length() - 1) != '/') { inquiryUrl.append('/'); }//from ww w .j a v a 2 s. c om inquiryUrl.append("kr/"); inquiryUrl.append(KRADConstants.INQUIRY_ACTION); BASE_INQUIRY_ACTION_URL = inquiryUrl.toString(); } return BASE_INQUIRY_ACTION_URL; }
From source file:org.zywx.wbpalmstar.plugin.uexzxing.qrcode.decoder.DecodedBitStreamParser.java
private static void decodeAlphanumericSegment(BitSource bits, StringBuffer result, int count, boolean fc1InEffect) throws FormatException { // Read two characters at a time int start = result.length(); while (count > 1) { int nextTwoCharsBits = bits.readBits(11); result.append(toAlphaNumericChar(nextTwoCharsBits / 45)); result.append(toAlphaNumericChar(nextTwoCharsBits % 45)); count -= 2;//from www . ja v a 2 s . c o m } if (count == 1) { // special case: one character left result.append(toAlphaNumericChar(bits.readBits(6))); } // See section 6.4.8.1, 6.4.8.2 if (fc1InEffect) { // We need to massage the result a bit if in an FNC1 mode: for (int i = start; i < result.length(); i++) { if (result.charAt(i) == '%') { if (i < result.length() - 1 && result.charAt(i + 1) == '%') { // %% is rendered as % result.deleteCharAt(i + 1); } else { // In alpha mode, % should be converted to FNC1 separator 0x1D result.setCharAt(i, (char) 0x1D); } } } } }
From source file:org.eclim.plugin.jdt.util.JavaUtils.java
/** * Format a region in the supplied source file. * * @param src The ICompilationUnit.//from w w w . j a va 2 s .c om * @param kind The kind of code snippet to format. * @param offset The starting offset of the region to format. * @param length The length of the region to format. */ public static void format(ICompilationUnit src, int kind, int offset, int length) throws Exception { IBuffer buffer = src.getBuffer(); String contents = buffer.getContents(); String delimiter = StubUtility.getLineDelimiterUsed(src); DefaultCodeFormatter formatter = new DefaultCodeFormatter(src.getJavaProject().getOptions(true)); // when the eclipse indent settings differ from vim (tabs vs spaces) then // the inserted method's indent may be a bit off. this is a workaround to // force reformatting of the code from the start of the line to the start of // the next set of code following the new method. Doesn't quite fix indent // formatting of methods in nested classes. while (offset > 0 && !IndentManipulation.isLineDelimiterChar(buffer.getChar(offset - 1))) { offset--; length++; } while ((offset + length) < contents.length() && IndentManipulation.isLineDelimiterChar(buffer.getChar(offset + length))) { length++; } TextEdit edits = formatter.format(kind, contents, offset, length, 0, delimiter); if (edits != null) { int oldLength = contents.length(); Document document = new Document(contents); edits.apply(document); String formatted = document.get(); // jdt formatter can introduce trailing whitespace (javadoc comments), so // we'll remove all trailing whitespace from the formatted section. length += formatted.length() - oldLength; if (offset < (offset + length)) { String pre = formatted.substring(0, offset); StringBuffer section = new StringBuffer(formatted.substring(offset, offset + length)); StringBuffer post = new StringBuffer(formatted.substring(offset + length)); // account for section not ending at a line delimiter while (!section.toString().endsWith(delimiter) && post.length() > 0) { section.append(post.charAt(0)); post.deleteCharAt(0); } Matcher matcher = TRAILING_WHITESPACE.matcher(section); String stripped = matcher.replaceAll(StringUtils.EMPTY); src.getBuffer().setContents(pre + stripped + post); } else { src.getBuffer().setContents(formatted); } if (src.isWorkingCopy()) { src.commitWorkingCopy(true, null); } src.save(null, false); } }
From source file:XMLUtils.java
/** * Returns the text value of an element. * @param el// ww w . j av a2 s . c om * @return */ public static String getTextValue(Element el) { StringBuffer b = new StringBuffer(); // retrieve the text node child NodeList nl = el.getChildNodes(); int len = nl.getLength(); for (int i = 0; i < len; i++) { Node n = nl.item(i); if (n instanceof Text) { Text t = (Text) n; b.append(t.getData()); } } // trim the result, ignoring the first spaces and cariage return int iFirst = 0; for (; iFirst < b.length(); iFirst++) { char c = b.charAt(iFirst); if (c != ' ' && c != '\r' && c != '\n' && c != '\t') { break; } } // start by the end as well int iLast = b.length() - 1; for (; iLast >= 0; iLast--) { char c = b.charAt(iLast); if (c != ' ' && c != '\r' && c != '\n' && c != '\t') { break; } } return b.substring(iFirst, iLast + 1); }
From source file:com.projity.server.data.MPXConverter.java
public static String removeInvalidChars(String in) { // had case of user with newlines in task names if (in == null) return null; StringBuffer inBuf = new StringBuffer(in); for (int i = 0; i < inBuf.length(); i++) { char c = inBuf.charAt(i); if (c == '\r' || c == '\n' || c == '\t') // using escape chars of the form � is not good - they show up in MSP literally. MSP doesn't seem to support newlines anyway inBuf.setCharAt(i, ' '); }//from ww w . j a v a 2s .c om return inBuf.toString(); }
From source file:ca.simplegames.micro.utils.StringUtils.java
/** * Trim all occurences of the supplied leading character from the given String. * * @param str the String to check * @param leadingCharacter the leading character to be trimmed * @return the trimmed String/*from ww w. ja va2 s . c o m*/ */ public static String trimLeadingCharacter(String str, char leadingCharacter) { if (!hasLength(str)) { return str; } StringBuffer buf = new StringBuffer(str); while (buf.length() > 0 && buf.charAt(0) == leadingCharacter) { buf.deleteCharAt(0); } return buf.toString(); }
From source file:ca.simplegames.micro.utils.StringUtils.java
/** * Trim all occurences of the supplied trailing character from the given String. * * @param str the String to check * @param trailingCharacter the trailing character to be trimmed * @return the trimmed String/* w w w . j a v a2 s.c o m*/ */ public static String trimTrailingCharacter(String str, char trailingCharacter) { if (!hasLength(str)) { return str; } StringBuffer buf = new StringBuffer(str); while (buf.length() > 0 && buf.charAt(buf.length() - 1) == trailingCharacter) { buf.deleteCharAt(buf.length() - 1); } return buf.toString(); }