Example usage for java.lang StringBuffer delete

List of usage examples for java.lang StringBuffer delete

Introduction

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

Prototype

@Override
public synchronized StringBuffer delete(int start, int end) 

Source Link

Usage

From source file:net.sf.infrared.collector.impl.persistence.ApplicationStatisticsDaoImpl.java

Object[] processQueryAndArgArrayForCollection(Object[] argArray, StringBuffer query, Collection appNames) {
    int length = 0;
    Object[] returnValues = new Object[2];

    for (Iterator iter = appNames.iterator(); iter.hasNext();) {
        String element = (String) iter.next();
        argArray[++argumentCount] = element;
        query.append("?, ");
    }//from ww w  .j  ava2s .com
    length = query.length();
    query.delete(length - 2, length - 1);
    returnValues[0] = query;
    returnValues[1] = argArray;

    return returnValues;
}

From source file:org.apache.axiom.attachments.impl.PartFactory.java

/**
 * The implementing class must call initHeaders prior to using
 * any of the Part methods.  /*w ww .  j  a va  2s  .co  m*/
 * @param is
 * @param headers
 */
private static InputStream readHeaders(InputStream in, Map headers) throws IOException {
    if (log.isDebugEnabled()) {
        log.debug("initHeaders");
    }
    boolean done = false;

    final int BUF_SIZE = 1024;
    byte[] headerBytes = new byte[BUF_SIZE];

    int size = in.read(headerBytes);
    int index = 0;
    StringBuffer sb = new StringBuffer(50);

    while (!done && index < size) {

        // Get the next byte
        int ch = headerBytes[index];
        index++;
        if (index == size) {
            size = in.read(headerBytes);
            index = 0;
        }

        if (ch == 13) {

            // Get the next byte
            ch = headerBytes[index];
            index++;
            if (index == size) {
                size = in.read(headerBytes);
                index = 0;
            }

            if (ch == 10) {
                // 13, 10 indicates we are starting a new line...thus a new header
                // Get the next byte
                ch = headerBytes[index];
                index++;
                if (index == size) {
                    size = in.read(headerBytes);
                    index = 0;
                }

                if (ch == 13) {

                    // Get the next byte
                    ch = headerBytes[index];
                    index++;
                    if (index == size) {
                        size = in.read(headerBytes);
                        index = 0;
                    }

                    if (ch == 10) {
                        // Blank line indicates we are done.
                        readHeader(sb, headers);
                        sb.delete(0, sb.length()); // Clear the buffer for reuse
                        done = true;
                    }
                } else {

                    // Semicolon is a continuation character
                    String check = sb.toString().trim();
                    if (!check.endsWith(";")) {
                        // now parse and add the header String
                        readHeader(sb, headers);
                        sb.delete(0, sb.length()); // Clear the buffer for reuse
                    }
                    sb.append((char) ch);
                }
            } else {
                sb.append(13);
                sb.append((char) ch);
            }
        } else {
            sb.append((char) ch);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("End initHeaders");
    }

    // Return an input stream containing the dross bits
    if (index >= size) {
        index = size;
    }
    ByteArrayInputStream dross = new ByteArrayInputStream(headerBytes, index, size - index);
    return dross;
}

From source file:org.josso.selfservices.password.generator.PasswordGeneratorImpl.java

/**
 * The real password generation is performed in this method
 *
 * @param size     the length of the password
 * @param pw_flags the settings for the password
 * @return the newly created password/*from   w w w  .  j a  v  a2s  . com*/
 */
private String phonemes(int size, int pw_flags) {
    int c, i, len, flags, feature_flags;
    int prev, should_be;
    boolean first;
    String str;
    char ch;
    StringBuffer buf = new StringBuffer();

    do {
        buf.delete(0, buf.length());
        feature_flags = pw_flags;
        c = 0;
        prev = 0;
        should_be = 0;
        first = true;
        should_be = random.nextBoolean() ? VOWEL : CONSONANT;

        while (c < size) {

            i = random.nextInt(PW_ELEMENTS.length);
            str = PW_ELEMENTS[i].getValue();
            len = str.length();
            flags = PW_ELEMENTS[i].getType();
            /* Filter on the basic type of the next element */
            if ((flags & should_be) == 0) {
                continue;
            }
            /* Handle the NOT_FIRST flag */
            if (first && ((flags & NOT_FIRST) != 0))
                continue;
            /* Don't allow VOWEL followed a Vowel/Dipthong pair */
            if (((prev & VOWEL) != 0) && ((flags & VOWEL) != 0) && ((flags & DIPTHONG) != 0))
                continue;
            /* Don't allow us to overflow the buffer */
            if (len > size - c)
                continue;
            /*
             * OK, we found an element which matches our criteria, let's do
             * it!
             */
            buf.append(str);

            /* Handle PW_UPPERS */
            if ((pw_flags & PW_UPPERS) != 0) {
                if ((first || ((flags & CONSONANT) != 0)) && (random.nextInt(10) < 2)) {
                    int lastChar = buf.length() - 1;
                    buf.setCharAt(lastChar, Character.toUpperCase(buf.charAt(lastChar)));
                    feature_flags &= ~PW_UPPERS;
                }
            }

            c += len;
            /* Handle the AMBIGUOUS flag */
            if ((pw_flags & PW_AMBIGUOUS) != 0) {
                int k = -1;
                for (int j = 0; j < PW_AMBIGUOUS_SYMBOLS.length(); j++) {
                    k = buf.indexOf(String.valueOf(PW_AMBIGUOUS_SYMBOLS.charAt(j)));
                    if (k != -1)
                        break;
                }
                if (k != -1) {
                    buf.delete(k, buf.length());
                    c = buf.length();
                }
            }

            /* Time to stop? */
            if (c >= size)
                break;

            /*
             * Handle PW_DIGITS
             */
            if ((pw_flags & PW_DIGITS) != 0) {
                if (!first && (random.nextInt(10) < 3)) {
                    do {
                        ch = (new Integer(random.nextInt(10))).toString().charAt(0);
                    } while (((pw_flags & PW_AMBIGUOUS) != 0) && (PW_AMBIGUOUS_SYMBOLS.indexOf(ch) != -1));
                    c++;
                    buf = buf.append(ch);
                    feature_flags &= ~PW_DIGITS;

                    first = true;
                    prev = 0;
                    should_be = random.nextBoolean() ? VOWEL : CONSONANT;
                    continue;
                }
            }

            /*
             * OK, figure out what the next element should be
             */
            if (should_be == CONSONANT) {
                should_be = VOWEL;
            } else { /* should_be == VOWEL */
                if (((prev & VOWEL) != 0) || ((flags & DIPTHONG) != 0) || (random.nextInt(10) > 3))
                    should_be = CONSONANT;
                else
                    should_be = VOWEL;
            }
            prev = flags;
            first = false;

            /* Handle PW_SYMBOLS */
            if ((pw_flags & PW_SYMBOLS) != 0) {
                if (!first && (random.nextInt(10) < 2)) {
                    do {
                        ch = PW_SPECIAL_SYMBOLS.charAt(random.nextInt(PW_SPECIAL_SYMBOLS.length()));
                    } while (((pw_flags & PW_AMBIGUOUS) != 0) && (PW_AMBIGUOUS_SYMBOLS.indexOf(ch) != -1));
                    c++;
                    buf = buf.append(ch);
                    feature_flags &= ~PW_SYMBOLS;
                }
            }

        }
    } while ((feature_flags & (PW_UPPERS | PW_DIGITS | PW_SYMBOLS)) != 0);

    return buf.toString();
}

From source file:org.j2free.util.ServletUtils.java

/**
 * Redirects the user to the current url over SSL
 *
 * @param request a HttpServletRequest/*www . j  a  va  2 s  .  c o m*/
 * @param response a HttpServletResponse
 * @param sslPort the port SSL requests should be forwarded to
 * @throws ServletException
 * @throws IOException
 */
public static void redirectOverSSL(HttpServletRequest request, HttpServletResponse response, int sslPort)
        throws ServletException, IOException {

    StringBuffer url = request.getRequestURL();

    // Make sure we're on https
    if (url.charAt(4) != 's')
        url.insert(4, 's');

    // If there is a ssl port, make sure we're on it,
    // otherwise assume we're already on the right port
    if (sslPort > 0) {
        int portStart = url.indexOf(":", 8) + 1;
        int portEnd = url.indexOf("/", 8);

        if (portEnd == -1) // If their isn't a trailing slash, then the end is the last char
            portEnd = url.length() - 1;

        if (portStart > 0 && portStart < portEnd) { // If we detected a : before the trailing slash or end of url, delete the port
            url.delete(portStart, portEnd);
        } else {
            url.insert(portEnd, ':'); // If the url didn't have a port, add in the :
            portStart = portEnd;
        }

        url.insert(portStart, sslPort); // Insert the right port where it should be
    }

    LogFactory.getLog(ServletUtils.class).debug("redirectOverSSL sending 301: " + url.toString());
    sendPermanentRedirect(response, url.toString());
}

From source file:org.j2free.util.ServletUtils.java

/**
 * Redirects the user to the provided url over SSL
 *
 * @param request a HttpServletRequest//from w  w w .  ja v a 2s  . c  o m
 * @param response a HttpServletResponse
 * @param urlStr 
 * @param sslPort the port SSL requests should be forwarded to
 * @throws ServletException
 * @throws IOException
 */
public static void redirectOverSSL(HttpServletRequest request, HttpServletResponse response, String urlStr,
        int sslPort) throws ServletException, IOException {

    StringBuffer url = new StringBuffer(urlStr);

    // Make sure we're on https
    if (url.charAt(4) != 's')
        url.insert(4, 's');

    // If there is a ssl port, make sure we're on it,
    // otherwise assume we're already on the right port
    if (sslPort > 0) {
        int portStart = url.indexOf(":", 8) + 1;
        int portEnd = url.indexOf("/", 8);

        if (portEnd == -1) // If their isn't a trailing slash, then the end is the last char
            portEnd = url.length() - 1;

        if (portStart > 0 && portStart < portEnd) { // If we detected a : before the trailing slash or end of url, delete the port
            url.delete(portStart, portEnd);
        } else {
            url.insert(portEnd, ':'); // If the url didn't have a port, add in the :
            portStart = portEnd;
        }

        url.insert(portStart, sslPort); // Insert the right port where it should be
    }

    LogFactory.getLog(ServletUtils.class).debug("redirectOverSSL sending 301: " + url.toString());
    sendPermanentRedirect(response, url.toString());
}

From source file:org.j2free.util.ServletUtils.java

/**
 * Redirects the user to the current url over HTTP
 *
 * @param request a HttpServletRequest//from   w w  w  .  ja  v  a  2s  .  com
 * @param response a HttpServletResponse
 * @param nonSslPort the port Non-SSL requests should be forwarded to
 * @throws ServletException
 * @throws IOException
 */
public static void redirectOverNonSSL(HttpServletRequest request, HttpServletResponse response, int nonSslPort)
        throws ServletException, IOException {

    StringBuffer url = request.getRequestURL();

    // Make sure we're on http
    if (url.charAt(4) == 's')
        url.deleteCharAt(4);

    // If there is a non-ssl port, make sure we're on it,
    // otherwise assume we're already on the right port
    if (nonSslPort > 0) {
        int portStart = url.indexOf(":", 8) + 1;
        int portEnd = url.indexOf("/", 8);

        if (portEnd == -1) // If their isn't a trailing slash, then the end is the last char
            portEnd = url.length() - 1;

        if (portStart > 0 && portStart < portEnd) { // If we detected a : before the trailing slash or end of url, delete the port
            url.delete(portStart, portEnd);
        } else {
            url.insert(portEnd, ':'); // If the url didn't have a port, add in the :
            portStart = portEnd;
        }

        url.insert(portStart, nonSslPort); // Insert the right port where it should be
    }

    LogFactory.getLog(ServletUtils.class).debug("redirectOverSSL sending 301: " + url.toString());
    sendPermanentRedirect(response, url.toString());
}

From source file:org.j2free.util.ServletUtils.java

/**
 * Redirects the user to the current url over HTTPS
 *
 * @param request a HttpServletRequest/*from  ww w  .  ja  va 2 s. c  o m*/
 * @param response a HttpServletResponse
 * @param urlStr 
 * @param nonSslPort the port Non-SSL requests should be forwarded to
 * @throws ServletException
 * @throws IOException
 */
public static void redirectOverNonSSL(HttpServletRequest request, HttpServletResponse response, String urlStr,
        int nonSslPort) throws ServletException, IOException {

    StringBuffer url = new StringBuffer(urlStr);

    // Make sure we're on http
    if (url.charAt(4) == 's')
        url.deleteCharAt(4);

    // If there is a non-ssl port, make sure we're on it,
    // otherwise assume we're already on the right port
    if (nonSslPort > 0) {
        int portStart = url.indexOf(":", 8) + 1;
        int portEnd = url.indexOf("/", 8);

        if (portEnd == -1) // If their isn't a trailing slash, then the end is the last char
            portEnd = url.length() - 1;

        if (portStart > 0 && portStart < portEnd) { // If we detected a : before the trailing slash or end of url, delete the port
            url.delete(portStart, portEnd);
        } else {
            url.insert(portEnd, ':'); // If the url didn't have a port, add in the :
            portStart = portEnd;
        }

        url.insert(portStart, nonSslPort); // Insert the right port where it should be
    }

    LogFactory.getLog(ServletUtils.class).debug("redirectOverSSL sending 301: " + url.toString());
    sendPermanentRedirect(response, url.toString());
}

From source file:com.appeligo.search.actions.ProgramAction.java

public StringBuffer getRequestURL() {
    StringBuffer requestURL = getServletRequest().getRequestURL();
    int slashes = requestURL.indexOf("//");
    int nextSlash = requestURL.indexOf("/", slashes + 2);
    requestURL.delete(nextSlash, requestURL.length());
    requestURL.append(programInfo.getWebPath());
    return requestURL;
}

From source file:com.glaf.report.web.springmvc.MxReportTaskController.java

@RequestMapping("/chooseReport")
public ModelAndView chooseReport(HttpServletRequest request, ModelMap modelMap) {
    RequestUtils.setRequestParameterToAttribute(request);
    request.removeAttribute("canSubmit");
    Map<String, Object> params = RequestUtils.getParameterMap(request);
    String rowId = ParamUtils.getString(params, "rowId");
    ReportQuery query = new ReportQuery();
    List<Report> list = reportService.list(query);
    request.setAttribute("unselecteds", list);
    ReportTask reportTask = null;/*from w  w w  .  ja  v a 2  s. c  om*/
    if (StringUtils.isNotEmpty(rowId)) {
        reportTask = reportTaskService.getReportTask(rowId);
        request.setAttribute("reportTask", reportTask);
        if (StringUtils.isNotEmpty(reportTask.getReportIds())) {
            StringBuffer sb01 = new StringBuffer();
            StringBuffer sb02 = new StringBuffer();
            List<String> selecteds = new java.util.ArrayList<String>();
            for (Report r : list) {
                if (StringUtils.contains(reportTask.getReportIds(), r.getId())) {
                    selecteds.add(r.getId());
                    sb01.append(r.getId()).append(",");
                    sb02.append(r.getSubject()).append(",");
                }
            }
            if (sb01.toString().endsWith(",")) {
                sb01.delete(sb01.length() - 1, sb01.length());
            }
            if (sb02.toString().endsWith(",")) {
                sb02.delete(sb02.length() - 1, sb02.length());
            }
            request.setAttribute("selecteds", selecteds);
            request.setAttribute("reportIds", sb01.toString());

            request.setAttribute("reportNames", sb02.toString());
        }
    }

    String x_view = ViewProperties.getString("reportTask.chooseReport");
    if (StringUtils.isNotEmpty(x_view)) {
        return new ModelAndView(x_view, modelMap);
    }

    return new ModelAndView("/bi/reportTask/chooseReport", modelMap);
}

From source file:com.chinamobile.bcbsp.graph.GraphDataMananger.java

@Override
public void saveAllVertices(RecordWriter output) throws IOException, InterruptedException {
    try {//from w  w w .ja  va 2  s  . c  om
        //Edge edge = edgeClass.newInstance();
        for (int i = (MetaDataOfGraph.BCBSP_DISKGRAPH_HASHNUMBER - 1); i >= 0; i--) {
            int counter = MetaDataOfGraph.VERTEX_NUM_PERBUCKET[i];
            if (counter == 0) {
                continue;
            }
            this.prepareBucket(i, this.lastSuperstepCounter + 1);
            for (int j = 0; j < counter; j++) {
                Vertex v = vertexClass.newInstance();
                fillVertex(v);
                this.edgelist = (ArrayList<Edge>) v.getAllEdges();
                StringBuffer outEdges = new StringBuffer();
                for (Edge edge : this.edgelist) {
                    outEdges.append(edge.getVertexID() + Constants.SPLIT_FLAG + edge.getEdgeValue()
                            + Constants.SPACE_SPLIT_FLAG);
                }
                if (outEdges.length() > 0) {
                    int k = outEdges.length();
                    outEdges.delete(k - 1, k - 1);
                }
                //LOG.info("Feng test! outgoingEdges "+outEdges.toString());
                output.write(new Text(v.getVertexID() + Constants.SPLIT_FLAG + v.getVertexValue()),
                        new Text(outEdges.toString()));
                this.vManager.processVertexSave(v);
            }
            this.finishPreparedBucket();
        }
    } catch (InstantiationException e) {
        throw new RuntimeException("[Graph Data Manager] saveAllVertices", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("[Graph Data Manager] saveAllVertices", e);
    }
}