Example usage for java.lang StringBuilder subSequence

List of usage examples for java.lang StringBuilder subSequence

Introduction

In this page you can find the example usage for java.lang StringBuilder subSequence.

Prototype

CharSequence subSequence(int start, int end);

Source Link

Document

Returns a CharSequence that is a subsequence of this sequence.

Usage

From source file:org.dcache.xrootd.plugins.authn.gsi.CertUtil.java

/**
 * Decodes PEM by removing the given header and footer, and decodes
 * the inner content with base64.//www.j a v a 2 s . c om
 * @param pem the full PEM-encoded data including header + footer
 * @param header the header to be striped off
 * @param footer the footer to be striped off
 * @return the content in DER format
 */
public static byte[] fromPEM(String pem, String header, String footer) {
    if (!pem.startsWith(header)) {
        throw new IllegalArgumentException("The provided PEM string doesn't start with '" + header + "'");
    }

    // strip header
    StringBuilder sb = new StringBuilder(pem);
    sb.delete(0, header.length());

    removeChar(sb, '\n');

    // remove footer
    if (!sb.subSequence(sb.length() - footer.length(), sb.length()).equals(footer)) {
        throw new IllegalArgumentException("The provided PEM string doesn't end with '" + footer + "'");
    }
    sb.delete(sb.indexOf(footer), sb.length());

    // finally decode base64
    return Base64.decodeBase64(sb.toString());
}

From source file:org.eclipse.dataset.AbstractDataset.java

/**
 * Make a line of output for last dimension of dataset
 * //from w  w  w . j  a  v a 2s.  c  o m
 * @param start
 * @return line
 */
private StringBuilder makeLine(final int end, final int... start) {
    StringBuilder line = new StringBuilder();
    final int[] pos;
    if (end >= start.length) {
        pos = Arrays.copyOf(start, end + 1);
    } else {
        pos = start;
    }
    pos[end] = 0;
    line.append(BLOCK_OPEN);
    line.append(getString(pos));

    final int length = shape[end];

    // trim elements printed if length exceed estimate of maximum elements
    int excess = length - maxStringLength / 3; // space + number + separator
    if (excess > 0) {
        int index = (length - excess) / 2;
        for (int y = 1; y < index; y++) {
            line.append(SEPARATOR + SPACE);
            pos[end] = y;
            line.append(getString(pos));
        }
        index = (length + excess) / 2;
        for (int y = index; y < length; y++) {
            line.append(SEPARATOR + SPACE);
            pos[end] = y;
            line.append(getString(pos));
        }
    } else {
        for (int y = 1; y < length; y++) {
            line.append(SEPARATOR + SPACE);
            pos[end] = y;
            line.append(getString(pos));
        }
    }
    line.append(BLOCK_CLOSE);

    // trim string down to limit
    excess = line.length() - maxStringLength - ELLIPSIS.length() - 1;
    if (excess > 0) {
        int index = line.substring(0, (line.length() - excess) / 2).lastIndexOf(SEPARATOR) + 2;
        StringBuilder out = new StringBuilder(line.subSequence(0, index));
        out.append(ELLIPSIS + SEPARATOR);
        index = line.substring((line.length() + excess) / 2).indexOf(SEPARATOR) + (line.length() + excess) / 2
                + 1;
        out.append(line.subSequence(index, line.length()));
        return out;
    }

    return line;
}

From source file:org.lockss.rewriter.RegexpCssLinkRewriterFactory.java

public InputStream createLinkRewriter(String mimeType, ArchivalUnit au, InputStream in, String encoding,
        String srcUrl, ServletUtil.LinkTransform srvLinkXform) throws PluginException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("Called with null InputStream");
    }//from  w w w. ja  v a  2  s.  co m
    // Cause error now if illegal base url
    try {
        new URL(srcUrl);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
    }
    log.debug("Rewriting " + srcUrl + " in AU " + au);
    setConfig();
    Collection<String> urlStems = au.getUrlStems();
    StringBuilder out = new StringBuilder();

    // This needs a regexp matcher that can match against a Reader.
    // Interim solution is to loop matching against a rolling fixed-length
    // chunk of input, with overlaps between chunks.  Can miss URLs in
    // pathological situations.

    Reader rdr = new BufferedReader(StringUtil.getLineReader(in, encoding));
    rdr = StringUtil.getLineContinuationReader(rdr);
    StringBuilder sb = new StringBuilder(maxBuf);
    try {
        while (StringUtil.fillFromReader(rdr, sb, maxBuf - sb.length())) {
            Matcher m1 = CSS_URL_PAT.matcher(sb);
            int lastAppendPosition = 0;
            while (m1.find()) {
                String url = processUrlEscapes(m1.group(GURL));
                String rewritten = rewrite(url, srcUrl, urlStems, srvLinkXform);
                if (url.equals(rewritten)) {
                    out.append(sb.subSequence(lastAppendPosition, m1.end()));
                    lastAppendPosition = m1.end();
                } else {
                    out.append(sb.subSequence(lastAppendPosition, m1.start(GQUOTE1)));
                    out.append("'");
                    out.append(urlEscape(rewritten));
                    out.append("'");
                    lastAppendPosition = m1.end(GQUOTE2);
                    out.append(sb.subSequence(lastAppendPosition, m1.end()));
                    lastAppendPosition = m1.end();
                }
            }
            int sblen = sb.length();
            if (sblen < maxBuf) {
                // less then full buffer means last buffer
                out.append(sb.subSequence(lastAppendPosition, sblen));
                break;
            }
            // Move the overlap amount to the beginning of the buffer
            int keep;
            if (lastAppendPosition == 0) {
                // no matches, shift all but overlap
                keep = Math.min(overlap, maxBuf / 2);
            } else {
                // keep chars after last match, or max overlap
                keep = Math.min(overlap, sblen - lastAppendPosition);
            }
            out.append(sb.subSequence(lastAppendPosition, sblen - keep));
            sb.delete(0, sblen - keep);
        }
    } finally {
        IOUtil.safeClose(rdr);
    }
    return new ReaderInputStream(new StringReader(out.toString()), encoding);
}

From source file:org.semanticweb.owlapi.model.IRI.java

@Override
public CharSequence subSequence(int start, int end) {
    StringBuilder sb = new StringBuilder(namespace);
    sb.append(remainder);/*from  ww  w .j  ava  2  s.c o m*/
    return sb.subSequence(start, end);
}

From source file:org.springsource.ide.eclipse.commons.gettingstarted.dashboard.BlogsProvider.java

private int findEndOfWord(StringBuilder sb, int pos) {
    Pattern pattern = Pattern.compile("\\w");
    while (pos < sb.length()) {
        if (pattern.matcher(sb.subSequence(pos, pos + 1)).matches()) {
            pos++;/*from  www  .  j  av a2  s . c om*/
        } else {
            return pos;
        }
    }
    return pos;
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java

public static String removeObfuscatedEncoding(StringBuilder sb, int startInd, int endInd, EncodingType[] type) {
    StringBuilder nsb = new StringBuilder(sb.subSequence(startInd, endInd));
    removeObfuscatedEncoding(nsb, type);
    sb.replace(startInd, endInd, nsb.toString());
    return nsb.toString();
}

From source file:pltag.corpus.StringTree.java

/**
 * Prints the structure of a string tree in bracketed notation
 *
 * @param nodeID//w  w  w .  j av  a  2  s  . c o m
 * @param printSemantics
 * @return
 */
public String getStructure(int nodeID, boolean printSemantics) {
    StringBuilder struct = new StringBuilder();
    ArrayList<Integer> childlist = children.get(nodeID);
    if (childlist == null || childlist.isEmpty()) {
        return getLexName(nodeID, printSemantics);
        //return nodeID + ":" +getLexName(nodeID);
    }
    StringBuilder childString = new StringBuilder();
    for (Integer childid : childlist) {
        //System.err.print(nodeID + ":" + childString + " " + childid + "\n");
        childString.append(" ").append(getStructure(childid, printSemantics));
    }
    String structString;
    if (!fullcategories[nodeID].equals(""))
    //        if (!fullcategories[Integer.parseInt(nodeID)].equals(""))
    {
        struct.append("( ").append(getLexName(nodeID, printSemantics)).append(childString.toString())
                .append(")");
        structString = struct.toString();
    } else {
        int upind = childString.indexOf("^");
        int downind = childString.indexOf("_");
        structString = childString.subSequence(0, upind + 1) + "null" + childString.substring(downind);
        structString = structString.trim();
    }
    //struct = "( "+nodeID + ":" + getLexName(nodeID)+childString+")";
    return structString;
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.AbstractDataset.java

/**
 * Make a line of output for last dimension of dataset
 * //from w ww.java  2 s  .  c  o  m
 * @param start
 * @return line
 */
private StringBuilder makeLine(final int end, final int... start) {
    StringBuilder line = new StringBuilder();
    final int[] pos;
    if (end >= start.length) {
        pos = Arrays.copyOf(start, end + 1);
    } else {
        pos = start;
    }
    pos[end] = 0;
    line.append('[');
    line.append(getString(pos));

    final int length = shape[end];

    // trim elements printed if length exceed estimate of maximum elements
    int excess = length - MAX_STRING_LENGTH / 3; // space + number + separator
    if (excess > 0) {
        int index = (length - excess) / 2;
        for (int y = 1; y < index; y++) {
            line.append(SEPARATOR + SPACING);
            pos[end] = y;
            line.append(getString(pos));
        }
        index = (length + excess) / 2;
        for (int y = index; y < length; y++) {
            line.append(SEPARATOR + SPACING);
            pos[end] = y;
            line.append(getString(pos));
        }
    } else {
        for (int y = 1; y < length; y++) {
            line.append(SEPARATOR + SPACING);
            pos[end] = y;
            line.append(getString(pos));
        }
    }
    line.append(']');

    // trim string down to limit
    excess = line.length() - MAX_STRING_LENGTH - ELLIPSES.length() - 1;
    if (excess > 0) {
        int index = line.substring(0, (line.length() - excess) / 2).lastIndexOf(SEPARATOR) + 2;
        StringBuilder out = new StringBuilder(line.subSequence(0, index));
        out.append(ELLIPSES + SEPARATOR);
        index = line.substring((line.length() + excess) / 2).indexOf(SEPARATOR) + (line.length() + excess) / 2
                + 1;
        out.append(line.subSequence(index, line.length()));
        return out;
    }

    return line;
}

From source file:wsattacker.plugin.dos.dosExtension.util.UtilHashDoS.java

public static String determineUntamperedAttributeName(CollisionInterface collisionInterface,
        int numberOfAttributes, boolean useNamespace) {
    String attributeName = DEFAULT_ATTRIBUTENAME;

    StringBuilder builder = new StringBuilder("");

    // n viele Kollisionen erzeugen
    collisionInterface.genNCollisions(numberOfAttributes, builder, useNamespace);
    int indexOf = builder.indexOf("=");
    if (indexOf != -1) {
        int start = 0;
        if (builder.indexOf(":") != -1) {
            start = builder.indexOf(":") + 1;
        }/*  w w w.  j a  va  2 s  .  com*/

        CharSequence subSequence = builder.subSequence(start, indexOf);
        // length for the attribute name -> e.g. aaaa = 4
        int count = subSequence.length();
        // substract the count -> e.g. 10 elements -> aaa0 - aaa9
        count -= String.valueOf(numberOfAttributes).length();

        if (count > 0) {
            attributeName = RandomStringUtils.randomAlphabetic(count);
        }
    }

    return attributeName;
}