1717#undef LOG_TAG
1818#define LOG_TAG " CursorWindow"
1919
20- // #include <JNIHelp.h>
21- // #include <android_runtime/AndroidRuntime.h>
22- // #include <utils/Log.h>
23- // #include "android_util_Binder.h"
24-
2520#include < jni.h>
2621#include < stdlib.h>
2722#include < stdio.h>
3227#include " jni_elements.h"
3328#include " jni_exception.h"
3429#include " sqlite3_exception.h"
35- // #include <utils/String16.h>
30+
31+ #include < wchar.h>
32+ #include < stdlib.h>
3633
3734namespace sqlcipher {
3835
@@ -170,9 +167,10 @@ LOG_WINDOW("Getting long for %d,%d from %p", row, column, window);
170167#if WINDOW_STORAGE_UTF8
171168 return strtoll ((char const *)window->offsetToPtr (field.data .buffer .offset ), NULL , 0 );
172169#else
173- String8 ascii ((char16_t *) window->offsetToPtr (field.data .buffer .offset ), size / 2 );
174- char const * str = ascii.string ();
175- return strtoll (str, NULL , 0 );
170+ return strtoll ((char const *)window->offsetToPtr (field.data .buffer .offset ), NULL , 0 );
171+ // String8 ascii((char16_t *) window->offsetToPtr(field.data.buffer.offset), size / 2);
172+ // char const * str = ascii.string();
173+ // return strtoll(str, NULL, 0);
176174#endif
177175 } else {
178176 return 0 ;
@@ -308,42 +306,37 @@ LOG_WINDOW("Checking if column is a float for %d,%d from %p", row, column, windo
308306
309307static jstring getString_native (JNIEnv* env, jobject object, jint row, jint column)
310308{
309+ int i;
311310 int32_t err;
312311 CursorWindow * window = GET_WINDOW (env, object);
313- LOG_WINDOW (" Getting string for %d,%d from %p" , row, column, window);
314-
312+ LOG_WINDOW (" Getting string for %d,%d from %p" , row, column, window);
315313 field_slot_t field;
316314 err = window->read_field_slot (row, column, &field);
317315 if (err != 0 ) {
318316 throwExceptionWithRowCol (env, row, column);
319317 return NULL ;
320318 }
321-
322319 uint8_t type = field.type ;
323- if (type == FIELD_TYPE_STRING) {
324- uint32_t size = field.data .buffer .size ;
325- if (size > 0 ) {
326- return env->NewString ((jchar const *)window->offsetToPtr (field.data .buffer .offset ), size);
327- // #if WINDOW_STORAGE_UTF8
328-
329- // // std::wstring_convert<std::codecvt<char16_t,char,std::mbstate_t>,char16_t> convert;
330- // // std::u16string utf16 = convert.from_bytes(window->offsetToPtr(field.data.buffer.offset));
331-
332- // // Pass size - 1 since the UTF8 is null terminated and we don't want a null terminator on the UTF16 string
333- // android::String16 utf16((char const *)window->offsetToPtr(field.data.buffer.offset), size - 1);
334- // return env->NewString((jchar const *)utf16.string(), utf16.size());
335- // #else
336- // return env->NewString((jchar const *)window->offsetToPtr(field.data.buffer.offset), size / 2);
337- // #endif
338- } else {
339- return env->NewStringUTF (" " );
340- }
320+ jint size = (jint)field.data .buffer .size ;
321+ if (type == FIELD_TYPE_NULL) {
322+ return NULL ;
323+ } else if (type == FIELD_TYPE_BLOB) {
324+ throw_sqlite3_exception (env, " Unable to convert BLOB to string" );
325+ return NULL ;
326+ } else if (type == FIELD_TYPE_STRING) {
327+ jchar * buf = (jchar *)window->offsetToPtr (field.data .buffer .offset );
328+ jclass strClass = env->FindClass (" java/lang/String" );
329+ jmethodID ctorID = env->GetMethodID (strClass, " <init>" , " ([BLjava/lang/String;)V" );
330+ jstring encoding = env->NewStringUTF (" UTF-16LE" );
331+ jbyteArray bytes = env->NewByteArray (size);
332+ env->SetByteArrayRegion (bytes, 0 , size, (jbyte*)buf);
333+ return (jstring)env->NewObject (strClass, ctorID, bytes, encoding);
341334 } else if (type == FIELD_TYPE_INTEGER) {
342335 int64_t value;
343336 if (window->getLong (row, column, &value)) {
344- char buf[32 ];
345- snprintf (buf, sizeof (buf), " %lld" , value);
346- return env->NewStringUTF (buf);
337+ char buf[32 ];
338+ snprintf (buf, sizeof (buf), " %lld" , value);
339+ return env->NewStringUTF (( const char *) buf);
347340 }
348341 return NULL ;
349342 } else if (type == FIELD_TYPE_FLOAT) {
@@ -354,14 +347,6 @@ LOG_WINDOW("Getting string for %d,%d from %p", row, column, window);
354347 return env->NewStringUTF (buf);
355348 }
356349 return NULL ;
357- } else if (type == FIELD_TYPE_NULL) {
358- return NULL ;
359- } else if (type == FIELD_TYPE_BLOB) {
360- throw_sqlite3_exception (env, " Unable to convert BLOB to string" );
361- return NULL ;
362- } else {
363- throwUnknowTypeException (env, type);
364- return NULL ;
365350 }
366351}
367352
@@ -410,12 +395,27 @@ LOG_WINDOW("Copying string for %d,%d from %p", row, column, window);
410395 uint32_t size = field.data .buffer .size ;
411396 if (size > 0 ) {
412397
413- if (size > bufferSize * 2 || dst == NULL ) {
414- newArray = env->NewCharArray (sizeCopied);
415- memcpy (newArray, (jchar const *)window->offsetToPtr (field.data .buffer .offset ), size);
398+ // std::string input = (const char*)window->offsetToPtr(field.data.buffer.offset);
399+ // std::wstring result = utf8_to_utf16(input);
400+
401+ jstring input = env->NewStringUTF ((const char *)window->offsetToPtr (field.data .buffer .offset ));
402+ const jchar* buffer = env->GetStringChars (input, JNI_FALSE);
403+ jstring result = env->NewString (buffer, size - 1 );
404+ int32_t strSize = size - 1 ;
405+ if (strSize > bufferSize || dst == NULL ) {
406+ newArray = env->NewCharArray (strSize);
407+ env->SetCharArrayRegion (newArray, 0 , strSize, (jchar const *)result);
416408 } else {
417- memcpy (dst, (jchar const *)window-> offsetToPtr (field. data . buffer . offset ), size );
409+ memcpy (dst, (jchar const *)result, strSize * 2 );
418410 }
411+ sizeCopied = strSize;
412+
413+ // if (size > bufferSize * 2 || dst == NULL) {
414+ // newArray = env->NewCharArray(sizeCopied);
415+ // memcpy(newArray, (jchar const *)window->offsetToPtr(field.data.buffer.offset), size);
416+ // } else {
417+ // memcpy(dst, (jchar const *)window->offsetToPtr(field.data.buffer.offset), size);
418+ // }
419419
420420
421421// #if WINDOW_STORAGE_UTF8
@@ -498,9 +498,10 @@ LOG_WINDOW("Getting double for %d,%d from %p", row, column, window);
498498#if WINDOW_STORAGE_UTF8
499499 return strtod ((char const *)window->offsetToPtr (field.data .buffer .offset ), NULL );
500500#else
501- String8 ascii ((char16_t *) window->offsetToPtr (field.data .buffer .offset ), size / 2 );
502- char const * str = ascii.string ();
503- return strtod (str, NULL );
501+ return strtod ((char const *)window->offsetToPtr (field.data .buffer .offset ), NULL );
502+ // String8 ascii((char16_t *) window->offsetToPtr(field.data.buffer.offset), size / 2);
503+ // char const * str = ascii.string();
504+ // return strtod(str, NULL);
504505#endif
505506 } else {
506507 return 0.0 ;
@@ -699,6 +700,7 @@ static JNINativeMethod sMethods[] =
699700 {" getBlob_native" , " (II)[B" , (void *)getBlob_native},
700701 {" isBlob_native" , " (II)Z" , (void *)isBlob_native},
701702 {" getString_native" , " (II)Ljava/lang/String;" , (void *)getString_native},
703+ // {"getString_native", "(II)[B", (void *)getString_native},
702704 {" copyStringToBuffer_native" , " (IIILandroid/database/CharArrayBuffer;)[C" , (void *)copyStringToBuffer_native},
703705 {" getDouble_native" , " (II)D" , (void *)getDouble_native},
704706 {" isNull_native" , " (II)Z" , (void *)isNull_native},
0 commit comments