okio.BufferedSource.readString()方法的使用及代码示例

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

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

BufferedSource.readString介绍

[英]Removes byteCount bytes from this, decodes them as charset, and returns the string.
[中]从中删除字节计数字节,将其解码为字符集,并返回字符串。

代码示例

代码示例来源:origin: square/okhttp

/**
 * Returns the response as a string.
 *
 * <p>If the response starts with a <a href="https://en.wikipedia.org/wiki/Byte_order_mark">Byte
 * Order Mark (BOM)</a>, it is consumed and used to determine the charset of the response bytes.
 *
 * <p>Otherwise if the response has a Content-Type header that specifies a charset, that is used
 * to determine the charset of the response bytes.
 *
 * <p>Otherwise the response bytes are decoded as UTF-8.
 *
 * <p>This method loads entire response body into memory. If the response body is very large this
 * may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a
 * possibility for your response.
 */
public final String string() throws IOException {
 try (BufferedSource source = source()) {
  Charset charset = Util.bomAwareCharset(source, charset());
  return source.readString(charset);
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

/**
 * Returns the response as a string.
 *
 * <p>If the response starts with a <a href="https://en.wikipedia.org/wiki/Byte_order_mark">Byte
 * Order Mark (BOM)</a>, it is consumed and used to determine the charset of the response bytes.
 *
 * <p>Otherwise if the response has a Content-Type header that specifies a charset, that is used
 * to determine the charset of the response bytes.
 *
 * <p>Otherwise the response bytes are decoded as UTF-8.
 *
 * <p>This method loads entire response body into memory. If the response body is very large this
 * may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a
 * possibility for your response.
 */
public final String string() throws IOException {
 try (BufferedSource source = source()) {
  Charset charset = Util.bomAwareCharset(source, charset());
  return source.readString(charset);
 }
}

代码示例来源:origin: square/okio

@Test public void readSpecificCharsetPartial() throws Exception {
 sink.write(ByteString.decodeHex("0000007600000259000002c80000006c000000e40000007300000259"
   + "000002cc000000720000006100000070000000740000025900000072"));
 sink.emit();
 assertEquals("vəˈläsə", source.readString(7 * 4, Charset.forName("utf-32")));
}

代码示例来源:origin: square/okio

@Test public void readSpecificCharset() throws Exception {
 sink.write(ByteString.decodeHex("0000007600000259000002c80000006c000000e40000007300000259"
   + "000002cc000000720000006100000070000000740000025900000072"));
 sink.emit();
 assertEquals("vəˈläsəˌraptər", source.readString(Charset.forName("utf-32")));
}

代码示例来源:origin: square/okio

@Test public void readStringTooShortThrows() throws IOException {
 sink.writeString("abc", US_ASCII);
 sink.emit();
 try {
  source.readString(4, US_ASCII);
  fail();
 } catch (EOFException expected) {
 }
 assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data.
}

代码示例来源:origin: ZhangQinhao/MONKOVEL

@Override
  public String convert(ResponseBody value) throws IOException {
    BufferedSource bufferedSource = Okio.buffer(value.source());
    String responseData = bufferedSource.readString(Charset.forName(encode));
    return responseData;
  }
};

代码示例来源:origin: opacapp/opacclient

private String readBody(Response response, String encoding) throws IOException {
  ResponseBody body = response.body();
  BufferedSource source = body.source();
  MediaType contentType = body.contentType();
  try {
    Charset charset = Util.bomAwareCharset(source,
        contentType != null ? contentType.charset(Charset.forName(encoding)) :
            Charset.forName(encoding));
    return source.readString(charset);
  } finally {
    Util.closeQuietly(source);
  }
}

代码示例来源:origin: com.github.ljun20160606/okhttp

/**
 * Returns the response as a string decoded with the charset of the Content-Type header. If that
 * header is either absent or lacks a charset, this will attempt to decode the response body in
 * accordance to <a href="https://en.wikipedia.org/wiki/Byte_order_mark">its BOM</a> or UTF-8.
 * Closes {@link ResponseBody} automatically.
 *
 * <p>This method loads entire response body into memory. If the response body is very large this
 * may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a
 * possibility for your response.
 */
public final String string() throws IOException {
 BufferedSource source = source();
 try {
  Charset charset = Util.bomAwareCharset(source, charset());
  return source.readString(charset);
 } finally {
  Util.closeQuietly(source);
 }
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Returns the response as a string decoded with the charset of the Content-Type header. If that
 * header is either absent or lacks a charset, this will attempt to decode the response body in
 * accordance to <a href="https://en.wikipedia.org/wiki/Byte_order_mark">its BOM</a> or UTF-8.
 * Closes {@link ResponseBody} automatically.
 *
 * <p>This method loads entire response body into memory. If the response body is very large this
 * may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a
 * possibility for your response.
 */
public final String string() throws IOException {
 BufferedSource source = source();
 try {
  Charset charset = Util.bomAwareCharset(source, charset());
  return source.readString(charset);
 } finally {
  Util.closeQuietly(source);
 }
}

代码示例来源:origin: kioko/android-liveData-viewModel

private void enqueueResponse(String fileName, Map<String, String> headers) throws IOException {
  InputStream inputStream = getClass().getClassLoader()
      .getResourceAsStream("api-response/" + fileName);
  BufferedSource source = Okio.buffer(Okio.source(inputStream));
  MockResponse mockResponse = new MockResponse();
  for (Map.Entry<String, String> header : headers.entrySet()) {
    mockResponse.addHeader(header.getKey(), header.getValue());
  }
  mockWebServer.enqueue(mockResponse
      .setBody(source.readString(StandardCharsets.UTF_8)));
}

代码示例来源:origin: anitaa1990/TrailersApp

private void enqueueResponse(String fileName, Map<String, String> headers) throws IOException {
  InputStream inputStream = ApiAbstract.class.getClassLoader().getResourceAsStream(String.format("api-response/%s", fileName));
  Source source = Okio.buffer(Okio.source(inputStream));
  MockResponse mockResponse = new MockResponse();
  for (Map.Entry<String, String> entry : headers.entrySet()) {
    mockResponse.addHeader(entry.getKey(), entry.getValue());
  }
  mockWebServer.enqueue(mockResponse.setBody(((BufferedSource) source).readString(StandardCharsets.UTF_8)));
}

相关文章

微信公众号

最新文章

更多