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