Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(533)

Unified Diff: platform_tools/android/apps/canvasproof/src/main/jni/JavaInputStream.cpp

Issue 1258123004: android/apps: Add CanvasProof App; (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-09-04 (Friday) 13:29:02 EDT Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: platform_tools/android/apps/canvasproof/src/main/jni/JavaInputStream.cpp
diff --git a/platform_tools/android/apps/canvasproof/src/main/jni/JavaInputStream.cpp b/platform_tools/android/apps/canvasproof/src/main/jni/JavaInputStream.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..823b72f7ef0b34df83f76cfc96542efe7b4e3616
--- /dev/null
+++ b/platform_tools/android/apps/canvasproof/src/main/jni/JavaInputStream.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "JavaInputStream.h"
+
+JavaInputStream::JavaInputStream(
+ JNIEnv* env, jbyteArray javaBuffer, jobject inputStream)
+ : fEnv(env)
+ , fStartIndex(0)
+ , fEndIndex(0) {
+ SkASSERT(inputStream);
+ SkASSERT(javaBuffer);
+ fInputStream = inputStream;
+ fJavaBuffer = javaBuffer;
+ fInputStreamClass = env->FindClass("java/io/InputStream");
+ SkASSERT(fInputStreamClass);
+ fReadMethodID = env->GetMethodID(fInputStreamClass, "read", "([B)I");
+ SkASSERT(fReadMethodID);
+}
+
+bool JavaInputStream::isAtEnd() const { return fStartIndex == fEndIndex; }
+
+size_t JavaInputStream::read(void* voidBuffer, size_t size) {
+ size_t totalRead = 0;
+ char* buffer = static_cast<char*>(voidBuffer); // may be NULL;
+ while (size) {
+ // make sure the cache has at least one byte or is done.
+ if (fStartIndex == fEndIndex) {
+ jint count =
+ fEnv->CallIntMethod(fInputStream, fReadMethodID, fJavaBuffer);
+ if (fEnv->ExceptionCheck()) {
+ fEnv->ExceptionDescribe();
+ fEnv->ExceptionClear();
+ SkDebugf("---- java.io.InputStream::read() threw an exception\n");
+ return 0;
+ }
+ fStartIndex = 0;
+ fEndIndex = count;
+ if (this->isAtEnd()) {
+ return totalRead; // No more to read.
+ }
+ }
+ SkASSERT(fEndIndex > fStartIndex);
+ size_t length = SkTMin(SkToSizeT(fEndIndex - fStartIndex), size);
+ if (buffer && length) {
+ jbyte* bufferElements
+ = fEnv->GetByteArrayElements(fJavaBuffer, NULL);
+ memcpy(buffer, &bufferElements[fStartIndex], length);
+ buffer += length;
+ fEnv->ReleaseByteArrayElements(fJavaBuffer, bufferElements, 0);
+ }
+ totalRead += length;
+ size -= length;
+ fStartIndex += length;
+ }
+ return totalRead;
+}

Powered by Google App Engine
This is Rietveld 408576698