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
patilajayr
Posts: 404
Registered: ‎07-28-2010
My Device: 8000
My Carrier: hi

Getting Out of OutOfMemoryError

hello friends, 

                       I am downloding one zip file from sever which is of size 4.20MB. it downloaded successfully. but when i am trying to extract that zip file it give me OutOfMemoryError  when that file extract that size will be 19MB. 

 

                       how i solve that issue? becuase when my file size of 2.5MB it works fine. but when i am trying to unzip that  4.20MB file i am getting that OutOfMemoryError. what should i do? reduce that size? or have any onther solution?

 

i am attaching code bellow.

 

 public static  String readZipFile(byte[] arrByteResp) {
		try {
			System.out.println("#####################################################");
			 
			ZipArchive zf = new ZipArchive(arrByteResp);
 
		      System.out.println("ZipFile");
		      Enumeration  entries = zf.entries();
		      System.out.println("Total Entries :" );

		      while (entries.hasMoreElements()) {
		        ZipEntry ze = (ZipEntry) entries.nextElement();
		        System.out.println("Read " + ze.getName() + "?" + ze.getSize());
		        
		        if(ze.getName().equals("database.sqlite")) {
		        	System.out.println("Read");
		        	
		        	if(ze.getName().indexOf("/")== -1) {
		        		try{
		        		System.out.println("Reading ZIP file :" + ze.getName());
				        InputStreamReader _in =  new InputStreamReader(zf.getInputStream(ze));
 		                int len = (int) ze.getSize(); 
		                System.out.println("Length-->" + len);
 		                int count =0;
		                byte[] data = null;
		                if(len > 999999 )
		                	data =  new byte[999999];
		                else 
		                	data =  new byte[len];
		                int b = -1;
		                String str ="";
		               
		                while(true) {
		                	System.out.println("000000000000000000000000000000000000000000");
		                	 b = _in.read();
		                	 if (b  == -1) break;
		                	 data[count] = (byte) b;
		                	 if(data.length -1 ==  count) {
		                		 str += new String (data);
		                		 count = -1;
 		                	 }
			                	count++;
		                }
		                System.out.println("***********************************************");
		                System.out.println("UNZIPPING FINISH");
		                System.out.println("UNZIPPING FINISH");
		                if(str.getBytes().length< len)
		                	str += new String (data);
 
		                _in.close();
		               
		                return str;
		        		}
		        		catch(Exception e) {
		        			Dialog.alert("Exception"+e);
		        			System.out.println("Exception inreadZipFile " + e.getMessage());
		        		}
		        	}
		        }
		       }
		    } catch (IOException e) {
		    	Dialog.alert("Exception"+e);
		      System.out.println("exception in readZipFile");
		    	e.printStackTrace();
		      
		    }	
		    return "";
	}    

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Getting Out of OutOfMemoryError

19mb is a lot of memory for a Blackberry, especially older models.

 

What device are you testing this on?

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting Out of OutOfMemoryError

I agree with RexDoug's comments.

 

In addition, I thin there are going to be some issues with attempting to copy an sqlite database out of the zip archive, convert it t a String and then write the String out and expect to get a useful SQLite database.  You should probably, once you have the ZipEntry 'open' just do some read/write processing copying the input stream out to the SD Card.  if you did this, you would never have the whole file in memory and I think your memory issue will disappear.  And if you write it using a byte array rather than attempting t convert it to a String, you will not have any conversion areas.  Win all round.

 

Let us know how you get on with this. 

Please use plain text.
Developer
patilajayr
Posts: 404
Registered: ‎07-28-2010
My Device: 8000
My Carrier: hi

Re: Getting Out of OutOfMemoryError

[ Edited ]

Thanks for reply me.

                      i am already creating my databse in SD card.

                     can you give me some sorce code how should i do that? i attacted my code above . what exactly i change? please help me.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting Out of OutOfMemoryError

The source code you have given does not include writing the file to the SD Card, so I'm not sure what to change, but I do know the current approach of reading everything before writing the file will give you problems. 

 

Anyway, rather than give you code, following is a description in 'words' of the process required.  .

 

Put simply, you would open the input stream from the zip file, open the output stream to the SD card, then have a loop with each iteration of the loop reading a small number (say 1000) bytes from the zip file and writing the bytes read to the SD Card.  Once all the bytes have been read, you exit the loop, and you close both. 

 

Have a go at coding that.

 

BTW the code you have given us above is not efficient and may suffer from issues with byte to character conversions.  You are gong to be throwing it away in this case, but if you have used similar code in other places, I recommend you review it closely.  I would suggest you look at IOUtilities, streamToBytes(..) as a way to read all the input and I would also suggest you look at the encoding that you can specify when converting from a byte array to a String.   

Please use plain text.