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

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

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

BufferedSource.readAll介绍

[英]Removes all bytes from this and appends them to sink. Returns the total number of bytes written to sink which will be 0 if this is exhausted.
[中]从中删除所有字节并将其附加到接收器。返回写入接收器的总字节数,如果已耗尽,则该总字节数将为0。

代码示例

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

static ResponseBody buffer(final ResponseBody body) throws IOException {
 Buffer buffer = new Buffer();
 body.source().readAll(buffer);
 return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
}

代码示例来源:origin: com.squareup.retrofit2/retrofit

static ResponseBody buffer(final ResponseBody body) throws IOException {
 Buffer buffer = new Buffer();
 body.source().readAll(buffer);
 return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
}

代码示例来源:origin: openzipkin/brave

/** like {@link #get(Request)} except doesn't throw unsupported on not found */
Response call(Request request) throws IOException {
 try (Response response = client.newCall(request).execute()) {
  if (!HttpHeaders.hasBody(response)) return response;
  // buffer response so tests can read it. Otherwise the finally block will drop it
  ResponseBody toReturn;
  try (ResponseBody body = response.body()) {
   Buffer buffer = new Buffer();
   body.source().readAll(buffer);
   toReturn = ResponseBody.create(body.contentType(), body.contentLength(), buffer);
  }
  return response.newBuilder().body(toReturn).build();
 }
}

代码示例来源:origin: Yalantis/uCrop

if (outputStream != null) {
  sink = Okio.sink(outputStream);
  source.readAll(sink);
} else {
  throw new NullPointerException("OutputStream for given output Uri is null");

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

try (HashingSource hashingSource = HashingSource.sha256(Okio.source(file));
   BufferedSource source = Okio.buffer(hashingSource)) {
 source.readAll(Okio.blackhole());
 System.out.println("    sha256: " + hashingSource.hash().hex());

代码示例来源:origin: k9mail/k-9

private void exhaustStream(InputStream inputStream) throws IOException {
  Okio.buffer(Okio.source(inputStream)).readAll(Okio.blackhole());
}

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

@Test public void readAllExhausted() throws IOException {
 MockSink mockSink = new MockSink();
 assertEquals(0, source.readAll(mockSink));
 assertTrue(source.exhausted());
 mockSink.assertLog();
}

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

@Test public void readAll() throws IOException {
 source.getBuffer().writeUtf8("abc");
 sink.writeUtf8("def");
 sink.emit();
 Buffer sink = new Buffer();
 assertEquals(6, source.readAll(sink));
 assertEquals("abcdef", sink.readUtf8());
 assertTrue(source.exhausted());
}

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

/**
  * We don't want readAll to buffer an unbounded amount of data. Instead it
  * should buffer a segment, write it, and repeat.
  */
 @Test public void readAllReadsOneSegmentAtATime() throws IOException {
  Buffer write1 = new Buffer().writeUtf8(TestUtil.repeat('a', SEGMENT_SIZE));
  Buffer write2 = new Buffer().writeUtf8(TestUtil.repeat('b', SEGMENT_SIZE));
  Buffer write3 = new Buffer().writeUtf8(TestUtil.repeat('c', SEGMENT_SIZE));

  Buffer source = new Buffer().writeUtf8(""
    + TestUtil.repeat('a', SEGMENT_SIZE)
    + TestUtil.repeat('b', SEGMENT_SIZE)
    + TestUtil.repeat('c', SEGMENT_SIZE));

  MockSink mockSink = new MockSink();
  BufferedSource bufferedSource = Okio.buffer((Source) source);
  assertEquals(SEGMENT_SIZE * 3, bufferedSource.readAll(mockSink));
  mockSink.assertLog(
    "write(" + write1 + ", " + write1.size() + ")",
    "write(" + write2 + ", " + write2.size() + ")",
    "write(" + write3 + ", " + write3.size() + ")");
 }
}

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

@Test public void peekSegmentThenInvalid() throws IOException {
 sink.writeUtf8("abc");
 sink.writeUtf8(repeat('d', 2 * SEGMENT_SIZE));
 sink.emit();
 assertEquals("abc", source.readUtf8(3));
 // Peek a little data and skip the rest of the upstream source
 BufferedSource peek = source.peek();
 assertEquals("ddd", peek.readUtf8(3));
 source.readAll(Okio.blackhole());
 // Skip the rest of the buffered data
 peek.skip(SEGMENT_SIZE - 3);
 try {
  peek.readByte();
  fail();
 } catch (IllegalStateException e) {
  assertEquals("Peek source is invalid because upstream source was used", e.getMessage());
 }
}

代码示例来源:origin: huangweicai/OkLibDemo

public static ResponseBody buffer(final ResponseBody body) throws IOException {
  Buffer buffer = new Buffer();
  body.source().readAll(buffer);
  return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
}

代码示例来源:origin: xing/xing-android-sdk

/** Buffers the response body, allowing to close the original source. */
static ResponseBody buffer(ResponseBody body) throws IOException {
  Buffer buffer = new Buffer();
  body.source().readAll(buffer);
  return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
}

代码示例来源:origin: com.sonymobile/lumbermill-core

public static void copy(InputStream is, OutputStream out) {
  try {
    buffer(source(is)).readAll(sink(out));
  } catch (IOException e) {
    throw new IllegalStateException(e);
  }
}

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

private void downloadJavadoc(Artifact artifact, File destination) throws IOException {
 try (BufferedSource source = mavenCentral.downloadJavadocJar(artifact);
    ZipInputStream zipIn = new ZipInputStream(source.inputStream())) {
  for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
   if (entry.isDirectory()) continue;
   File file = new File(destination + "/" + entry.getName());
   if (!file.getParentFile().mkdirs() && !file.getParentFile().isDirectory()) {
    throw new IOException("failed to mkdirs for " + file);
   }
   try (Sink out = Okio.sink(file)) {
    Okio.buffer(Okio.source(zipIn)).readAll(out);
   }
  }
 }
}

代码示例来源:origin: io.zipkin.brave/brave-instrumentation-http-tests

/** like {@link #get(Request)} except doesn't throw unsupported on not found */
Response call(Request request) throws IOException {
 try (Response response = client.newCall(request).execute()) {
  if (!HttpHeaders.hasBody(response)) return response;
  // buffer response so tests can read it. Otherwise the finally block will drop it
  ResponseBody toReturn;
  try (ResponseBody body = response.body()) {
   Buffer buffer = new Buffer();
   body.source().readAll(buffer);
   toReturn = ResponseBody.create(body.contentType(), body.contentLength(), buffer);
  }
  return response.newBuilder().body(toReturn).build();
 }
}

代码示例来源:origin: net.sf.sprockets/sprockets

/**
   * Download the resource at the URL with the headers and write it to the file.
   *
   * @return Response whose body has already been consumed and closed
   */
  public Response download(String url, File destination, String... headers) throws IOException {
    try (Response resp = response(url, headers)) {
      if (resp.isSuccessful()) {
        try (BufferedSink sink = Okio.buffer(Okio.sink(destination))) {
          resp.body().source().readAll(sink);
        }
      }
      return resp;
    }
  }
}

代码示例来源:origin: pushbit/sprockets

/**
   * Download the resource at the URL with the headers and write it to the file.
   *
   * @return Response whose body has already been consumed and closed
   */
  public Response download(String url, File destination, String... headers) throws IOException {
    try (Response resp = response(url, headers)) {
      if (resp.isSuccessful()) {
        try (BufferedSink sink = Okio.buffer(Okio.sink(destination))) {
          resp.body().source().readAll(sink);
        }
      }
      return resp;
    }
  }
}

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

source.timeout().timeout(30, TimeUnit.SECONDS);
 source.timeout().deadline(5, TimeUnit.MINUTES);
 source.readAll(buffer);
} catch (IOException e) {
 throw new IOException("Failed to execute " + Arrays.toString(command) + ":\n"

代码示例来源:origin: io.syndesis/rest

ImageInputStream iis = ImageIO.createImageInputStream(source.inputStream());) {
so.write(sink.outputStream());
source.readAll(sink);
final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {

代码示例来源:origin: io.syndesis.server/server-endpoint

ImageInputStream iis = ImageIO.createImageInputStream(source.inputStream());) {
so.write(sink.outputStream());
source.readAll(sink);
final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {

相关文章

微信公众号

最新文章

更多