| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
From a forthcoming document... don't ask when it will be finished :-) Working With Strings Accessing or creating Java Strings with the JNI is no different than on any other platform. As in Java, CoreFoundation and Cocoa string objects represent an array of 16-bit Unicode characters; Unicode is therefore the preferred character type when translating between Java Strings and other native string types on Mac OS X. This is worth mentioning because many of the older JNI tutorials use UTF-8 characters when dealing with Java Strings. Creating Native Strings From Java Strings Creating native strings (NSString/CFString) is equally simple. Listings 3 and 4 demonstrate both techniques. Note the call to ReleaseStringChars in both cases; this call is necessary to notify the JVM that you no longer require access to the Java string. The Java Native Interface: Programmer's Guide and Specification states: "Forgetting to call the ReleaseStringChars function may cause either the jstring object to be pinned indefinitely, leading to memory fragmentation, or the C copy to be retained indefinitely, a memory leak." Listing 3: Creating an NSString from a Java String const jchar *chars = (*env)->GetStringChars(env, my_jstring, NULL); NSString *myNSString = [NSString stringWithCharacters:(unichar *)chars length:(*env)->GetStringLength(env, my_jstring)]; (*env)->ReleaseStringChars(env, my_jstring, chars); Listing 4: Creating a CFString from a Java String const jchar *chars = (*env)->GetStringChars(env, my_jstring, NULL); CFStringRef myCFString = CFStringCreateWithCharacters (kCFAllocatorDefault, chars, (*env)->GetStringLength(env, my_jstring)); (*env)->ReleaseStringChars(env, my_jstring, chars); ---------------------------------------------- Matt Drance DTS Engineer Java - Dashboard - Address Book On Jan 18, 2006, at 4:42 AM, Michael Hall wrote:
|
_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden This email sent to email@hidden
| References: | |
| >Java String to CFUrlRef via JNI? (From: Mike P <email@hidden>) | |
| >Re: Java String to CFUrlRef via JNI? (From: Michael Hall <email@hidden>) |
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.