1 // Copyright (c) 2010 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/jni/jni_utils.h" 6 #include "android/jni/platform_file_jni.h" 7 #include "base/file_path.h" 8 #include "base/logging.h" 9 10 namespace android { 11 12 JavaISWrapper::JavaISWrapper(const FilePath& path) { 13 JNIEnv* env = jni::GetJNIEnv(); 14 jclass inputStreamClass = env->FindClass("java/io/InputStream"); 15 m_read = env->GetMethodID(inputStreamClass, "read", "([B)I"); 16 m_close = env->GetMethodID(inputStreamClass, "close", "()V"); 17 18 jclass bridgeClass = env->FindClass("android/webkit/JniUtil"); 19 jmethodID method = env->GetStaticMethodID( 20 bridgeClass, 21 "contentUrlStream", 22 "(Ljava/lang/String;)Ljava/io/InputStream;"); 23 m_inputStream = env->NewGlobalRef(env->CallStaticObjectMethod( 24 bridgeClass, 25 method, 26 jni::ConvertUTF8ToJavaString(env, path.value()))); 27 env->DeleteLocalRef(bridgeClass); 28 env->DeleteLocalRef(inputStreamClass); 29 } 30 31 JavaISWrapper::~JavaISWrapper() { 32 JNIEnv* env = jni::GetJNIEnv(); 33 env->CallVoidMethod(m_inputStream, m_close); 34 jni::CheckException(env); 35 env->DeleteGlobalRef(m_inputStream); 36 } 37 38 int JavaISWrapper::Read(char* out, int length) { 39 JNIEnv* env = jni::GetJNIEnv(); 40 jbyteArray buffer = env->NewByteArray(length); 41 42 int size = (int) env->CallIntMethod(m_inputStream, m_read, buffer); 43 if (jni::CheckException(env) || size < 0) { 44 env->DeleteLocalRef(buffer); 45 return 0; 46 } 47 48 env->GetByteArrayRegion(buffer, 0, size, (jbyte*)out); 49 env->DeleteLocalRef(buffer); 50 return size; 51 } 52 53 uint64 contentUrlSize(const FilePath& name) { 54 JNIEnv* env = jni::GetJNIEnv(); 55 jclass bridgeClass = env->FindClass("android/webkit/JniUtil"); 56 jmethodID method = env->GetStaticMethodID( 57 bridgeClass, 58 "contentUrlSize", 59 "(Ljava/lang/String;)J"); 60 jlong length = env->CallStaticLongMethod( 61 bridgeClass, 62 method, 63 jni::ConvertUTF8ToJavaString(env, name.value())); 64 env->DeleteLocalRef(bridgeClass); 65 66 return static_cast<uint64>(length); 67 } 68 69 } 70 71