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
Contributor
Posts: 28
Registered: ‎06-02-2010
My Device: 9530
My Carrier: ATnT

ExtendedFileConnection.create() failing

[ Edited ]

Hi,

 

OS: >= 4.5.0, Testing on 8310, 8900, 9000 and Storm

 

I am trying to write some binary data (png images received over http) into files. Some of the files are saved without any issues. But after around 40 files the create() is crashing without throwing any exception. I am observing this in selective devices such as Storm, 9000 Bold etc and never in any of the simulators. The devices I am testing with has plenty of free memory and the images are not more than 3KB each.

 

Part of the code is given below:

 

String nextLuId = "27";

if (img != null) {
    final String imageFile =
        new StringBuffer(nextLuId).append("oi.png").toString();
    try { FileManager.writeFile(img, imageFile); }
    catch (DataStoreException e) {
        throw new IOException(e.getMessage()); };
    }
}

class FileManager
{
    private static final String pathPrefix =
        System.getProperty("fileconn.dir.photos"); 

    public static void writeFile(byte[] data, String fileName) throws DataStoreException
    {
        ExtendedFileConnection fconn = null;
        String fileNamePath =
            new StringBuffer(pathPrefix).append(fileName).toString();

        try { fconn =
            (ExtendedFileConnection) Connector.open(fileNamePath, Connector.READ_WRITE); }
        catch (IOException e) { throw new DataStoreException("Error opening file"); }

        // Flushing the file by recreating it
        if (fconn.exists()) {
        	try { fconn.delete(); }
        	catch (IOException e) { throw new DataStoreException("Error deleting file"); }
        }

        try { fconn.create(); }
        catch (IOException e) { throw new DataStoreException(new StringBuffer("Error creating file : ").append(e.getMessage()).toString()); }
        catch (SecurityException e)  { throw new DataStoreException(new StringBuffer("Error creating file : ").append(e.getMessage()).toString()); }
        catch (IllegalModeException e)  { throw new DataStoreException(new StringBuffer("Error creating file : ").append(e.getMessage()).toString()); }
        
        OutputStream out = null;
        try { out = fconn.openOutputStream(); }
        catch (IOException e) { throw new DataStoreException("Error opening output stream"); }

        try { out.write(data); out.flush(); out.close(); }
        catch (IOException e) { throw new DataStoreException("Error writing to output stream"); }

        try { fconn.close(); }
        catch (IOException e) { throw new DataStoreException("Error closing file"); }
	}
};

 

 

Where am I going wrong? Please help.

 

TIA

Bikram

Contributor
Posts: 28
Registered: ‎06-02-2010
My Device: 9530
My Carrier: ATnT

Re: ExtendedFileConnection.create() failing

I am being able to narrow it down a bit farther -- now I can see that, the following code is throwing "File system out of resources" exception.

fconn.create();

Any ideas when and how this could result. And why specially for only a few devices. The testing devices has plenty of spaces.

 

TIA

Bikram

Contributor
Posts: 28
Registered: ‎06-02-2010
My Device: 9530
My Carrier: ATnT

Re: ExtendedFileConnection.create() failing

I figured it out. I was also opening these files else where and the stream/connections were not closed and I ran out of max simultaneous open connections.

 

The post below helped:

 

http://supportforums.blackberry.com/t5/Java-Development/File-System-Out-of-Resources/m-p/101398#M114...

 

How do I mark a thread as cosed?

Contributor
Posts: 15
Registered: ‎04-19-2011
My Device: Torch

Re: ExtendedFileConnection.create() failing

U dont close the thread you accept the right solution.thats it.

Developer
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: ExtendedFileConnection.create() failing

try
{
//No need to write this line:  String fileNamePath =
            new StringBuffer(pathPrefix).append(fileName).toString();

FileConnection fc1=(FileConnection)Connector.open(pathPrefix+fileName);
  if(fc.exists())
         fc.truncate(0);//It deletes the data in the file.
  else
        fc.create();
  OutputStream outputSream=fc.openOutputStream();
   outputSream.write(data);
   outputSream.close();
    fc1.close();
    outputStream.close();
 }
catch(Exception e)
{
         e.printStackTrace();
} 	

 place this at:

 

public static void writeFile(byte[] data, String fileName)
    {
           //place code here;
     }

 Enough. you can get data in required data into required file.

==================================================================================

Feel free to click THUMB symbol;

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.