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 li ke | |
pauljensen
2015/12/08 19:49:52
extra space before "Adapts"
Charles
2015/12/11 16:45:39
Done.
| |
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 | |
pauljensen
2015/12/08 19:49:52
it looks like you used "git cl format" to line-wra
Charles
2015/12/11 16:45:39
Done.
| |
49 // a ByteBuffer won't have a backing byte[] is if it was created wra pping a void * in | |
50 // native code, or if it | |
51 // represents a memory-mapped file. Especially in the latter case, w e want to avoid | |
52 // allocating a buffer that | |
53 // could be very large. | |
54 final int possibleToRead = Math.min( | |
55 Math.max(mInputStream.available(), MIN_TMP_BUFFER_SIZE), dst .remaining()); | |
56 final int reasonableToRead = Math.min(MAX_TMP_BUFFER_SIZE, possibleT oRead); | |
57 byte[] tmpBuf = new byte[Math.min(reasonableToRead, MAX_TMP_BUFFER_S IZE)]; | |
pauljensen
2015/12/08 19:49:52
it's interesting that you marked your ints final b
Charles
2015/12/11 16:45:39
The style guide is "don't put final on local varia
pauljensen
2016/03/17 12:10:04
Charles, what is "The style guide" you are referri
| |
58 read = mInputStream.read(tmpBuf); | |
59 if (read > 0) { | |
60 dst.put(tmpBuf, 0, read); | |
61 } | |
62 } | |
63 return read; | |
64 } | |
65 | |
66 @Override | |
67 public boolean isOpen() { | |
68 return mIsOpen.get(); | |
69 } | |
70 | |
71 @Override | |
72 public void close() throws IOException { | |
73 if (mIsOpen.compareAndSet(true, false)) { | |
74 mInputStream.close(); | |
75 } | |
76 } | |
77 } | |
OLD | NEW |