Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Contributor
Posts: 13
Registered: ‎06-18-2010
My Device: Z10
My Carrier: MTN

UTF enc BAMFS failing to read

[ Edited ]

I use BAMFS to create a resource file that contains both text files and *.png images into my

application.

This resource file can be replaced via a download to update things within the application.

--not important.

 

My problem is the text files, when they are read the via the following methods (from the resource):

 

 


/**
	 * Gets the config file.
	 * 
	 * @param which_cfg
	 *            the which_cfg
	 * @return the config file
	 */
	public static String getConfigFile(int which_cfg) {
		byte temp[] = getFile(which_cfg);
		StringBuffer sb = new StringBuffer(temp.length);
		for (int i = 0; i < temp.length; i++) {
			sb.append((char) temp[i]);
		}
		return sb.toString();
	}

	/**
	 * Gets the file.
	 * 
	 * @param file_id
	 *            the file_id
	 * @return the file
	 */
	public static byte[] getFile(int file_id) {
		getFileTable();
		byte[] file = new byte[1];
		try {
			DataInputStream din = null;
			switch (MVCC_MIDlet.midlet.res_file_index) {
			case 0:
				din = new DataInputStream(file.getClass().getResourceAsStream(
						RES_FILE_DEF));
				break;
			case 1:
				FileConnection filecon;
				try {
					filecon = (FileConnection) Connector.open(getFileSystem()
							+ APP_DATA_DIR + MVC_DIR + RES_FILE_DL);
					din = new DataInputStream(filecon.openDataInputStream());
				} catch (IOException ioe) {
					// #mdebug
					System.out.println("open 'getFile' file error: "
							+ ioe.toString());
					// #enddebug
				}
				break;
			}
			int size = res_files[file_id + 1] - res_files[file_id];
			int offset = res_files[file_id];
			file = new byte[size];
			din.skipBytes(offset);
			din.read(file);
			din.close();
		} catch (Exception e) {
			// #mdebug
			System.out.println("Get File error: " + e.toString());
			// #enddebug
		}
		return file;
	}

I don't get the 'special' characters from the extraction, chars like :

ñ, ð, etc... (Spanish stuff)

 

What should I 'FIX' to get a better result?

I know it's not a problem with displaying the characters as I can call something like the following with

no problems.

 

 

System.out.println("cñòö");

 

g.drawString("cñòö", x, y, Graphics.HCENTER);

  and read them fine afterwards.

 

 

It's the reading FROM the resource that gets screwed some how.

 

PS: Even when I open the Binary 'resource' file via a text editor, the characters are correct,

       So it's not the writing of the resource file either, It's definitely the reading...

 

IDE:

 

Pulsar for Mobile Java Developers. (Eclipse)

Build id: 20100218-1602

 

Eclipse Blackberry Plugin (Version:  1.1.2.201004161203-16):

BlackBerry Java SDK 

 

Version:  5.0.0.25

 

 

 

Help will be GREATLY appreciated 

 

BlackBerry Development Advisor
Posts: 15,694
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: UTF enc BAMFS failing to read

By default, a string will use the ISO-8859-1 character set.   If you are storing the characters in UTF-8 format (or UTF-16BE) you'll need to specify it in the string's constructor, otherwise the characters will not be correct.  Refer to the JavaDocs for the String class for details.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Contributor
Posts: 13
Registered: ‎06-18-2010
My Device: Z10
My Carrier: MTN

Re: UTF enc BAMFS failing to read

No, like I said that is not the problem, it's the reading of the file from the resources

(Which is a binary file of images and text files).

 

So I'm using DataInputStream as suggested with the java file auto creation from the Bamfs application.

 

 

public static byte[] getFile(int file_id) {
		getFileTable();
		byte[] file = new byte[1];
		try {
			DataInputStream din = null;
			switch (MVCC_MIDlet.midlet.res_file_index) {
			case 0:
				din = new DataInputStream(file.getClass().getResourceAsStream(
						RES_FILE_DEF));
				break;
			case 1:
				FileConnection filecon;
				try {
					filecon = (FileConnection) Connector.open(getFileSystem()
							+ APP_DATA_DIR + MVC_DIR + RES_FILE_DL);
					din = new DataInputStream(filecon.openDataInputStream());
				} catch (IOException ioe) {
					// #mdebug
					System.out.println("open 'getFile' file error: "
							+ ioe.toString());
					// #enddebug
				}
				break;
			}
			int size = res_files[file_id + 1] - res_files[file_id];
			int offset = res_files[file_id];
			file = new byte[size];
			din.skipBytes(offset);
			din.read(file);
			din.close();
		} catch (Exception e) {
			// #mdebug
			System.out.println("Get File error: " + e.toString());
			// #enddebug
		}
		return file;
	}

 How can I make that input stream read it as UTF 8 ?

InputStreamReader does not have the  skipbytes(), read(wholePortion), etc that I need.

 

BlackBerry Development Advisor
Posts: 15,694
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: UTF enc BAMFS failing to read

You can specify the character format used when creating the InputStreamReader.  Use the InputStreamReader(InputStream is, String enc) constructor.

 

You can then use its skip method to skip over a specified number of characters.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker