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.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::ClearException; |
| 16 using base::android::JavaRef; |
| 17 using JNI_InputStream::Java_InputStream_available; |
| 18 using JNI_InputStream::Java_InputStream_skip; |
| 19 using JNI_InputStream::Java_InputStream_readI_AB_I_I; |
| 20 |
| 21 namespace android_webview { |
| 22 |
| 23 bool RegisterInputStream(JNIEnv* env) { |
| 24 return JNI_InputStream::RegisterNativesImpl(env); |
| 25 } |
| 26 |
| 27 // Maximum number of bytes to be read in a single read. |
| 28 const int InputStream::kBufferSize = 4096; |
| 29 |
| 30 InputStream::InputStream() { |
| 31 } |
| 32 |
| 33 InputStream::InputStream(const JavaRef<jobject>& stream) |
| 34 : jobject_(stream) { |
| 35 DCHECK(!stream.is_null()); |
| 36 } |
| 37 |
| 38 InputStream::~InputStream() { |
| 39 } |
| 40 |
| 41 bool InputStream::BytesAvailable(JNIEnv* env, |
| 42 int* bytes_available) const { |
| 43 // TODO: Use unsafe version once BUG 157880 is fixed. |
| 44 int bytes = Java_InputStream_available(env, jobject_.obj()); |
| 45 if (ClearException(env)) |
| 46 return false; |
| 47 *bytes_available = bytes; |
| 48 return true; |
| 49 } |
| 50 |
| 51 bool InputStream::Skip(JNIEnv* env, |
| 52 int64_t n, |
| 53 int64_t* bytes_skipped) { |
| 54 // TODO: Use unsafe version once BUG 157880 is fixed. |
| 55 int bytes = Java_InputStream_skip(env, jobject_.obj(), n); |
| 56 if (ClearException(env)) |
| 57 return false; |
| 58 *bytes_skipped = bytes; |
| 59 return true; |
| 60 } |
| 61 |
| 62 bool InputStream::Read(JNIEnv* env, |
| 63 net::IOBuffer* dest, |
| 64 int length, |
| 65 int* bytes_read) { |
| 66 if (!buffer_.obj()) { |
| 67 // Allocate transfer buffer. |
| 68 buffer_.Reset(env, env->NewByteArray(kBufferSize)); |
| 69 if (ClearException(env)) |
| 70 return false; |
| 71 } |
| 72 |
| 73 jbyteArray buffer = buffer_.obj(); |
| 74 int read_size = std::min(length, kBufferSize); |
| 75 *bytes_read = 0; |
| 76 |
| 77 while (length > 0) { |
| 78 int32_t byte_count = |
| 79 Java_InputStream_readI_AB_I_I( |
| 80 env, jobject_.obj(), buffer, 0, read_size); |
| 81 |
| 82 if (ClearException(env)) |
| 83 return false; |
| 84 |
| 85 if (byte_count <= 0) |
| 86 break; |
| 87 |
| 88 #ifndef NDEBUG |
| 89 int32_t buffer_length = env->GetArrayLength(buffer); |
| 90 DCHECK_GE(read_size, byte_count); |
| 91 DCHECK_GE(buffer_length, byte_count); |
| 92 #endif // NDEBUG |
| 93 |
| 94 // Copy the data over to the provided C++ side buffer. |
| 95 DCHECK_GE(length, byte_count); |
| 96 env->GetByteArrayRegion(buffer, 0, byte_count, |
| 97 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); |
| 98 if (ClearException(env)) |
| 99 return false; |
| 100 |
| 101 *bytes_read += byte_count; |
| 102 length -= byte_count; |
| 103 } |
| 104 return true; |
| 105 } |
| 106 |
| 107 } // namespace android_webview |
OLD | NEW |