17
17
#undef LOG_TAG
18
18
#define LOG_TAG " CursorWindow"
19
19
20
- // #include <JNIHelp.h>
21
- // #include <android_runtime/AndroidRuntime.h>
22
- // #include <utils/Log.h>
23
- // #include "android_util_Binder.h"
24
-
25
20
#include < jni.h>
26
21
#include < stdlib.h>
27
22
#include < stdio.h>
32
27
#include " jni_elements.h"
33
28
#include " jni_exception.h"
34
29
#include " sqlite3_exception.h"
35
- // #include <utils/String16.h>
30
+
31
+ #include < wchar.h>
32
+ #include < stdlib.h>
36
33
37
34
namespace sqlcipher {
38
35
@@ -170,9 +167,10 @@ LOG_WINDOW("Getting long for %d,%d from %p", row, column, window);
170
167
#if WINDOW_STORAGE_UTF8
171
168
return strtoll ((char const *)window->offsetToPtr (field.data .buffer .offset ), NULL , 0 );
172
169
#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);
176
174
#endif
177
175
} else {
178
176
return 0 ;
@@ -308,42 +306,37 @@ LOG_WINDOW("Checking if column is a float for %d,%d from %p", row, column, windo
308
306
309
307
static jstring getString_native (JNIEnv* env, jobject object, jint row, jint column)
310
308
{
309
+ int i;
311
310
int32_t err;
312
311
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);
315
313
field_slot_t field;
316
314
err = window->read_field_slot (row, column, &field);
317
315
if (err != 0 ) {
318
316
throwExceptionWithRowCol (env, row, column);
319
317
return NULL ;
320
318
}
321
-
322
319
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);
341
334
} else if (type == FIELD_TYPE_INTEGER) {
342
335
int64_t value;
343
336
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);
347
340
}
348
341
return NULL ;
349
342
} else if (type == FIELD_TYPE_FLOAT) {
@@ -354,14 +347,6 @@ LOG_WINDOW("Getting string for %d,%d from %p", row, column, window);
354
347
return env->NewStringUTF (buf);
355
348
}
356
349
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 ;
365
350
}
366
351
}
367
352
@@ -410,12 +395,27 @@ LOG_WINDOW("Copying string for %d,%d from %p", row, column, window);
410
395
uint32_t size = field.data .buffer .size ;
411
396
if (size > 0 ) {
412
397
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 =
10000
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);
416
408
} else {
417
- memcpy (dst, (jchar const *)window-> offsetToPtr (field. data . buffer . offset ), size );
409
+ memcpy (dst, (jchar const *)result, strSize * 2 );
418
410
}
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
+ // }
419
419
420
420
421
421
// #if WINDOW_STORAGE_UTF8
@@ -498,9 +498,10 @@ LOG_WINDOW("Getting double for %d,%d from %p", row, column, window);
498
498
#if WINDOW_STORAGE_UTF8
499
499
return strtod ((char const *)window->offsetToPtr (field.data .buffer .offset ), NULL );
500
500
#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);
504
505
#endif
505
506
} else {
506
507
return 0.0 ;
@@ -699,6 +700,7 @@ static JNINativeMethod sMethods[] =
699
700
{" getBlob_native" , " (II)[B" , (void *)getBlob_native},
700
701
{" isBlob_native" , " (II)Z" , (void *)isBlob_native},
701
702
{" getString_native" , " (II)Ljava/lang/String;" , (void *)getString_native},
703
+ // {"getString_native", "(II)[B", (void *)getString_native},
702
704
{" copyStringToBuffer_native" , " (IIILandroid/database/CharArrayBuffer;)[C" , (void *)copyStringToBuffer_native},
703
705
{" getDouble_native" , " (II)D" , (void *)getDouble_native},
704
706
{" isNull_native" , " (II)Z" , (void *)isNull_native},
0 commit comments