OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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; |
| 6 |
| 7 import java.io.FileInputStream; |
| 8 import java.io.IOException; |
| 9 import java.io.InputStream; |
| 10 import java.nio.ByteBuffer; |
| 11 import java.nio.channels.ReadableByteChannel; |
| 12 import java.util.concurrent.atomic.AtomicBoolean; |
| 13 |
| 14 /** |
| 15 * Adapts an {@link InputStream} into a {@link ReadableByteChannel}, exactly lik
e |
| 16 * {@link java.nio.channels.Channels#newChannel(InputStream)} does, but more eff
iciently, since it |
| 17 * does not allocate a temporary buffer if it doesn't have to, and it freely tak
es advantage of |
| 18 * {@link FileInputStream}'s trivial conversion to {@link java.nio.channels.File
Channel}. |
| 19 */ |
| 20 final class InputStreamChannel implements ReadableByteChannel { |
| 21 private static final int MAX_TMP_BUFFER_SIZE = 16384; |
| 22 private static final int MIN_TMP_BUFFER_SIZE = 4096; |
| 23 private final InputStream mInputStream; |
| 24 private final AtomicBoolean mIsOpen = new AtomicBoolean(true); |
| 25 |
| 26 private InputStreamChannel(InputStream inputStream) { |
| 27 this.mInputStream = inputStream; |
| 28 } |
| 29 |
| 30 static ReadableByteChannel wrap(InputStream inputStream) { |
| 31 if (inputStream instanceof FileInputStream) { |
| 32 return ((FileInputStream) inputStream).getChannel(); |
| 33 } |
| 34 return new InputStreamChannel(inputStream); |
| 35 } |
| 36 |
| 37 @Override |
| 38 public int read(ByteBuffer dst) throws IOException { |
| 39 final int read; |
| 40 if (dst.hasArray()) { |
| 41 read = mInputStream.read( |
| 42 dst.array(), dst.arrayOffset() + dst.position(), dst.remaini
ng()); |
| 43 if (read > 0) { |
| 44 dst.position(dst.position() + read); |
| 45 } |
| 46 } else { |
| 47 // Since we're allocating a buffer for every read, we want to choose
a good size - on |
| 48 // Android, the only case where a ByteBuffer won't have a backing by
te[] is if it was |
| 49 // created wrapping a void * in native code, or if it represents a m
emory-mapped file. |
| 50 // Especially in the latter case, we want to avoid allocating a buff
er that could be |
| 51 // very large. |
| 52 final int possibleToRead = Math.min( |
| 53 Math.max(mInputStream.available(), MIN_TMP_BUFFER_SIZE), dst
.remaining()); |
| 54 final int reasonableToRead = Math.min(MAX_TMP_BUFFER_SIZE, possibleT
oRead); |
| 55 byte[] tmpBuf = new byte[reasonableToRead]; |
| 56 read = mInputStream.read(tmpBuf); |
| 57 if (read > 0) { |
| 58 dst.put(tmpBuf, 0, read); |
| 59 } |
| 60 } |
| 61 return read; |
| 62 } |
| 63 |
| 64 @Override |
| 65 public boolean isOpen() { |
| 66 return mIsOpen.get(); |
| 67 } |
| 68 |
| 69 @Override |
| 70 public void close() throws IOException { |
| 71 if (mIsOpen.compareAndSet(true, false)) { |
| 72 mInputStream.close(); |
| 73 } |
| 74 } |
| 75 } |
OLD | NEW |