Java StringBuilder.insert Examples

Java StringBuilder.insert - 30 examples found. These are the top rated real world Java examples of StringBuilder.insert extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 private void wire(final String header, final byte[] b, final int pos, final int off) {
   final StringBuilder buffer = new StringBuilder();
   for (int i = 0; i < off; i++) {
     final int ch = b[pos + i];
     if (ch == 13) {
       buffer.append("[\\r]");
     } else if (ch == 10) {
       buffer.append("[\\n]\"");
       buffer.insert(0, "\"");
       buffer.insert(0, header);
       this.log.debug(this.id + " " + buffer.toString());
       buffer.setLength(0);
     } else if ((ch < 32) || (ch > 127)) {
       buffer.append("[0x");
       buffer.append(Integer.toHexString(ch));
       buffer.append("]");
     } else {
       buffer.append((char) ch);
     }
   }
   if (buffer.length() > 0) {
     buffer.append('\"');
     buffer.insert(0, '\"');
     buffer.insert(0, header);
     this.log.debug(this.id + " " + buffer.toString());
   }
 }
Example #2
0
 private void timesClause(StringBuilder sb, int offset, Map<String, Timestamp> eachTime)
     throws ParseException {
   Timestamp ts = null;
   List<String> times = bulkFilter.get(BulkResponseImpl.BULK_FILTER_START_CREATED_EPOCH);
   if (times != null) {
     ts = new Timestamp(DateUtils.parseDateUTC(times.get(0)).getTime());
     sb.insert(offset - 1, " AND a.createdTimestamp >= :startCreated");
     eachTime.put("startCreated", ts);
   }
   times = bulkFilter.get(BulkResponseImpl.BULK_FILTER_END_CREATED_EPOCH);
   if (times != null) {
     ts = new Timestamp(DateUtils.parseDateUTC(times.get(0)).getTime());
     sb.insert(offset - 1, " AND a.createdTimestamp <= :endCreated");
     eachTime.put("endCreated", ts);
   }
   times = bulkFilter.get(BulkResponseImpl.BULK_FILTER_START_NOMINAL_EPOCH);
   if (times != null) {
     ts = new Timestamp(DateUtils.parseDateUTC(times.get(0)).getTime());
     sb.insert(offset - 1, " AND a.nominalTimestamp >= :startNominal");
     eachTime.put("startNominal", ts);
   }
   times = bulkFilter.get(BulkResponseImpl.BULK_FILTER_END_NOMINAL_EPOCH);
   if (times != null) {
     ts = new Timestamp(DateUtils.parseDateUTC(times.get(0)).getTime());
     sb.insert(offset - 1, " AND a.nominalTimestamp <= :endNominal");
     eachTime.put("endNominal", ts);
   }
 }
  private void generateParens(
      ArrayList<String> list, int leftRem, int rightRem, StringBuilder sb, int index) {

    // number of left parentheses remaining cannot be less than 0
    // cannot possibly have a valid string having inserted more
    // right parentheses than left parentheses
    if (leftRem < 0 || rightRem < leftRem) return;

    if (leftRem == 0 && rightRem == 0) list.add(sb.toString());
    else {
      // add left parenthesis if any left
      if (leftRem > 0) {
        try {
          sb.setCharAt(index, '(');
        } catch (Exception e) {
          sb.insert(index, "(");
        }
        generateParens(list, leftRem - 1, rightRem, sb, index + 1);
      }

      // add right parenthesis if valid
      if (rightRem > leftRem) {
        try {
          sb.setCharAt(index, ')');
        } catch (Exception e) {
          sb.insert(index, ")");
        }
        generateParens(list, leftRem, rightRem - 1, sb, index + 1);
      }
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
    // If we have a payload/bucket/container that is not all lowercase, vhost-style URLs are not an
    // option and must be
    // automatically converted to their path-based equivalent.  This should only be possible for
    // AWS-S3 since it is
    // the only S3 implementation configured to allow uppercase payload/bucket/container names.
    //
    // http://code.google.com/p/jclouds/issues/detail?id=992
    String payloadAsString = payload.toString();

    if (isVhostStyle && payloadAsString.equals(payloadAsString.toLowerCase())) {
      request = bindAsHostPrefix.bindToRequest(request, payload);
      String host = request.getEndpoint().getHost();
      if (request.getEndpoint().getPort() != -1) {
        host += ":" + request.getEndpoint().getPort();
      }
      return (R) request.toBuilder().replaceHeader(HttpHeaders.HOST, host).build();
    } else {
      StringBuilder path = new StringBuilder(request.getEndpoint().getPath());
      if (servicePath.equals("/")) {
        if (path.toString().equals("/")) path.append(payloadAsString);
        else path.insert(0, "/" + payloadAsString);
      } else {
        int indexToInsert = 0;
        indexToInsert = path.indexOf(servicePath);
        indexToInsert = indexToInsert == -1 ? 0 : indexToInsert;
        indexToInsert += servicePath.length();
        path.insert(indexToInsert, "/" + payloadAsString);
      }
      return (R) request.toBuilder().replacePath(path.toString()).build();
    }
  }
Example #5
0
  /**
   * Creates a relative path to the given {@link ConfigurationSection} from the given relative
   * section.
   *
   * <p>You may use this method for any given {@link ConfigurationSection}, not only {@link
   * MemorySection}.
   *
   * @param section Section to create a path for.
   * @param key Name of the specified section.
   * @param relativeTo Section to create the path relative to.
   * @return Full path of the section from its root.
   */
  public static String createPath(
      ConfigurationSection section, String key, ConfigurationSection relativeTo) {
    StringBuilder builder = new StringBuilder();

    if (section != null) {
      for (ConfigurationSection parent = section;
          (parent != null) && (parent != relativeTo);
          parent = parent.getParent()) {
        if (builder.length() > 0) {
          builder.insert(0, section.getRoot().options().pathSeparator());
        }

        builder.insert(0, parent.getName());
      }
    }

    if ((key != null) && (key.length() > 0)) {
      if (builder.length() > 0) {
        builder.append(section.getRoot().options().pathSeparator());
      }

      builder.append(key);
    }

    return builder.toString();
  }
Example #6
0
  @Override
  public void saveConfig() {
    String str = this.getConfig().saveToString();
    StringBuilder sb = new StringBuilder(str);

    sb.insert(
        sb.indexOf("\nversion:") + 1,
        "\n# Configuration version used during upgrade. Do not change.\n");

    sb.insert(
        sb.indexOf("\nmute_minutes") + 1,
        "\n# The default number of minutes a player will remain muted.\n");

    sb.insert(
        sb.indexOf("\nprotect_boats") + 1,
        "\n# When true, protects boats from breakage unless deliberately broken.\n");

    final File cfg_file = new File(this.getDataFolder(), "config.yml");
    final String cfg_str = sb.toString();
    final Logger logger = this.getLogger();

    new BukkitRunnable() {
      @Override
      public void run() {
        cfg_file.delete();
        try (PrintWriter writer = new PrintWriter(cfg_file, "UTF-8")) {
          cfg_file.createNewFile();
          writer.write(cfg_str);
          writer.close();
        } catch (IOException ex) {
          logger.severe("Error saving configuration!");
        }
      }
    }.runTaskAsynchronously(this);
  }