| 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 |
| 15 private final ByteBuffer mBuf; |
| 16 |
| 17 public ByteBufferInputStream(ByteBuffer buf) { |
| 18 // Makes a read-only copy of the buffer passed in. |
| 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 |