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: Mac Character encoding



I don't know if this will help, but I also had to struggle with character encodings. I had a lot of trouble before I settled on UTF-16, then I had more trouble before I figured out how to master the byte-order mark at the front of UTF-16 files. At the bottom is the code I wrote long ago to read in and write out files in UTF-16. For what it's worth, since I figured this out I haven't had any trouble with any character encoding at all because UTF-16 supports just about every character in the world (right?).

peace
Nicholas







// dumps the contents of the file into a string
// ONLY WORKS WITH UTF-16 FILES INCLUDING A BYTE ORDER MARK
// the BOM is two bytes at the front of a UNICODE UTF-16 file
// which indicate... well, the byte order used in the file.
// the BOM will indicate either big-endian or little-endian.
// the BOM is not returned as a character from the file so long
// as we explicitly tell Java to interpret the file as UTF-16
protected static String dumpFile( File f ) {
StringBuffer ret = new StringBuffer();
try {
InputStreamReader reader = new InputStreamReader( new FileInputStream( f ), "UTF-16" );
for( int c = reader.read(); c != -1; c = reader.read() ) {
ret.append( (char) c );
}
} catch( Exception e ) {
Lex.out.error( "Error while trying to read file \"" + f.getName() + "\": " + e.toString() );
}
return ret.toString();
}


// writes an asset object to a given file
// ONLY WORKS WRITING OUT UTF-16 FILES INCLUDING A BYTE ORDER MARK
// explicitly written to work with UTF 16 files only
protected void writeAssetToFile( Asset asset, File f ) {
try {
FileOutputStream outStream = new FileOutputStream( f );
outStream.write( asset.getXmlDescription().getBytes( "UTF-16" ) );
outStream.close();
} catch( FileNotFoundException fnfe ) {
Lex.out.error( "Could not write asset \"" + asset.getName() + "\" to file because the output file could not be found: " + fnfe );
} catch( IOException ioe ) {
Lex.out.error( "Could not write asset \"" + asset.getName() + "\" to file because an I/O error occured: " + ioe );
}
}


_______________________________________________
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: 
 >Re: Mac Character encoding (From: Greg Guerin <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.