Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/urlconnection/ByteBufferInputStream.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/ByteBufferInputStream.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/ByteBufferInputStream.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..feee74dec832a850e1a04480a87c35d2bc7c7a06 |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/ByteBufferInputStream.java |
| @@ -0,0 +1,40 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net.urlconnection; |
| + |
| +import java.io.InputStream; |
| +import java.nio.ByteBuffer; |
| + |
| +/** |
| + * Wrapper class to construct an InputSteam based on a ByteBuffer. |
| + */ |
| +class ByteBufferInputStream extends InputStream { |
| + |
|
mmenke
2014/11/18 15:53:52
nit: Remove blank line, to be consistent with oth
xunjieli
2014/11/18 18:13:35
Done.
|
| + private final ByteBuffer mBuf; |
|
mmenke
2014/11/18 15:53:51
optional nit: Suggest just writing out buffer for
xunjieli
2014/11/18 18:13:35
Done.
|
| + |
| + public ByteBufferInputStream(ByteBuffer buf) { |
| + // Makes a read-only copy of the buffer passed in. |
|
mmenke
2014/11/18 15:53:51
This is not actually a copy, but a read only refer
xunjieli
2014/11/18 18:13:35
The java docs says it is a new instance http://dev
mmenke
2014/11/18 18:40:59
It's a new Java object, but it shares the buffer.
|
| + mBuf = buf.asReadOnlyBuffer(); |
| + } |
| + |
| + @Override |
| + public int read() { |
| + if (!mBuf.hasRemaining()) { |
| + return -1; |
| + } |
| + return mBuf.get() & 0xFF; |
| + } |
| + |
| + @Override |
| + public int read(byte[] bytes, int off, int len) { |
| + if (!mBuf.hasRemaining()) { |
| + return -1; |
| + } |
| + |
| + len = Math.min(len, mBuf.remaining()); |
| + mBuf.get(bytes, off, len); |
| + return len; |
| + } |
| +} |