OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 #include "android_webview/native/input_stream_impl.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "net/base/io_buffer.h" | |
9 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java | |
10 // system class and we have to generate C++ hooks for all methods in the class | |
11 // even if they're unused. | |
12 #pragma GCC diagnostic ignored "-Wunused-function" | |
13 #include "jni/InputStream_jni.h" | |
14 | |
15 using base::android::AttachCurrentThread; | |
16 using base::android::ClearException; | |
17 using base::android::JavaRef; | |
18 using JNI_InputStream::Java_InputStream_available; | |
19 using JNI_InputStream::Java_InputStream_skip; | |
20 using JNI_InputStream::Java_InputStream_readI_AB_I_I; | |
21 | |
22 namespace android_webview { | |
23 | |
24 bool RegisterInputStream(JNIEnv* env) { | |
25 return JNI_InputStream::RegisterNativesImpl(env); | |
26 } | |
27 | |
28 // Maximum number of bytes to be read in a single read. | |
29 const int InputStreamImpl::kBufferSize = 4096; | |
30 | |
31 //static | |
32 const InputStreamImpl* InputStreamImpl::FromInputStream( | |
33 const InputStream* input_stream) { | |
34 return static_cast<const InputStreamImpl*>(input_stream); | |
35 } | |
36 | |
37 InputStreamImpl::InputStreamImpl() { | |
38 } | |
39 | |
40 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) | |
41 : jobject_(stream) { | |
42 DCHECK(!stream.is_null()); | |
43 } | |
44 | |
45 InputStreamImpl::~InputStreamImpl() { | |
46 } | |
47 | |
48 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { | |
49 JNIEnv* env = AttachCurrentThread(); | |
50 DCHECK(env); | |
joth
2012/11/20 20:46:35
nit: never need DCHECK(env), AttachCurrentThread d
mkosiba (inactive)
2012/11/21 15:19:47
Done.
| |
51 // TODO: Use unsafe version once BUG 157880 is fixed. | |
joth
2012/11/20 20:46:35
nit: bug 160011 seems specific to this file? (mult
mkosiba (inactive)
2012/11/21 15:19:47
fixing 160011 would currently affect only this fil
| |
52 int bytes = Java_InputStream_available(env, jobject_.obj()); | |
53 if (ClearException(env)) | |
54 return false; | |
55 *bytes_available = bytes; | |
56 return true; | |
57 } | |
58 | |
59 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { | |
60 JNIEnv* env = AttachCurrentThread(); | |
61 DCHECK(env); | |
62 // TODO: Use unsafe version once BUG 157880 is fixed. | |
63 int bytes = Java_InputStream_skip(env, jobject_.obj(), n); | |
64 if (ClearException(env)) | |
65 return false; | |
66 *bytes_skipped = bytes; | |
67 return true; | |
68 } | |
69 | |
70 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { | |
71 JNIEnv* env = AttachCurrentThread(); | |
72 DCHECK(env); | |
73 // TODO: Use unsafe version once BUG 157880 is fixed. | |
joth
2012/11/20 20:46:35
don't think you mean TODO here? line 87.
mkosiba (inactive)
2012/11/21 15:19:47
Done.
| |
74 if (!buffer_.obj()) { | |
75 // Allocate transfer buffer. | |
76 buffer_.Reset(env, env->NewByteArray(kBufferSize)); | |
77 if (ClearException(env)) | |
78 return false; | |
79 } | |
80 | |
81 jbyteArray buffer = buffer_.obj(); | |
82 int read_size = std::min(length, kBufferSize); | |
joth
2012/11/20 20:46:35
move this inside the loop, else it will over-read
mkosiba (inactive)
2012/11/21 15:19:47
right, good spot.
| |
83 *bytes_read = 0; | |
84 | |
85 while (length > 0) { | |
86 int32_t byte_count = | |
87 Java_InputStream_readI_AB_I_I( | |
88 env, jobject_.obj(), buffer, 0, read_size); | |
89 | |
90 if (ClearException(env)) | |
91 return false; | |
92 | |
93 if (byte_count <= 0) | |
94 break; | |
95 | |
96 #ifndef NDEBUG | |
97 int32_t buffer_length = env->GetArrayLength(buffer); | |
98 DCHECK_GE(read_size, byte_count); | |
99 DCHECK_GE(buffer_length, byte_count); | |
100 #endif // NDEBUG | |
101 | |
102 // Copy the data over to the provided C++ side buffer. | |
103 DCHECK_GE(length, byte_count); | |
104 env->GetByteArrayRegion(buffer, 0, byte_count, | |
105 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); | |
106 if (ClearException(env)) | |
107 return false; | |
108 | |
109 *bytes_read += byte_count; | |
110 length -= byte_count; | |
111 } | |
112 return true; | |
113 } | |
114 | |
115 } // namespace android_webview | |
OLD | NEW |