本文整理了Java中okio.BufferedSource.inputStream()
方法的一些代码示例,展示了BufferedSource.inputStream()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedSource.inputStream()
方法的具体详情如下:
包路径:okio.BufferedSource
类名称:BufferedSource
方法名:inputStream
[英]Returns an input stream that reads from this source.
[中]返回从该源读取的输入流。
代码示例来源:origin: square/okhttp
public final InputStream byteStream() {
return source().inputStream();
}
代码示例来源:origin: square/okhttp
@Override public int read(char[] cbuf, int off, int len) throws IOException {
if (closed) throw new IOException("Stream closed");
Reader delegate = this.delegate;
if (delegate == null) {
Charset charset = Util.bomAwareCharset(source, this.charset);
delegate = this.delegate = new InputStreamReader(source.inputStream(), charset);
}
return delegate.read(cbuf, off, len);
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
public final InputStream byteStream() {
return source().inputStream();
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
@Override public int read(char[] cbuf, int off, int len) throws IOException {
if (closed) throw new IOException("Stream closed");
Reader delegate = this.delegate;
if (delegate == null) {
Charset charset = Util.bomAwareCharset(source, this.charset);
delegate = this.delegate = new InputStreamReader(source.inputStream(), charset);
}
return delegate.read(cbuf, off, len);
}
代码示例来源:origin: square/picasso
} else {
if (calculateSize) {
BitmapFactory.decodeStream(bufferedSource.peek().inputStream(), null, options);
calculateInSampleSize(request.targetWidth, request.targetHeight,
checkNotNull(options, "options == null"), request);
bitmap = BitmapFactory.decodeStream(bufferedSource.inputStream(), null, options);
代码示例来源:origin: square/okio
@Test public void inputStreamCloses() throws Exception {
BufferedSource source = Okio.buffer((Source) new Buffer());
InputStream in = source.inputStream();
in.close();
try {
source.require(1);
fail();
} catch (IllegalStateException e) {
assertEquals("closed", e.getMessage());
}
}
代码示例来源:origin: aa112901/remusic
public static FilterInputStream getFromCache(Context context, String url) throws Exception {
// File cacheDirectory = new File("/storage/emulated/0/Android/data/com.name.demo .dev/cache/HttpCache");
File cacheDirectory = context.getExternalCacheDir();
DiskLruCache cache = DiskLruCache.create(FileSystem.SYSTEM, cacheDirectory, 201105, 2, 1024 * 1024 * 30);
cache.flush();
String key = Util.md5Hex(url);
final DiskLruCache.Snapshot snapshot;
try {
snapshot = cache.get(key);
if (snapshot == null) {
return null;
}
} catch (IOException e) {
return null;
}
okio.Source source = snapshot.getSource(1);
BufferedSource metadata = Okio.buffer(source);
FilterInputStream bodyIn = new FilterInputStream(metadata.inputStream()) {
@Override
public void close() throws IOException {
snapshot.close();
super.close();
}
};
return bodyIn;
}
代码示例来源:origin: square/okio
@Test public void inputStreamCharByChar() throws Exception {
sink.writeUtf8("abc");
sink.emit();
InputStream in = source.inputStream();
assertEquals('a', in.read());
assertEquals('b', in.read());
assertEquals('c', in.read());
assertEquals(-1, in.read());
}
代码示例来源:origin: lingochamp/okdownload
@Test
public void getInputStream_executed_getRightInputStream() throws IOException {
final Call call = mock(Call.class);
when(client.newCall(any(Request.class))).thenReturn(call);
final ResponseBody body = mock(ResponseBody.class);
final Response response = createResponseBuilder()
.body(body).build();
when(call.execute()).thenReturn(response);
final BufferedSource source = mock(BufferedSource.class);
when(body.source()).thenReturn(source);
final InputStream expectedInputStream = mock(InputStream.class);
when(source.inputStream()).thenReturn(expectedInputStream);
connection.execute();
final InputStream resultInputStream = connection.getInputStream();
assertThat(resultInputStream).isEqualTo(expectedInputStream);
}
代码示例来源:origin: square/okio
@Test public void inputStreamSkip() throws Exception {
sink.writeUtf8("abcde");
sink.emit();
InputStream in = source.inputStream();
assertEquals(4, in.skip(4));
assertEquals('e', in.read());
sink.writeUtf8("abcde");
sink.emit();
assertEquals(5, in.skip(10)); // Try to skip too much.
assertEquals(0, in.skip(1)); // Try to skip when exhausted.
}
代码示例来源:origin: square/okio
@Test public void inputStreamBounds() throws IOException {
sink.writeUtf8(repeat('a', 100));
sink.emit();
InputStream in = source.inputStream();
try {
in.read(new byte[100], 50, 51);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
}
}
代码示例来源:origin: square/okio
@Test public void inputStreamOffsetCount() throws Exception {
sink.writeUtf8("abcde");
sink.emit();
InputStream in = source.inputStream();
byte[] bytes = { 'z', 'z', 'z', 'z', 'z' };
int read = in.read(bytes, 1, 3);
if (factory.isOneByteAtATime()) {
assertEquals(1, read);
assertByteArrayEquals("zazzz", bytes);
} else {
assertEquals(3, read);
assertByteArrayEquals("zabcz", bytes);
}
}
代码示例来源:origin: square/okio
@Test public void inputStream() throws Exception {
sink.writeUtf8("abc");
sink.emit();
InputStream in = source.inputStream();
byte[] bytes = { 'z', 'z', 'z' };
int read = in.read(bytes);
if (factory.isOneByteAtATime()) {
assertEquals(1, read);
assertByteArrayEquals("azz", bytes);
read = in.read(bytes);
assertEquals(1, read);
assertByteArrayEquals("bzz", bytes);
read = in.read(bytes);
assertEquals(1, read);
assertByteArrayEquals("czz", bytes);
} else {
assertEquals(3, read);
assertByteArrayEquals("abc", bytes);
}
assertEquals(-1, in.read());
}
代码示例来源:origin: square/okio
@Test public void gzipSink() throws Exception {
Pipe pipe = new Pipe(1024 * 1024);
GzipSink gzipSink = new GzipSink(pipe.sink());
// Disable compression to speed up a slow test. Improved from 141s to 35s on one machine.
gzipSink.deflater().setLevel(Deflater.NO_COMPRESSION);
Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), gzipSink);
HashingSink hashingSink = HashingSink.sha256(Okio.blackhole());
GZIPInputStream gzipIn = new GZIPInputStream(Okio.buffer(pipe.source()).inputStream());
readAllAndClose(Okio.source(gzipIn), hashingSink);
assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get());
assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash());
}
代码示例来源:origin: square/okio
InputStream is = bufferedSource.inputStream();
try {
is.read();
代码示例来源:origin: square/okio
@Test public void inputStreamTracksSegments() throws Exception {
Buffer source = new Buffer();
source.writeUtf8("a");
source.writeUtf8(repeat('b', SEGMENT_SIZE));
source.writeUtf8("c");
InputStream in = Okio.buffer((Source) source).inputStream();
assertEquals(0, in.available());
assertEquals(SEGMENT_SIZE + 2, source.size());
// Reading one byte buffers a full segment.
assertEquals('a', in.read());
assertEquals(SEGMENT_SIZE - 1, in.available());
assertEquals(2, source.size());
// Reading as much as possible reads the rest of that buffered segment.
byte[] data = new byte[SEGMENT_SIZE * 2];
assertEquals(SEGMENT_SIZE - 1, in.read(data, 0, data.length));
assertEquals(repeat('b', SEGMENT_SIZE - 1), new String(data, 0, SEGMENT_SIZE - 1, UTF_8));
assertEquals(2, source.size());
// Continuing to read buffers the next segment.
assertEquals('b', in.read());
assertEquals(1, in.available());
assertEquals(0, source.size());
// Continuing to read reads from the buffer.
assertEquals('c', in.read());
assertEquals(0, in.available());
assertEquals(0, source.size());
// Once we've exhausted the source, we're done.
assertEquals(-1, in.read());
assertEquals(0, source.size());
}
代码示例来源:origin: com.nytimes.android/middleware
@Override
public Parsed call(@Nonnull BufferedSource source) {
try (InputStreamReader reader = new InputStreamReader(source.inputStream(), Charset.forName("UTF-8"))) {
return gson.fromJson(reader, type);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
代码示例来源:origin: apache/servicemix-bundles
@Override public int read(char[] cbuf, int off, int len) throws IOException {
if (closed) throw new IOException("Stream closed");
Reader delegate = this.delegate;
if (delegate == null) {
Charset charset = Util.bomAwareCharset(source, this.charset);
delegate = this.delegate = new InputStreamReader(source.inputStream(), charset);
}
return delegate.read(cbuf, off, len);
}
代码示例来源:origin: com.github.ljun20160606/okhttp
@Override public int read(char[] cbuf, int off, int len) throws IOException {
if (closed) throw new IOException("Stream closed");
Reader delegate = this.delegate;
if (delegate == null) {
Charset charset = Util.bomAwareCharset(source, this.charset);
delegate = this.delegate = new InputStreamReader(source.inputStream(), charset);
}
return delegate.read(cbuf, off, len);
}
代码示例来源:origin: com.nytimes.android/middleware3
@Override
public Parsed apply(@NonNull BufferedSource bufferedSource) throws ParserException {
try (InputStreamReader reader = new InputStreamReader(bufferedSource.inputStream(), Charset.forName("UTF-8"))) {
return gson.fromJson(reader, type);
} catch (IOException e) {
throw new ParserException(e.getMessage(), e);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!