Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Java String to CFUrlRef via JNI?



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

email@hidden



On Jan 18, 2006, at 4:42 AM, Michael Hall wrote:


On Jan 18, 2006, at 5:39 AM, Mike P wrote:

I'm need to pass a sting into some c++ code and convert the string to a CFUrlRef.  Does anyone have experience doing such a thing?  It doesn't need to be passed back.  The c++ code will do what it needs without returning any value.

I think the usual approach would be to convert the string to a char or byte array which ever works better with what the native method expects.

Mike Hall        mikehall at spacestar dot net



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 _______________________________________________
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>)



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.