Example usage for java.lang StringBuffer substring

List of usage examples for java.lang StringBuffer substring

Introduction

In this page you can find the example usage for java.lang StringBuffer substring.

Prototype

@Override
public synchronized String substring(int start, int end) 

Source Link

Usage

From source file:org.glite.slcs.shibclient.TestShibbolethClient.java

public static String getCertificate(StringBuffer response) {
    String pemCert = null;//  w  ww.j a  va 2  s .  co  m
    int start = response.indexOf("<Certificate>");
    if (start != -1) {
        start += "<Certificate>".length();
        int stop = response.indexOf("</Certificate>", start);
        if (stop != -1) {
            pemCert = response.substring(start, stop);
        } else {
            System.err.println("</Certificate> not found!");
        }
    } else {
        System.err.println("<Certificate> not found!");
    }
    return pemCert;

}

From source file:ch.qos.logback.core.pattern.parser2.PatternParser.java

/**
 * Escapes regex characters in a substring. This is used to escape any regex
 * special chars in a layout pattern, which could be unintentionally
 * interpreted by the Java regex engine.
 *
 * @param s string buffer, containing the substring to be escaped; escape sequences are
 * inserted for literals in this buffer/*from   ww w  .  ja va2 s. co  m*/
 * @param start zero-based starting character position within {@code s} to be escaped
 * @param length number of characters of substring
 * @return number of new characters added to string buffer
 */
static private int escapeRegexChars(StringBuffer s, int start, int length) {
    String substr = s.substring(start, start + length);

    Matcher matcher = REGEX_CHARS_PATTERN.matcher(substr);
    int numNewChars = 0;

    while (matcher.find()) {
        s.insert(matcher.start() + numNewChars + start, ESC_SEQ);
        numNewChars += ESC_SEQ_LEN;
    }
    return numNewChars;
}

From source file:ch.qos.logback.core.pattern.parser2.PatternParser.java

/**
 * Unescapes (removes escape sequence) regex characters in a substring. This
 * is the converse of {@link #escapeRegexChars(StringBuffer, int, int)}.
 *
 * @param s string buffer, containing the substring to be unescaped; escape sequences are
 * removed from literals in this buffer//from  w w  w  .  j  ava  2s .  c o  m
 * @param start zero-based starting character position within {@code s} to be unescaped
 * @param length number of characters of substring
 * @return number of characters removed from the string buffer
 */
static private int unescapeRegexChars(StringBuffer s, int start, int length) {
    String substr = s.substring(start, start + length);
    int numRemovedChars = 0;
    Matcher matcher = REGEX_CHARS_PATTERN.matcher(substr);

    while (matcher.find()) {
        int idxOfRegexChar = matcher.start() - numRemovedChars + start;
        int idxOfEscSeq = idxOfRegexChar - ESC_SEQ_LEN;

        if (idxOfEscSeq >= 0) {
            String prefix = s.substring(idxOfEscSeq, idxOfRegexChar);
            if (prefix.equals(ESC_SEQ)) {
                s.delete(idxOfEscSeq, idxOfRegexChar);
                numRemovedChars += ESC_SEQ_LEN;
            }
        }
    }
    return numRemovedChars;
}

From source file:com.robin.testcase.ApkResigner.java

private static String getAutVersion(final File autFile) {
    AndroidTools androidTools = AndroidTools.get();
    String versionCode = "";
    Process2 process = null;/*from   w w w.  j  av a 2s .c o  m*/
    try {
        StringBuffer strBuffer = new StringBuffer();
        process = androidTools.aapt("dumb", "badging", autFile.getAbsolutePath());
        process.connectStdout(strBuffer).waitForSuccess();

        int versionCodePosition = strBuffer.indexOf("versionCode");
        int versionControlEndPosition = strBuffer.indexOf(" ", versionCodePosition);
        versionCode = splitVersionCode(strBuffer.substring(versionCodePosition, versionControlEndPosition));
    } catch (InterruptedException e) {
        versionCode = "NotSet";
    } catch (IOException e) {
        versionCode = "NotSet";
    }
    return versionCode;
}

From source file:org.glite.slcs.shibclient.TestShibbolethClient.java

public static String getAuthorizationToken(StringBuffer response) {
    String authToken = null;// www .  j  a  v a  2 s  .c om
    int start = response.indexOf("<AuthorizationToken>");
    if (start != -1) {
        start += "<AuthorizationToken>".length();
        int stop = response.indexOf("</AuthorizationToken>", start);
        if (stop != -1) {
            authToken = response.substring(start, stop);
        } else {
            System.err.println("</AuthorizationToken> not found!");
        }
    } else {
        System.err.println("<AuthorizationToken> not found!");
    }
    return authToken;
}

From source file:IndexService.IndexMR.java

public static RunningJob run(Configuration conf2, String inputfiles, boolean column, String ids,
        String outputdir) {//w  w  w. ja  v  a  2  s .co m
    if (inputfiles == null || outputdir == null)
        return null;

    JobConf conf = new JobConf(conf2);
    conf.setJobName("IndexMR:\t" + ids);
    conf.setJarByClass(IndexMR.class);
    FileSystem fs = null;
    try {
        fs = FileSystem.get(conf);
        fs.delete(new Path(outputdir), true);
    } catch (IOException e3) {
        e3.printStackTrace();
    }

    conf.set("index.ids", ids);
    if (column) {
        conf.set("datafiletype", "column");
    } else {
        conf.set("datafiletype", "format");
    }

    String[] ifs = inputfiles.split(",");
    long wholerecnum = 0;

    String[] idxs = ids.split(",");
    String[] fieldStrings = new String[idxs.length + 2];

    if (!column) {
        IFormatDataFile ifdf;
        try {
            ifdf = new IFormatDataFile(conf);
            ifdf.open(ifs[0]);
            for (int i = 0; i < idxs.length; i++) {
                int id = Integer.parseInt(idxs[i]);
                byte type = ifdf.fileInfo().head().fieldMap().fieldtypes().get(id).type();
                fieldStrings[i] = type + ConstVar.RecordSplit + i;
            }
            ifdf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            IColumnDataFile icdf = new IColumnDataFile(conf);
            icdf.open(ifs[0]);
            for (int i = 0; i < idxs.length; i++) {
                int id = Integer.parseInt(idxs[i]);
                byte type = icdf.fieldtypes().get(id).type();
                fieldStrings[i] = type + ConstVar.RecordSplit + i;
            }
            icdf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    fieldStrings[fieldStrings.length - 2] = ConstVar.FieldType_Short + ConstVar.RecordSplit
            + (fieldStrings.length - 2);
    fieldStrings[fieldStrings.length - 1] = ConstVar.FieldType_Int + ConstVar.RecordSplit
            + (fieldStrings.length - 1);

    conf.setStrings(ConstVar.HD_fieldMap, fieldStrings);

    if (!column) {
        conf.set(ConstVar.HD_index_filemap, inputfiles);
        for (String file : ifs) {
            IFormatDataFile fff;
            try {
                fff = new IFormatDataFile(conf);
                fff.open(file);
                wholerecnum += fff.segIndex().recnum();
                fff.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        HashSet<String> files = new HashSet<String>();
        for (String file : ifs) {
            files.add(file);
        }
        StringBuffer sb = new StringBuffer();
        for (String str : files) {
            sb.append(str).append(",");
        }
        conf.set(ConstVar.HD_index_filemap, sb.substring(0, sb.length() - 1));

        for (String file : files) {
            Path parent = new Path(file).getParent();
            try {
                FileStatus[] fss = fs.listStatus(parent);
                String openfile = "";
                for (FileStatus status : fss) {
                    if (status.getPath().toString().contains(file)) {
                        openfile = status.getPath().toString();
                        break;
                    }
                }
                IFormatDataFile fff = new IFormatDataFile(conf);
                fff.open(openfile);
                wholerecnum += fff.segIndex().recnum();
                fff.close();

            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    conf.setNumReduceTasks((int) ((wholerecnum - 1) / (100000000) + 1));

    FileInputFormat.setInputPaths(conf, inputfiles);
    Path outputPath = new Path(outputdir);
    FileOutputFormat.setOutputPath(conf, outputPath);

    conf.setOutputKeyClass(IndexKey.class);
    conf.setOutputValueClass(IndexValue.class);

    conf.setPartitionerClass(IndexPartitioner.class);

    conf.setMapperClass(IndexMap.class);
    conf.setCombinerClass(IndexReduce.class);
    conf.setReducerClass(IndexReduce.class);

    if (column) {
        conf.setInputFormat(IColumnInputFormat.class);
    } else {
        conf.setInputFormat(IFormatInputFormat.class);
    }
    conf.setOutputFormat(IndexIFormatOutputFormat.class);

    try {
        JobClient jc = new JobClient(conf);
        return jc.submitJob(conf);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:acp.sdk.SDKUtil.java

/**
 * Map???Keyascii???key1=value1&key2=value2? ????signature
 * //w  w w  .  j  a v a  2s  . c om
 * @param data
 *            Map?
 * @return ?
 */
public static String coverMap2String(Map<String, String> data) {
    TreeMap<String, String> tree = new TreeMap<String, String>();
    Iterator<Entry<String, String>> it = data.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, String> en = it.next();
        if (SDKConstants.param_signature.equals(en.getKey().trim())) {
            continue;
        }
        tree.put(en.getKey(), en.getValue());
    }
    it = tree.entrySet().iterator();
    StringBuffer sf = new StringBuffer();
    while (it.hasNext()) {
        Entry<String, String> en = it.next();
        sf.append(en.getKey() + SDKConstants.EQUAL + en.getValue() + SDKConstants.AMPERSAND);
    }
    return sf.substring(0, sf.length() - 1);
}

From source file:com.inmobi.conduit.distcp.tools.util.DistCpUtils.java

/**
 * Pack file preservation attributes into a string, containing
 * just the first character of each preservation attribute
 * @param attributes - Attribute set to preserve
 * @return - String containing first letters of each attribute to preserve
 *///from  w  ww. j a  va 2 s .c  om
public static String packAttributes(EnumSet<FileAttribute> attributes) {
    StringBuffer buffer = new StringBuffer(5);
    int len = 0;
    for (FileAttribute attribute : attributes) {
        buffer.append(attribute.name().charAt(0));
        len++;
    }
    return buffer.substring(0, len);
}

From source file:arena.cron.CronUtils.java

/**
 * Take a single cron token, and remove any duplicates, modulus on max value, etc. Basically
 * clean it up and make it legal//from  w w  w  .ja va2  s  .c  o m
 */
protected static String formatCronPatternToken(String token, int minValue, int maxValue) {
    // Break on commas
    String splitOnCommas[] = splitOnCommas(token);
    for (int n = 0; n < splitOnCommas.length; n++) {
        splitOnCommas[n] = splitOnCommas[n].trim();
        if (splitOnCommas[n].equals(STAR)) {
            return STAR;
        }
        int parsed = Integer.parseInt(splitOnCommas[n]);
        if (parsed < minValue) {
            LogFactory.getLog(CronUtils.class).warn("Skipping cron pattern element " + splitOnCommas[n]
                    + " - should be between " + minValue + " and " + maxValue);
        } else if (parsed > maxValue) {
            parsed = ((parsed - minValue) % (maxValue + 1 - minValue)) + minValue;
        }
        splitOnCommas[n] = Integer.toString(parsed);
    }

    Arrays.sort(splitOnCommas, new Comparator<String>() {
        public int compare(String one, String two) {
            return Integer.valueOf(one).compareTo(Integer.valueOf(two));
        }
    });
    StringBuffer out = new StringBuffer();
    for (int n = 0; n < splitOnCommas.length; n++) {
        if ((n == 0) || !splitOnCommas[n].equals(splitOnCommas[n - 1])) {
            out.append(splitOnCommas[n]).append(",");
        }
    }
    return out.length() > 0 ? out.substring(0, out.length() - 1) : STAR;
}

From source file:org.yestech.lib.util.DecoderUtil.java

private static void uriDecode(final StringBuffer buffer, final int offset, final int length) {
    int index = offset;
    int count = length;
    for (; count > 0; count--, index++) {
        final char ch = buffer.charAt(index);
        if (ch != '%') {
            continue;
        }//from www. j  av a 2  s  . co  m
        if (count < 3) {
            throw new RuntimeException(
                    "invalid-escape-sequence.error: " + buffer.substring(index, index + count));
        }

        // Decode
        int dig1 = Character.digit(buffer.charAt(index + 1), 16);
        int dig2 = Character.digit(buffer.charAt(index + 2), 16);
        if (dig1 == -1 || dig2 == -1) {
            throw new RuntimeException("invalid-escape-sequence.error " + buffer.substring(index, index + 3));
        }
        char value = (char) (dig1 << 4 | dig2);

        // Replace
        buffer.setCharAt(index, value);
        buffer.delete(index + 1, index + 3);
        count -= 2;
    }
}