OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "android_webview/native/input_stream_impl.h" | 5 #include "android_webview/native/input_stream_impl.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java | 8 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java |
9 // system class and we have to generate C++ hooks for all methods in the class | 9 // system class and we have to generate C++ hooks for all methods in the class |
10 // even if they're unused. | 10 // even if they're unused. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 InputStreamImpl::InputStreamImpl() { | 42 InputStreamImpl::InputStreamImpl() { |
43 } | 43 } |
44 | 44 |
45 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) | 45 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) |
46 : jobject_(stream) { | 46 : jobject_(stream) { |
47 DCHECK(!stream.is_null()); | 47 DCHECK(!stream.is_null()); |
48 } | 48 } |
49 | 49 |
50 InputStreamImpl::~InputStreamImpl() { | 50 InputStreamImpl::~InputStreamImpl() { |
51 JNIEnv* env = AttachCurrentThread(); | 51 JNIEnv* env = AttachCurrentThread(); |
52 Java_InputStreamUtil_close(env, jobject_.obj()); | 52 Java_InputStreamUtil_close(env, jobject_); |
53 } | 53 } |
54 | 54 |
55 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { | 55 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { |
56 JNIEnv* env = AttachCurrentThread(); | 56 JNIEnv* env = AttachCurrentThread(); |
57 int bytes = Java_InputStreamUtil_available(env, jobject_.obj()); | 57 int bytes = Java_InputStreamUtil_available(env, jobject_); |
58 if (bytes == kExceptionThrownStatusCode) | 58 if (bytes == kExceptionThrownStatusCode) |
59 return false; | 59 return false; |
60 *bytes_available = bytes; | 60 *bytes_available = bytes; |
61 return true; | 61 return true; |
62 } | 62 } |
63 | 63 |
64 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { | 64 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { |
65 JNIEnv* env = AttachCurrentThread(); | 65 JNIEnv* env = AttachCurrentThread(); |
66 int bytes = Java_InputStreamUtil_skip(env, jobject_.obj(), n); | 66 int bytes = Java_InputStreamUtil_skip(env, jobject_, n); |
67 if (bytes < 0) | 67 if (bytes < 0) |
68 return false; | 68 return false; |
69 if (bytes > n) | 69 if (bytes > n) |
70 return false; | 70 return false; |
71 *bytes_skipped = bytes; | 71 *bytes_skipped = bytes; |
72 return true; | 72 return true; |
73 } | 73 } |
74 | 74 |
75 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { | 75 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { |
76 JNIEnv* env = AttachCurrentThread(); | 76 JNIEnv* env = AttachCurrentThread(); |
77 if (!buffer_.obj()) { | 77 if (!buffer_.obj()) { |
78 // Allocate transfer buffer. | 78 // Allocate transfer buffer. |
79 base::android::ScopedJavaLocalRef<jbyteArray> temp( | 79 base::android::ScopedJavaLocalRef<jbyteArray> temp( |
80 env, env->NewByteArray(kBufferSize)); | 80 env, env->NewByteArray(kBufferSize)); |
81 buffer_.Reset(temp); | 81 buffer_.Reset(temp); |
82 if (ClearException(env)) | 82 if (ClearException(env)) |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 int remaining_length = length; | 86 int remaining_length = length; |
87 char* dest_write_ptr = dest->data(); | 87 char* dest_write_ptr = dest->data(); |
88 jbyteArray buffer = buffer_.obj(); | 88 jbyteArray buffer = buffer_.obj(); |
89 *bytes_read = 0; | 89 *bytes_read = 0; |
90 | 90 |
91 while (remaining_length > 0) { | 91 while (remaining_length > 0) { |
92 const int max_transfer_length = std::min(remaining_length, kBufferSize); | 92 const int max_transfer_length = std::min(remaining_length, kBufferSize); |
93 const int transfer_length = Java_InputStreamUtil_read( | 93 const int transfer_length = Java_InputStreamUtil_read( |
94 env, jobject_.obj(), buffer, 0, max_transfer_length); | 94 env, jobject_, buffer, 0, max_transfer_length); |
95 if (transfer_length == kExceptionThrownStatusCode) | 95 if (transfer_length == kExceptionThrownStatusCode) |
96 return false; | 96 return false; |
97 | 97 |
98 if (transfer_length < 0) // EOF | 98 if (transfer_length < 0) // EOF |
99 break; | 99 break; |
100 | 100 |
101 // Note: it is possible, yet unlikely, that the Java InputStream returns | 101 // Note: it is possible, yet unlikely, that the Java InputStream returns |
102 // a transfer_length == 0 from time to time. In such cases we just continue | 102 // a transfer_length == 0 from time to time. In such cases we just continue |
103 // the read until we get either valid data or reach EOF. | 103 // the read until we get either valid data or reach EOF. |
104 if (transfer_length == 0) | 104 if (transfer_length == 0) |
(...skipping 18 matching lines...) Expand all Loading... |
123 dest_write_ptr += transfer_length; | 123 dest_write_ptr += transfer_length; |
124 } | 124 } |
125 // bytes_read can be strictly less than the req. length if EOF is encountered. | 125 // bytes_read can be strictly less than the req. length if EOF is encountered. |
126 DCHECK_GE(remaining_length, 0); | 126 DCHECK_GE(remaining_length, 0); |
127 DCHECK_LE(remaining_length, length); | 127 DCHECK_LE(remaining_length, length); |
128 *bytes_read = length - remaining_length; | 128 *bytes_read = length - remaining_length; |
129 return true; | 129 return true; |
130 } | 130 } |
131 | 131 |
132 } // namespace android_webview | 132 } // namespace android_webview |
OLD | NEW |