Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net.urlconnection; | |
| 6 | |
| 7 import java.io.InputStream; | |
| 8 import java.nio.ByteBuffer; | |
| 9 | |
| 10 /** | |
| 11 * Wrapper class to construct an InputSteam based on a ByteBuffer. | |
| 12 */ | |
| 13 class ByteBufferInputStream extends InputStream { | |
| 14 | |
|
mmenke
2014/11/18 15:53:52
nit: Remove blank line, to be consistent with oth
xunjieli
2014/11/18 18:13:35
Done.
| |
| 15 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.
| |
| 16 | |
| 17 public ByteBufferInputStream(ByteBuffer buf) { | |
| 18 // 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.
| |
| 19 mBuf = buf.asReadOnlyBuffer(); | |
| 20 } | |
| 21 | |
| 22 @Override | |
| 23 public int read() { | |
| 24 if (!mBuf.hasRemaining()) { | |
| 25 return -1; | |
| 26 } | |
| 27 return mBuf.get() & 0xFF; | |
| 28 } | |
| 29 | |
| 30 @Override | |
| 31 public int read(byte[] bytes, int off, int len) { | |
| 32 if (!mBuf.hasRemaining()) { | |
| 33 return -1; | |
| 34 } | |
| 35 | |
| 36 len = Math.min(len, mBuf.remaining()); | |
| 37 mBuf.get(bytes, off, len); | |
| 38 return len; | |
| 39 } | |
| 40 } | |
| OLD | NEW |