java.lang.StringBuffer.toString()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(244)

本文整理了Java中java.lang.StringBuffer.toString()方法的一些代码示例,展示了StringBuffer.toString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringBuffer.toString()方法的具体详情如下:
包路径:java.lang.StringBuffer
类名称:StringBuffer
方法名:toString

StringBuffer.toString介绍

[英]Converts to a string representing the data in this string buffer. A new String object is allocated and initialized to contain the character sequence currently represented by this string buffer. This String is then returned. Subsequent changes to the string buffer do not affect the contents of the String.

Implementation advice: This method can be coded so as to create a new String object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the string buffer. Any subsequent operation that alters the content or capacity of the string buffer must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation when it is implemented using a string buffer.
[中]转换为表示此字符串缓冲区中数据的字符串。分配并初始化一个新的String对象,以包含此字符串缓冲区当前表示的字符序列。然后返回此String。对字符串缓冲区的后续更改不会影响String的内容。
实现建议:可以对该方法进行编码,以便创建新的String对象,而无需分配新内存来保存字符序列的副本。相反,字符串可以共享字符串缓冲区使用的内存。任何改变字符串缓冲区内容或容量的后续操作都必须在此时复制内部缓冲区。当使用字符串缓冲区实现字符串连接操作时,此策略对于减少字符串连接操作分配的内存量是有效的。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private static URI initUri(HttpServletRequest request) throws URISyntaxException {
  Assert.notNull(request, "'request' must not be null");
  StringBuffer url = request.getRequestURL();
  String query = request.getQueryString();
  if (StringUtils.hasText(query)) {
    url.append('?').append(query);
  }
  return new URI(url.toString());
}

代码示例来源:origin: apache/incubator-dubbo

public static String generateGetMethodName(String fieldName) {
  return new StringBuffer(16)
      .append("get")
      .append(Character.toUpperCase(fieldName.charAt(0)))
      .append(fieldName.substring(1))
      .toString();
}

代码示例来源:origin: apache/incubator-dubbo

public static String generateGetMethodName(String fieldName) {
  return new StringBuffer(16)
      .append("get")
      .append(Character.toUpperCase(fieldName.charAt(0)))
      .append(fieldName.substring(1))
      .toString();
}

代码示例来源:origin: apache/incubator-dubbo

private String readLenString(int len)
    throws IOException {
  _isLastChunk = true;
  _chunkLength = len;
  _sbuf.setLength(0);
  int ch;
  while ((ch = parseChar()) >= 0)
    _sbuf.append((char) ch);
  return _sbuf.toString();
}

代码示例来源:origin: apache/incubator-dubbo

private String readLenString()
    throws IOException {
  int len = readInt();
  _isLastChunk = true;
  _chunkLength = len;
  _sbuf.setLength(0);
  int ch;
  while ((ch = parseChar()) >= 0)
    _sbuf.append((char) ch);
  return _sbuf.toString();
}

代码示例来源:origin: spring-projects/spring-framework

private static String format(String[] names) {
  StringBuffer sb = new StringBuffer();
  sb.append("(");
  for (int i = 0; i < names.length; i++) {
    sb.append(names[i]);
    if ((i + 1) < names.length) {
      sb.append(",");
    }
  }
  sb.append(")");
  return sb.toString();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
    String s = (String) args[0];
    return new StringBuffer(s).reverse().toString();
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestURLNewStringBuffer() throws Exception {
  this.request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
  this.request.setRequestURI("/mvc-showcase");
  HttpServletRequest actual = filterAndGetWrappedRequest();
  actual.getRequestURL().append("?key=value");
  assertEquals("http://localhost/prefix/mvc-showcase", actual.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getRequestURLWithDefaults() {
  StringBuffer requestURL = request.getRequestURL();
  assertEquals("http://localhost", requestURL.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void buildRequestUrl() {
  String uri = requestBuilder.buildRequest(servletContext).getRequestURL().toString();
  assertThat(uri, equalTo("http://example.com/test/this/here"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void contextPathPreserveEncoding() throws Exception {
  this.request.setContextPath("/app%20");
  this.request.setRequestURI("/app%20/path/");
  HttpServletRequest actual = filterAndGetWrappedRequest();
  assertEquals("/app%20", actual.getContextPath());
  assertEquals("/app%20/path/", actual.getRequestURI());
  assertEquals("http://localhost/app%20/path/", actual.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestUriPreserveSemicolonContent() throws Exception {
  this.request.setContextPath("");
  this.request.setRequestURI("/path;a=b/with/semicolon");
  HttpServletRequest actual = filterAndGetWrappedRequest();
  assertEquals("", actual.getContextPath());
  assertEquals("/path;a=b/with/semicolon", actual.getRequestURI());
  assertEquals("http://localhost/path;a=b/with/semicolon", actual.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestUriPreserveEncoding() throws Exception {
  this.request.setContextPath("/app");
  this.request.setRequestURI("/app/path%20with%20spaces/");
  HttpServletRequest actual = filterAndGetWrappedRequest();
  assertEquals("/app", actual.getContextPath());
  assertEquals("/app/path%20with%20spaces/", actual.getRequestURI());
  assertEquals("http://localhost/app/path%20with%20spaces/", actual.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestUriWithForwardedPrefixTrailingSlash() throws Exception {
  this.request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
  this.request.setRequestURI("/mvc-showcase");
  HttpServletRequest actual = filterAndGetWrappedRequest();
  assertEquals("http://localhost/prefix/mvc-showcase", actual.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestUriWithForwardedPrefix() throws Exception {
  this.request.addHeader(X_FORWARDED_PREFIX, "/prefix");
  this.request.setRequestURI("/mvc-showcase");
  HttpServletRequest actual = filterAndGetWrappedRequest();
  assertEquals("http://localhost/prefix/mvc-showcase", actual.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getRequestURLWithNullRequestUri() {
  request.setRequestURI(null);
  StringBuffer requestURL = request.getRequestURL();
  assertEquals("http://localhost", requestURL.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getRequestURLWithNegativePort() {
  request.setServerPort(-99);
  StringBuffer requestURL = request.getRequestURL();
  assertEquals("http://localhost", requestURL.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getRequestURL() {
  request.setServerPort(8080);
  request.setRequestURI("/path");
  assertEquals("http://localhost:8080/path", request.getRequestURL().toString());
  request.setScheme("https");
  request.setServerName("example.com");
  request.setServerPort(8443);
  assertEquals("https://example.com:8443/path", request.getRequestURL().toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getRequestURLWithDefaultsAndHttps() {
  request.setScheme("https");
  request.setServerPort(443);
  StringBuffer requestURL = request.getRequestURL();
  assertEquals("https://localhost", requestURL.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void uri() {
  String uri = "https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)";
  this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, uri);
  MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
  assertEquals("https", request.getScheme());
  assertEquals("foo=bar", request.getQueryString());
  assertEquals("java.sun.com", request.getServerName());
  assertEquals(8080, request.getServerPort());
  assertEquals("/javase/6/docs/api/java/util/BitSet.html", request.getRequestURI());
  assertEquals("https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html",
      request.getRequestURL().toString());
}

相关文章