Java StringBuilder.ensureCapacity Examples

Java StringBuilder.ensureCapacity - 13 examples found. These are the top rated real world Java examples of StringBuilder.ensureCapacity extracted from open source projects. You can rate examples to help us improve the quality of examples.
  /**
   * Read lines of a text file into a String, optionally ignoring comments.
   *
   * @param listFile the File to read from.
   * @param withcomments If <code>false</code> ignore lines starting with '#'.
   * @return String representation of the file content.
   */
  public static String getListString(final File listFile, final boolean withcomments) {
    final StringBuilder temp = new StringBuilder(300);

    BufferedReader br = null;
    try {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(listFile)));
      temp.ensureCapacity((int) listFile.length());

      // Read the List
      String line = "";
      while ((line = br.readLine()) != null) {
        if (line.isEmpty()) {
          continue;
        }
        if (line.charAt(0) != '#' || withcomments) {
          // temp += line + serverCore.CRLF_STRING;
          temp.append(line).append(CR).append(LF);
        }
      }
      br.close();
    } catch (final IOException e) {
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (final Exception e) {
        }
      }
    }

    return new String(temp);
  }
Example #2
0
  /**
   * Escape编码(Unicode)
   *
   * @param content
   * @return 编码后的字符串
   */
  public static String escape(String content) {
    if (StrUtil.isBlank(content)) {
      return content;
    }

    int i;
    char j;
    StringBuilder tmp = new StringBuilder();
    tmp.ensureCapacity(content.length() * 6);

    for (i = 0; i < content.length(); i++) {

      j = content.charAt(i);

      if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
        tmp.append(j);
      else if (j < 256) {
        tmp.append("%");
        if (j < 16) tmp.append("0");
        tmp.append(Integer.toString(j, 16));
      } else {
        tmp.append("%u");
        tmp.append(Integer.toString(j, 16));
      }
    }
    return tmp.toString();
  }
  private void sendResults(
      HashMap<String, List<String>> results,
      String reportEmail,
      String subject,
      String logPath,
      String fileName) {
    StringBuilder message = new StringBuilder(1024);
    message.ensureCapacity(256);

    List<String> messages = ((List<String>) results.get("errors"));
    if ((messages != null) && (0 < messages.size())) {
      message.append("\nError Found:\n\n");
      Logger.info(ContentImporterThread.class, "\nError Found:\n");

      for (String tempMsg : messages) {
        message.append(tempMsg + "\n");
        Logger.info(ContentImporterThread.class, tempMsg);
      }
    }

    messages = ((List<String>) results.get("warnings"));
    if ((messages != null) && (0 < messages.size())) {
      message.append("\nWarnings Found:\n\n");
      Logger.info(ContentImporterThread.class, "\nWarnings Found:\n");

      for (String tempMsg : messages) {
        message.append(tempMsg + "\n");
        Logger.info(ContentImporterThread.class, tempMsg);
      }
    }

    messages = ((List<String>) results.get("results"));
    if ((messages != null) && (0 < messages.size())) {
      message.append("\nResults:\n\n");
      Logger.info(ContentImporterThread.class, "\nResults:\n");

      for (String tempMsg : messages) {
        message.append(tempMsg + "\n");
        Logger.info(ContentImporterThread.class, tempMsg);
      }
    }

    Company company = PublicCompanyFactory.getDefaultCompany();

    contentImporterLogger(logPath, fileName, message.toString());
    if (UtilMethods.isSet(reportEmail)) {
      Mailer m = new Mailer();
      m.setToEmail(reportEmail);
      m.setFromEmail(company.getEmailAddress());
      m.setCc(null);
      m.setBcc(null);
      m.setSubject(subject);
      m.setTextBody(message.toString());

      if (!m.sendMessage()) {
        Logger.info(ContentImporterThread.class, "Email couldn't be sent.");
      }
    }
  }
Example #4
0
  public void display(String html, String charset) {

    if (html != null) {
      m_content.ensureCapacity(html.length());
    }

    m_content.setLength(0);
    m_content.append(html);
  }
  public static void main(String args[]) {
    StringBuilder sb = new StringBuilder("푸른 산빛을");
    sb.ensureCapacity(50);
    printStringBuilder(sb);

    sb.append(" 깨지고 적은 길을 걸어서 차마 떨치고 갔습니다.");
    printStringBuilder(sb);

    sb.insert(10, " 단풍나무 숲을 향하여 난");
    printStringBuilder(sb);

    sb.delete(10, 29);
    printStringBuilder(sb);
  }
  /**
   * Parses through an html daybreak file and extracts any found daybreak fragments
   *
   * @param daybreakFile
   * @return
   */
  public static List<DaybreakFragment> extractDaybreakFragments(DaybreakFile daybreakFile)
      throws IOException {
    Assert.isTrue(
        daybreakFile.getDaybreakDocType() != DaybreakDocType.PAGE_FILE,
        "This parser is not for page files");

    List<DaybreakFragment> daybreakFragments = new ArrayList<>();

    String fullText =
        FileUtils.readFileToString(daybreakFile.getFile(), "UTF-8").replaceAll("\\r?\\n", " ");

    Matcher rowMatcher = rowPattern.matcher(fullText);
    rowMatcher.find(); // Throw the first two rows away
    rowMatcher.find(); // They are just headers for the table
    while (rowMatcher.find()) { // Each row contains 1 bill
      String text =
          stripParts
              .matcher(rowMatcher.group(1)) // Match all non <br> and </td> tags
              .replaceAll("") // Remove them
              .replace("</td>", "\n") // convert </td> and <br> to newlines
              .replace("<br>", "\n")
              .replace("�", " ") // Replace all instances of � with space
          ;

      // Here we are going through each line and trimming excess whitespace
      String[] lines = text.split("\\n");
      String fragmentPrintNo = null;
      StringBuilder fragmentText = new StringBuilder();
      fragmentText.ensureCapacity(text.length());
      for (int i = 0; i < lines.length; i++) {
        if (i == 0) { // The first line should be the bill print number
          fragmentPrintNo = lines[i].trim();
        }
        fragmentText.append(lines[i].trim());
        fragmentText.append('\n');
      }

      // TODO: it is assumed that the daybreak only contains bills from the current session year
      // todo: perhaps there is another way of getting the session year?
      BillId fragmentBillId =
          new BillId(fragmentPrintNo, SessionYear.of(daybreakFile.getReportDate().getYear()));

      daybreakFragments.add(
          new DaybreakFragment(fragmentBillId, daybreakFile, fragmentText.toString()));
    }

    return daybreakFragments;
  }