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
Developer
QuiteSimple
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810
Accepted Solution

net.rim.device.api.io.file.FileIOException: File system out of resources

I'm really lost here. About one from 100 customers reports this error for one of my apps (different phones, different OS). The app creates a file on SDCard (or DeviceMemory if no SDCard), the code is not executed on startup, but after the users enters the app.

I've searched the forums - this errors is caused when file connections are not properly closed. However I think that I have done it right. Could you please look at that piece of code and let me know what I'm doing wrong?

Thanks a lot

 

        try
        {
            Class classs = this.class;
            InputStream readData = classs.getResourceAsStream("new" + getExtension(type));
            byte[] docsData = IOUtilities.streamToBytes(readData);
            readData.close();

            String baseUrl;
            if (hasMediaCard())
                baseUrl = Constants.DOCS_URI_CARD;
            else
                baseUrl = Constants.DOCS_URI_STORE;

            String fileURL = baseUrl + text + getExtension(type);

            FileConnection fconn = (FileConnection)Connector.open(fileURL);
            if (!fconn.exists())
            {
                fconn.create();  // create the file if it doesn't exist
            }
            
            DataOutputStream out = fconn.openDataOutputStream();                
            for (int i = 0; i < docsData.length; i++)
            {
                out.write(docsData[i]);
            }
            out.flush();
        
            out.close();
            fconn.close();
            
            Status.show(fileURL + " created!", 2000);
            errorMessField_.setText(fileURL + " created!");
        }
        catch (Exception ex)
        {
            Status.show("Error creating " + text, 2000);
            errorMessField_.setText("Error: " + ex);
        }        
   

 



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

[ Edited ]

I would code this:

 

        FileConnection fconn = null;
        InputStream readData = null;
        DataOutputStream out = null;
        try
        {
            Class classs = this.class;
            readData = classs.getResourceAsStream("new" + getExtension(type));
            byte[] docsData = IOUtilities.streamToBytes(readData);
            readData.close();
            readData = null;

            String baseUrl;
            if (hasMediaCard())
                baseUrl = Constants.DOCS_URI_CARD;
            else
                baseUrl = Constants.DOCS_URI_STORE;

            String fileURL = baseUrl + text + getExtension(type);

            fconn = (FileConnection)Connector.open(fileURL);
            if (!fconn.exists())
            {
                fconn.create();  // create the file if it doesn't exist
            }
           
            out = fconn.openDataOutputStream();               
            for (int i = 0; i < docsData.length; i++)
            {
                out.write(docsData[i]);
            }
            out.flush();
       
            out.close();
            out = null;

            fconn.close();
            fconn = null;
           
            Status.show(fileURL + " created!", 2000);
            errorMessField_.setText(fileURL + " created!");
        }
        catch (Exception ex)
        {
            Status.show("Error creating " + text, 2000);
            errorMessField_.setText("Error: " + ex);
        }
        finally
        {
            try {
                if ( readData != null ) {
                    readData.close();
                }
            } catch (Exception e) {
            }
            try {
                if ( out != null ) {
                    out.close();
                }
            } catch (Exception e) {
            }
            try {
                if ( fconn != null ) {
                    fconn.close();
                }
            } catch (Exception e) {
            }
        }

 

Note: Code not tested, or even comiled.

Developer
shism2
Posts: 246
Registered: ‎12-13-2010
My Device: Not Specified

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

Always put the code that closes the Streams into a finally clause like peter shows in his code.

Developer
QuiteSimple
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

Thanks! That makes perfect sense, if some error occures in the try-catch block, connections would never close. Sigh, I should have figured it out by myself :smileysad:

 

One last question before I close the topic:

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is...

 

In this article stands that maximum number of file connections is 16 (OS4.7 and above). Is the maximum number for every app or for all apps in general?

If the number is for all apps - is it possible then that another app has already consumed all allowed file connections and when my app starts, it will always fail?



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry
Contributor
Airoh12
Posts: 12
Registered: ‎02-10-2013
My Device: 9320

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

What Do These Codes Means And How Do You Use Them To Stop This Problem?
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

Please ask a new question on a new Thread.

 

And can you please clarify the question.  What codes are you talking about?  What problem are you trying to stop?

Contributor
Airoh12
Posts: 12
Registered: ‎02-10-2013
My Device: 9320

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

What do these codes mean?

{
Class classs = this.class;
InputStream readData = classs.getResourceAsStream("new" + getExtension(type));
byte[] docsData = IOUtilities.streamToBytes(readData);
readData.close();

String baseUrl;
if (hasMediaCard())
baseUrl = Constants.DOCS_URI_CARD;
else
baseUrl = Constants.DOCS_URI_STORE;

String fileURL = baseUrl + text + getExtension(type);

FileConnection fconn = (FileConnection)Connector.open(fileURL);
if (!fconn.exists())
{
fconn.create(); // create the file if it doesn't exist
}

DataOutputStream out = fconn.openDataOutputStream();
for (int i = 0; i < docsData.length; i++)
{
out.write(docsData[i]);
}
out.flush();

out.close();
fconn.close();

Status.show(fileURL + " created!", 2000);
errorMessField_.setText(fileURL + " created!");
}
catch (Exception ex)
{
Status.show("Error creating " + text, 2000);
errorMessField_.setText("Error: " + ex);
}

And How Do You Use Them To Stop The Problem?
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

I am not clear what you are asking.  The word codes can mean a number of things.  Can you explain in more detail what you are asking?

 

In answer to the unanswered question from QuiteSimple, the limits are per Application and have been extended in later OS's, I think 16 actually applied to OS 4.5 and lower, the limit in newer OS is significantly higher.  But this is from memory.   

Contributor
Airoh12
Posts: 12
Registered: ‎02-10-2013
My Device: 9320

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

Ok , How Can I Get Rid Of The Problem, "net.rim.device.api.io.file.
FileIOException : File Systems Out Of Resources"
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: net.rim.device.api.io.file.FileIOException: File system out of resources

Assuming it works correctly, in my quick review, the code you have shown correctly opens and closes its connections.  I can see a number of things that I am not happy about including:

 

1) There is no 'finally' clause that makes sure that the connections are in fact closed after an Exception

 

2) I'm not sure why you use a DataOutputStream in the following code:

DataOutputStream out = fconn.openDataOutputStream();
for (int i = 0; i < docsData.length; i++)
{
out.write(docsData[i]);
}

You could use just an OutputStream and write the whole buffer out at once.

 

3) Probably the most important. 

You are doing this processing on the EventThread.  I think you will get away with this if you have small files.  For large files this will block an the OS might throw your app out.  You should do operations like this on their own Thread.  That makes advising the user problematic, but there are ways round that. 

 

But in summary, from my quick review, if this is the only code you are running, then you will not see the error.  So if you are seeing it, I suspect there is something else going on.