| 1 | #include "CreateJavaOutputStreamAdaptor.h" |
| 2 | #include "SkData.h" |
| 3 | #include "SkMalloc.h" |
| 4 | #include "SkRefCnt.h" |
| 5 | #include "SkStream.h" |
| 6 | #include "SkTypes.h" |
| 7 | #include "Utils.h" |
| 8 | |
| 9 | #include <nativehelper/JNIHelp.h> |
| 10 | #include <log/log.h> |
| 11 | #include <memory> |
| 12 | |
| 13 | static jmethodID gInputStream_readMethodID; |
| 14 | static jmethodID gInputStream_skipMethodID; |
| 15 | |
| 16 | /** |
| 17 | * Wrapper for a Java InputStream. |
| 18 | */ |
| 19 | class JavaInputStreamAdaptor : public SkStream { |
| 20 | JavaInputStreamAdaptor(JavaVM* jvm, jobject js, jbyteArray ar, jint capacity, |
| 21 | bool swallowExceptions) |
| 22 | : fJvm(jvm) |
| 23 | , fJavaInputStream(js) |
| 24 | , fJavaByteArray(ar) |
| 25 | , fCapacity(capacity) |
| 26 | , fBytesRead(0) |
| 27 | , fIsAtEnd(false) |
| 28 | , fSwallowExceptions(swallowExceptions) {} |
| 29 | |
| 30 | public: |
| 31 | static JavaInputStreamAdaptor* Create(JNIEnv* env, jobject js, jbyteArray ar, |
| 32 | bool swallowExceptions) { |
| 33 | JavaVM* jvm; |
| 34 | LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&jvm) != JNI_OK); |
| 35 | |
| 36 | js = env->NewGlobalRef(js); |
| 37 | if (!js) { |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | ar = (jbyteArray) env->NewGlobalRef(ar); |
| 42 | if (!ar) { |
| 43 | env->DeleteGlobalRef(js); |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | jint capacity = env->GetArrayLength(ar); |
| 48 | return new JavaInputStreamAdaptor(jvm, js, ar, capacity, swallowExceptions); |
| 49 | } |
| 50 | |
| 51 | ~JavaInputStreamAdaptor() override { |
| 52 | auto* env = android::requireEnv(fJvm); |
| 53 | env->DeleteGlobalRef(fJavaInputStream); |
| 54 | env->DeleteGlobalRef(fJavaByteArray); |
| 55 | } |
| 56 | |
| 57 | size_t read(void* buffer, size_t size) override { |
| 58 | auto* env = android::requireEnv(fJvm); |
| 59 | if (!fSwallowExceptions && checkException(env)) { |
| 60 | // Just in case the caller did not clear from a previous exception. |
| 61 | return 0; |
| 62 | } |
| 63 | if (NULL == buffer) { |
| 64 | if (0 == size) { |
| 65 | return 0; |
| 66 | } else { |
| 67 | /* InputStream.skip(n) can return <=0 but still not be at EOF |
| 68 | If we see that value, we need to call read(), which will |
| 69 | block if waiting for more data, or return -1 at EOF |
| 70 | */ |
| 71 | size_t amountSkipped = 0; |
| 72 | do { |
| 73 | size_t amount = this->doSkip(size - amountSkipped, env); |
| 74 | if (0 == amount) { |
| 75 | char tmp; |
| 76 | amount = this->doRead(&tmp, 1, env); |
| 77 | if (0 == amount) { |
| 78 | // if read returned 0, we're at EOF |
| 79 | fIsAtEnd = true; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | amountSkipped += amount; |
| 84 | } while (amountSkipped < size); |
| 85 | return amountSkipped; |
| 86 | } |
| 87 | } |
| 88 | return this->doRead(buffer, size, env); |
| 89 | } |
| 90 | |
| 91 | bool isAtEnd() const override { return fIsAtEnd; } |
| 92 | |
| 93 | private: |
| 94 | size_t doRead(void* buffer, size_t size, JNIEnv* env) { |
| 95 | size_t bytesRead = 0; |
| 96 | // read the bytes |
| 97 | do { |
| 98 | jint requested = 0; |
| 99 | if (size > static_cast<size_t>(fCapacity)) { |
| 100 | requested = fCapacity; |
| 101 | } else { |
| 102 | // This is safe because requested is clamped to (jint) |
| 103 | // fCapacity. |
| 104 | requested = static_cast<jint>(size); |
| 105 | } |
| 106 | |
| 107 | jint n = env->CallIntMethod(fJavaInputStream, |
| 108 | gInputStream_readMethodID, fJavaByteArray, 0, requested); |
| 109 | if (checkException(env)) { |
| 110 | ALOGD("---- read threw an exception\n"); |
| 111 | return bytesRead; |
| 112 | } |
| 113 | |
| 114 | if (n < 0) { // n == 0 should not be possible, see InputStream read() specifications. |
| 115 | fIsAtEnd = true; |
| 116 | break; // eof |
| 117 | } |
| 118 | |
| 119 | env->GetByteArrayRegion(fJavaByteArray, 0, n, |
| 120 | reinterpret_cast<jbyte*>(buffer)); |
| 121 | if (checkException(env)) { |
| 122 | ALOGD("---- read:GetByteArrayRegion threw an exception\n"); |
| 123 | return bytesRead; |
| 124 | } |
| 125 | |
| 126 | buffer = (void*)((char*)buffer + n); |
| 127 | bytesRead += n; |
| 128 | size -= n; |
| 129 | fBytesRead += n; |
| 130 | } while (size != 0); |
| 131 | |
| 132 | return bytesRead; |
| 133 | } |
| 134 | |
| 135 | size_t doSkip(size_t size, JNIEnv* env) { |
| 136 | jlong skipped = env->CallLongMethod(fJavaInputStream, |
| 137 | gInputStream_skipMethodID, (jlong)size); |
| 138 | if (checkException(env)) { |
| 139 | ALOGD("------- skip threw an exception\n"); |
| 140 | return 0; |
| 141 | } |
| 142 | if (skipped < 0) { |
| 143 | skipped = 0; |
| 144 | } |
| 145 | |
| 146 | return (size_t)skipped; |
| 147 | } |
| 148 | |
| 149 | bool checkException(JNIEnv* env) { |
| 150 | if (!env->ExceptionCheck()) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | env->ExceptionDescribe(); |
| 155 | if (fSwallowExceptions) { |
| 156 | env->ExceptionClear(); |
| 157 | } |
| 158 | |
| 159 | // There is no way to recover from the error, so consider the stream |
| 160 | // to be at the end. |
| 161 | fIsAtEnd = true; |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | JavaVM* fJvm; |
| 167 | jobject fJavaInputStream; |
| 168 | jbyteArray fJavaByteArray; |
| 169 | const jint fCapacity; |
| 170 | size_t fBytesRead; |
| 171 | bool fIsAtEnd; |
| 172 | const bool fSwallowExceptions; |
| 173 | }; |
| 174 | |
| 175 | SkStream* CreateJavaInputStreamAdaptor(JNIEnv* env, jobject stream, jbyteArray storage, |
| 176 | bool swallowExceptions) { |
| 177 | return JavaInputStreamAdaptor::Create(env, stream, storage, swallowExceptions); |
| 178 | } |
| 179 | |
| 180 | sk_sp<SkData> CopyJavaInputStream(JNIEnv* env, jobject inputStream, jbyteArray storage) { |
| 181 | std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, inputStream, storage)); |
| 182 | if (!stream) { |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
| 186 | size_t bufferSize = 4096; |
| 187 | size_t streamLen = 0; |
| 188 | size_t len; |
| 189 | char* data = (char*)sk_malloc_throw(bufferSize); |
| 190 | |
| 191 | while ((len = stream->read(data + streamLen, |
| 192 | bufferSize - streamLen)) != 0) { |
| 193 | streamLen += len; |
| 194 | if (streamLen == bufferSize) { |
| 195 | bufferSize *= 2; |
| 196 | data = (char*)sk_realloc_throw(data, bufferSize); |
| 197 | } |
| 198 | } |
| 199 | data = (char*)sk_realloc_throw(data, streamLen); |
| 200 | |
| 201 | return SkData::MakeFromMalloc(data, streamLen); |
| 202 | } |
| 203 | |
| 204 | /////////////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | static jmethodID gOutputStream_writeMethodID; |
| 207 | static jmethodID gOutputStream_flushMethodID; |
| 208 | |
| 209 | class SkJavaOutputStream : public SkWStream { |
| 210 | public: |
| 211 | SkJavaOutputStream(JNIEnv* env, jobject stream, jbyteArray storage) |
| 212 | : fEnv(env), fJavaOutputStream(stream), fJavaByteArray(storage), fBytesWritten(0) { |
| 213 | fCapacity = env->GetArrayLength(storage); |
| 214 | } |
| 215 | |
| 216 | virtual size_t bytesWritten() const { |
| 217 | return fBytesWritten; |
| 218 | } |
| 219 | |
| 220 | virtual bool write(const void* buffer, size_t size) { |
| 221 | JNIEnv* env = fEnv; |
| 222 | jbyteArray storage = fJavaByteArray; |
| 223 | |
| 224 | while (size > 0) { |
| 225 | jint requested = 0; |
| 226 | if (size > static_cast<size_t>(fCapacity)) { |
| 227 | requested = fCapacity; |
| 228 | } else { |
| 229 | // This is safe because requested is clamped to (jint) |
| 230 | // fCapacity. |
| 231 | requested = static_cast<jint>(size); |
| 232 | } |
| 233 | |
| 234 | env->SetByteArrayRegion(storage, 0, requested, |
| 235 | reinterpret_cast<const jbyte*>(buffer)); |
| 236 | if (env->ExceptionCheck()) { |
| 237 | env->ExceptionDescribe(); |
| 238 | env->ExceptionClear(); |
| 239 | ALOGD("--- write:SetByteArrayElements threw an exception\n"); |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | fEnv->CallVoidMethod(fJavaOutputStream, gOutputStream_writeMethodID, |
| 244 | storage, 0, requested); |
| 245 | if (env->ExceptionCheck()) { |
| 246 | env->ExceptionDescribe(); |
| 247 | env->ExceptionClear(); |
| 248 | ALOGD("------- write threw an exception\n"); |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | buffer = (void*)((char*)buffer + requested); |
| 253 | size -= requested; |
| 254 | fBytesWritten += requested; |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | virtual void flush() { |
| 260 | fEnv->CallVoidMethod(fJavaOutputStream, gOutputStream_flushMethodID); |
| 261 | } |
| 262 | |
| 263 | private: |
| 264 | JNIEnv* fEnv; |
| 265 | jobject fJavaOutputStream; // the caller owns this object |
| 266 | jbyteArray fJavaByteArray; // the caller owns this object |
| 267 | jint fCapacity; |
| 268 | size_t fBytesWritten; |
| 269 | }; |
| 270 | |
| 271 | SkWStream* CreateJavaOutputStreamAdaptor(JNIEnv* env, jobject stream, |
| 272 | jbyteArray storage) { |
| 273 | return new SkJavaOutputStream(env, stream, storage); |
| 274 | } |
| 275 | |
| 276 | static jclass findClassCheck(JNIEnv* env, const char classname[]) { |
| 277 | jclass clazz = env->FindClass(classname); |
| 278 | SkASSERT(!env->ExceptionCheck()); |
| 279 | return clazz; |
| 280 | } |
| 281 | |
| 282 | static jmethodID getMethodIDCheck(JNIEnv* env, jclass clazz, |
| 283 | const char methodname[], const char type[]) { |
| 284 | jmethodID id = env->GetMethodID(clazz, methodname, type); |
| 285 | SkASSERT(!env->ExceptionCheck()); |
| 286 | return id; |
| 287 | } |
| 288 | |
| 289 | int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env) { |
| 290 | jclass inputStream_Clazz = findClassCheck(env, "java/io/InputStream"); |
| 291 | gInputStream_readMethodID = getMethodIDCheck(env, inputStream_Clazz, "read", "([BII)I"); |
| 292 | gInputStream_skipMethodID = getMethodIDCheck(env, inputStream_Clazz, "skip", "(J)J"); |
| 293 | |
| 294 | jclass outputStream_Clazz = findClassCheck(env, "java/io/OutputStream"); |
| 295 | gOutputStream_writeMethodID = getMethodIDCheck(env, outputStream_Clazz, "write", "([BII)V"); |
| 296 | gOutputStream_flushMethodID = getMethodIDCheck(env, outputStream_Clazz, "flush", "()V"); |
| 297 | |
| 298 | return 0; |
| 299 | } |