Example usage for java.lang StringBuffer deleteCharAt

List of usage examples for java.lang StringBuffer deleteCharAt

Introduction

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

Prototype

@Override
public synchronized StringBuffer deleteCharAt(int index) 

Source Link

Usage

From source file:org.latticesoft.util.common.StringUtil.java

/**
 * Formats a map into string//from  w ww  .  j  ava 2s.  co m
 * @param map the map to be formatted
 * @param separator the separator to separate the elements
 * @param enclosing to include the enclosing braces or not
 * @return the formatted string
 */
public static String formatMap(Map map, String separator, boolean enclosing) {
    StringBuffer sb = new StringBuffer();
    if (map == null || separator == null) {
        return null;
    }
    Iterator iter = map.keySet().iterator();
    if (enclosing) {
        sb.append("{");
    }
    while (iter.hasNext()) {
        Object key = iter.next();
        Object value = map.get(key);
        if (key != null) {
            sb.append(key).append("=");
            if (value != null) {
                sb.append(value);
            }
            sb.append(separator);
        }
    }
    int len = separator.length();
    if (sb.length() > len) {
        int startIndex = sb.length() - len;
        int endIndex = sb.length();
        String s = sb.substring(startIndex, endIndex);
        if (s.equals(separator)) {
            for (int i = 0; i < len; i++) {
                sb.deleteCharAt(sb.length() - 1);
            }
        }
    }
    if (enclosing) {
        sb.append("}");
    }
    return sb.toString();
}

From source file:org.latticesoft.util.common.FileUtil.java

private static void writeToZip(ZipOutputStream zos, File file, File baseDir) {
    BufferedInputStream bis = null;
    try {//from   w w  w.  j a  v a  2  s  . c  o  m
        if (file == null || zos == null || file.isDirectory()) {
            return;
        }
        bis = new BufferedInputStream(new FileInputStream(file));
        StringBuffer sb = new StringBuffer();
        String path = null;
        if (baseDir == null) {
            sb.append(file.getPath());
        } else {
            sb.append(FileUtil.getRelativePath(file, baseDir, false));
            sb.deleteCharAt(0);
        }
        path = sb.toString();

        ZipEntry target = new ZipEntry(path);
        if (log.isInfoEnabled()) {
            log.info("Zipping : " + path);
        }
        zos.putNextEntry(target);
        byte buf[] = new byte[1024];
        int count;
        while ((count = bis.read(buf, 0, 1024)) != -1) {
            zos.write(buf, 0, count);
        }
    } catch (Exception e) {
        if (log.isErrorEnabled()) {
            log.error(e);
        }
    } finally {
        try {
            bis.close();
        } catch (Exception e) {
        }
        try {
            zos.closeEntry();
        } catch (Exception e) {
        }
        bis = null;
    }
}

From source file:org.tdod.ether.taimpl.telnet.InputStreamGobbler.java

public void run() {
    try {// w  w w  .  java2s. c o  m
        // list of previous entered lines
        final List<String> lines = new ArrayList<String>();
        // position within lines
        int lineNumber = -1;
        // line buffer
        final StringBuffer inputBuffer = new StringBuffer();
        // position within line buffer
        int cursor = 0;
        do {
            int c = _shellIo.read();
            if (_log.isDebugEnabled()) {
                _log.debug("STDIN> " + c + " " + (char) c);
            }
            switch (c) {
            case BasicTerminalIO.DELETE:
                /*_log.debug("STDIN> DELETE");
                if (cursor < inputBuffer.length()) {
                   inputBuffer.deleteCharAt(cursor);
                   _shellIo.write(_deleteChar);
                }
                break;*/
            case BasicTerminalIO.BACKSPACE:
                _log.debug("STDIN> BACKSPACE");
                if (cursor > 0) {
                    inputBuffer.deleteCharAt(cursor - 1);
                    cursor--;
                    _shellIo.write((char) TerminalIO.BS);
                    // shellIo.moveLeft(1);
                    _shellIo.write(_deleteChar);
                }
                break;
            case BasicTerminalIO.LEFT:
                _log.debug("STDIN> LEFT");
                if (cursor > 0) {
                    cursor--;
                    _shellIo.write((char) TerminalIO.BS);
                }
                break;
            case BasicTerminalIO.RIGHT:
                _log.debug("STDIN> RIGHT");
                if (cursor < inputBuffer.length()) {
                    _shellIo.moveRight(1);
                    // m_IO.write(inputBuffer.charAt(cursor));
                    cursor++;
                }
                break;
            case BasicTerminalIO.UP:
                _log.debug("STDIN> UP");
                if (lines.size() > 0 && lineNumber >= 0) {
                    if (inputBuffer.length() > cursor) {
                        // for (int i = cursor; i < inputBuffer.length(); i++) {
                        // shellIo.write(deleteChar);
                        // }
                        _shellIo.write(deleteChars(inputBuffer.length() - cursor));
                    }
                    for (int i = 0; i < cursor; i++) {
                        _shellIo.write((char) TerminalIO.BS);
                        _shellIo.write(_deleteChar);
                    }
                    final String line = (String) lines.get(lineNumber);
                    if (lineNumber > 0) {
                        lineNumber--;
                    }
                    _shellIo.write(line);
                    inputBuffer.setLength(0);
                    inputBuffer.append(line);
                    cursor = line.length();
                }
                break;
            case BasicTerminalIO.DOWN:
                _log.debug("STDIN> DOWN");
                if (lineNumber >= 0 && lineNumber < lines.size()) {
                    if (inputBuffer.length() > cursor) {
                        _shellIo.write(deleteChars(inputBuffer.length() - cursor));
                    }
                    for (int i = 0; i < cursor; i++) {
                        _shellIo.write((char) TerminalIO.BS);
                        _shellIo.write(_deleteChar);
                    }
                    final String line = (String) lines.get(lineNumber);
                    if (lineNumber + 1 < lines.size()) {
                        lineNumber++;
                    }
                    _shellIo.write(line);
                    inputBuffer.setLength(0);
                    inputBuffer.append(line);
                    cursor = line.length();
                }
                break;
            case BasicTerminalIO.ENTER:
                _log.debug("STDIN> ENTER");
                _shellIo.write(BasicTerminalIO.CRLF);
                cursor = 0;
                lines.add(inputBuffer.toString());
                PlayerInputManager.postPlayerInputEvent(PlayerInputEventId.General, _connection.getId(),
                        inputBuffer.toString());
                lineNumber = lines.size() - 1;
                inputBuffer.setLength(0);
                break;
            default:
                if (c < 256) {
                    _log.debug("STDIN> no special char");
                    if (!_hideInput) {
                        _shellIo.write(_insertChar);
                        _shellIo.write((char) c);
                    }
                    inputBuffer.insert(cursor, (char) c);
                    cursor++;
                } else {
                    _log.debug("STDIN> unknown char");
                }
            }
        } while (_connection.isActive());
    } catch (SocketException e) {
        _log.warn("SocketException -- connection closed");
        PlayerConnectedManager.postPlayerConnectedEvent(PlayerConnectedEventId.Disconnected, _taShell);
    } catch (IOException e) {
        _log.warn("IOException -- connection closed");
        PlayerConnectedManager.postPlayerConnectedEvent(PlayerConnectedEventId.Disconnected, _taShell);
    }
}

From source file:org.apache.lucene.analysis.de.GermanStemmer.java

/**
  * suffix stripping (stemming) on the current term. The stripping is reduced
  * to the seven "base" suffixes "e", "s", "n", "t", "em", "er" and * "nd",
  * from which all regular suffixes are build of. The simplification causes
  * some overstemming, and way more irregular stems, but still provides
  * unique. discriminators in the most of those cases. The algorithm is
  * context free, except of the length restrictions.
  *//*from  ww w . j  a v  a  2 s.  co m*/
 private void strip(StringBuffer buffer) {
     boolean doMore = true;
     while (doMore && buffer.length() > 3) {
         if ((buffer.length() + substCount > 5)
                 && buffer.substring(buffer.length() - 2, buffer.length()).equals("nd")) {
             buffer.delete(buffer.length() - 2, buffer.length());
         } else if ((buffer.length() + substCount > 4)
                 && buffer.substring(buffer.length() - 2, buffer.length()).equals("em")) {
             buffer.delete(buffer.length() - 2, buffer.length());
         } else if ((buffer.length() + substCount > 4)
                 && buffer.substring(buffer.length() - 2, buffer.length()).equals("er")) {
             buffer.delete(buffer.length() - 2, buffer.length());
         } else if (buffer.charAt(buffer.length() - 1) == 'e') {
             buffer.deleteCharAt(buffer.length() - 1);
         } else if (buffer.charAt(buffer.length() - 1) == 's') {
             buffer.deleteCharAt(buffer.length() - 1);
         } else if (buffer.charAt(buffer.length() - 1) == 'n') {
             buffer.deleteCharAt(buffer.length() - 1);
         }
         // "t" occurs only as suffix of verbs.
         else if (buffer.charAt(buffer.length() - 1) == 't') {
             buffer.deleteCharAt(buffer.length() - 1);
         } else {
             doMore = false;
         }
     }
 }

From source file:se.nrm.bio.mediaserver.rs.NewMediaResource.java

private StringBuffer buildKeyValueString(MultivaluedMap<String, String> param) {
    StringBuffer sb = new StringBuffer();
    final String DELIMITER = ":";
    final String SPLITTER = "&";

    if (param != null) {
        Iterator it = param.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = param.getFirst(key);
            sb.append(key).append(DELIMITER).append(value).append(SPLITTER);
        }/*from   w ww. j  av a  2 s  . com*/
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb;
}

From source file:com.skilrock.lms.coreEngine.accMgmt.common.PwtTicketDirectPlayerHelper.java

public List<GameTicketFormatBean> getGameTicketFormatList(List<ActiveGameBean> activeGameList) {

    StringBuffer gameIdList = new StringBuffer();
    List<GameTicketFormatBean> gameFormatList = null;
    GameTicketFormatBean gameFormatBean = null;

    Connection connection = null;
    Statement stmt = null;// w  ww. jav a2 s.c o  m
    ResultSet resultSet = null;
    String query = null;

    for (int i = 0; i < activeGameList.size(); i++) {
        gameIdList.append(activeGameList.get(i).getGameId());
        gameIdList.append(",");
    }

    if (gameIdList.length() > 0) {
        gameIdList.deleteCharAt(gameIdList.length() - 1);

        gameFormatList = new ArrayList<GameTicketFormatBean>();

        try {

            connection = DBConnect.getConnection();

            query = QueryManager.getGameFormatInformation();
            query = query + gameIdList.toString() + " )";

            stmt = connection.createStatement();
            resultSet = stmt.executeQuery(query);

            while (resultSet.next()) {

                gameFormatBean = new GameTicketFormatBean();

                gameFormatBean.setGameId(resultSet.getInt(TableConstants.SGTNF_GAME_ID));
                gameFormatBean.setGameNbrDigits(resultSet.getInt(TableConstants.SGTNF_GAME_NBR_DIGITS));
                gameFormatBean.setPackDigits(resultSet.getInt(TableConstants.SGTNF_PACK_DIGITS));
                gameFormatBean.setBookDigits(resultSet.getInt(TableConstants.SGTNF_BOOK_DIGITS));

                gameFormatList.add(gameFormatBean);
            }

        } catch (SQLException e) {
            logger.error("Exception: " + e);
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                logger.error("Exception: " + e);
                e.printStackTrace();
            }
        }

    }
    return gameFormatList;
}

From source file:eionet.meta.imp.VocabularyImportBaseHandler.java

/**
 * If a newly created concept does not have a label field, then remove it from list and add it ${this.notSeenConceptsYet}. This
 * is necessary not to receive a null constraint fail exception when updating table. This error won't be received from CSV
 * import, since it has more strict controls. This method should be called before processUnseenConceptsForRelatedElements.
 *
 * @return log message// w  w  w. ja  v  a 2  s  .c o  m
 */
protected List<String> processNewlyCreatedConceptsForNullCheck() {
    int numberOfRemovedConcepts = 0;
    StringBuffer messageBuffer = new StringBuffer("--> Identifiers of not created concepts are as following: ");
    Iterator<VocabularyConcept> conceptIterator = this.toBeUpdatedConcepts.iterator();
    while (conceptIterator.hasNext()) {
        VocabularyConcept concept = conceptIterator.next();
        if (concept.getId() < 0 && StringUtils.isEmpty(concept.getLabel())) {
            conceptIterator.remove();
            messageBuffer.append(concept.getIdentifier()).append(", ");
            numberOfRemovedConcepts++;
            this.notSeenConceptsYet.add(concept);
        }
    }
    if (numberOfRemovedConcepts > 0) {
        // delete last two characters from message buffer because they are no necessary.
        messageBuffer.deleteCharAt(messageBuffer.length() - 1);
        messageBuffer.deleteCharAt(messageBuffer.length() - 1);
        List<String> messageBufferList = new ArrayList<String>();
        messageBufferList
                .add(numberOfRemovedConcepts + " concepts not created because of empty label specification.");
        messageBufferList.add(messageBuffer.toString());
        return messageBufferList;
    }

    return null;
}

From source file:org.talend.core.hadoop.version.custom.HadoopCustomVersionDefineDialog.java

/**
 * DOC ycbai Comment method "getLibListStr".
 * //from  ww w  . j  a va 2 s .com
 * <p>
 * Get libraries which <code>definitionType</code> need.
 * </p>
 * 
 * @param definitionType
 * @return Libraries string(separated by ';') which needed by the <code>definitionType</code>.
 */
public String getLibListStr(ECustomVersionGroup definitionType) {
    StringBuffer libStr = new StringBuffer();
    Set<String> libList = getLibList(definitionType);
    if (libList != null) {
        for (String lib : libList) {
            if (StringUtils.isNotEmpty(lib)) {
                libStr.append(lib).append(";"); //$NON-NLS-1$
            }
        }
        if (libStr.length() > 0) {
            libStr.deleteCharAt(libStr.length() - 1);
        }
    }

    return libStr.toString();
}

From source file:hr.fer.zemris.vhdllab.service.extractor.automaton.AutomatonMetadataExtractor.java

private StringBuffer addEntity(StringBuffer buffer, AUTPodatci podatci) {
    buffer.append("ENTITY ").append(podatci.ime).append(" IS PORT(\n")
            .append("\tclock: IN std_logic;\n\treset: IN std_logic;");
    String[] redovi = podatci.interfac.split("\n");
    for (int i = 0; i < redovi.length; i++) {
        buffer.append("\n\t");
        String[] rijeci = redovi[i].split(" ");
        buffer.append(rijeci[0]).append(": ").append(rijeci[1].toUpperCase()).append(" ").append(rijeci[2]);
        if (rijeci[2].toUpperCase().equals("STD_LOGIC_VECTOR")) {
            if (Integer.parseInt(rijeci[3]) < Integer.parseInt(rijeci[4]))
                buffer.append("(").append(Integer.parseInt(rijeci[3])).append(" TO ")
                        .append(Integer.parseInt(rijeci[4])).append(")");
            else/*from w w  w .j ava2 s . c  o  m*/
                buffer.append("(").append(Integer.parseInt(rijeci[3])).append(" DOWNTO ")
                        .append(Integer.parseInt(rijeci[4])).append(")");
        }
        buffer.append(";");
    }
    buffer.deleteCharAt(buffer.length() - 1);
    buffer.append(");\nEND ").append(podatci.ime).append(";\n");
    return buffer;
}

From source file:org.jivesoftware.community.util.StringUtils.java

public static String makeURLSafe(String str, char replacement) {
    if (str == null || !isASCIIURLSafe(String.valueOf(replacement)))
        return null;
    String trimmed = str.trim();//from  www  .  jav  a  2 s. co  m
    boolean ascii = str.trim()
            .replaceAll((new StringBuilder()).append("([\\p{Punct}&&[^\\").append(replacement).append("]]+)")
                    .toString(), "")
            .replaceAll(
                    (new StringBuilder()).append("([^A-Za-z\\").append(replacement).append("]+)").toString(),
                    String.valueOf(replacement))
            .toLowerCase().replaceAll((new StringBuilder()).append("").append(replacement).toString(), "")
            .replaceAll(" ", "").length() > 0;
    if (ascii) {
        trimmed = trimmed
                .replaceAll((new StringBuilder()).append("([\\p{Punct}&&[^\\").append(replacement)
                        .append("]]+)").toString(), "")
                .replaceAll((new StringBuilder()).append("([^0-9A-Za-z\\").append(replacement).append("]+)")
                        .toString(), String.valueOf(replacement))
                .toLowerCase();
        if (trimmed.length() > 1
                && (replacement == trimmed.charAt(0) || replacement == trimmed.charAt(trimmed.length() - 1))) {
            StringBuffer buf = new StringBuffer(trimmed);
            if (replacement == buf.charAt(0))
                buf.deleteCharAt(0);
            if (replacement == buf.charAt(buf.length() - 1))
                buf.deleteCharAt(buf.length() - 1);
            trimmed = buf.toString();
        }
    } else {
        boolean hasLetters = str.trim()
                .replaceAll((new StringBuilder()).append("([\\p{Punct}&&[^\\").append(replacement)
                        .append("]]+)").toString(), "")
                .replaceAll((new StringBuilder()).append("([^\\p{L}\\p{Nd}\\").append(replacement).append("]+)")
                        .toString(), String.valueOf(replacement))
                .replaceAll((new StringBuilder()).append("").append(replacement).toString(), "")
                .replaceAll(" ", "").length() > 0;
        if (hasLetters) {
            trimmed = str.trim();
            boolean cantLowercase = trimmed
                    .replaceAll((new StringBuilder()).append("([\\p{Punct}&&[^\\").append(replacement)
                            .append("]]+)").toString(), "")
                    .replaceAll((new StringBuilder()).append("([^\\p{Lo}\\").append(replacement).append("]+)")
                            .toString(), String.valueOf(replacement))
                    .replaceAll((new StringBuilder()).append("").append(replacement).toString(), "")
                    .replaceAll(" ", "").length() > 0;
            trimmed = trimmed
                    .replaceAll((new StringBuilder()).append("([\\p{Punct}&&[^\\").append(replacement)
                            .append("]]+)").toString(), "")
                    .replaceAll((new StringBuilder()).append("([^\\p{L}\\p{Nd}\\").append(replacement)
                            .append("]+)").toString(), String.valueOf(replacement));
            if (!cantLowercase)
                trimmed = trimmed.toLowerCase();
        } else {
            trimmed = trimmed.toLowerCase();
        }
    }
    return "".equals(trimmed) || "-".equals(trimmed) ? String.valueOf(Math.abs(str.hashCode())) : trimmed;
}