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: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi
Accepted Solution

How to reuse stringBuffer..

[ Edited ]

I have this:

 

StringBuffer sb = null;
String value;

Loop{ value = g4ttu6n; sb = new StringBuffer(value);
....
Send(sb);
}

And I was wondering: Is there a way to do something like this:

 

String value;
StringBuffer sb = new StringBuffer(value);

Loop
{
   value = grfdt5tgr;
   Send(sb);
}

 I'm not sure if it's possible..

 

Thanks

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to reuse stringBuffer..

setLength(..) and append(...) appear to be the methods you will want to use, specifically setLength(0) to clear it. 

Developer
Posts: 152
Registered: ‎12-03-2012
My Device: PP
My Carrier: base(kpn)/BE, t-mobile/PL

Re: How to reuse stringBuffer..


Pisun wrote:

I have this:

 

StringBuffer sb = null;
String value;

Loop{ value = g4ttu6n; sb = new StringBuffer(value);
....
Send(sb);
}

And I was wondering: Is there a way to do something like this:

 

String value;
StringBuffer sb = new StringBuffer(value);

Loop
{
   value = grfdt5tgr;
   Send(sb);
}

 

1) 

StringBuffer sb = new StringBuffer();

Loop {
sb.delete(0, sb.length());

sb.append(value);

Send(sb);
}

2) 

StringBuffer sb = new StringBuffer();

Loop {
sb.setLength(0);

sb.append(value);

Send(sb);
}

 

--
Regards,
Pawel Gorny
my apps: http://pawelgorny.com/
PGpgp, Your Reply, Anagrammatist, Texas Hold'em Odds Calculator, Contacts to CSV
If your issue has been solved, please resolve it by marking "Accept as Solution"
Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: How to reuse stringBuffer..

[ Edited ]

Wow! That's an awesome example! Thanks. Can I do the same with char[] that has dynamic size? like

 

int size;
char[] chArray = new char[size];

Loop
{
  size = 3;
  chArray[0] = blah1;
  chArray[1] = blah2;
  System.arraycopy(blah3, 0, chArray, 2, blah3.length);
Send(chArray); }

 And size can change in a next loop... 

Is it possible to do something similar on this...?

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to reuse stringBuffer..

No - the char array, like all Java arrays, is a fixed size.

 

In this specific circumstance, I am not clear on what the advantage of retaining the 'holding object' is.  For example instead of this:

 

StringBuffer sb = new StringBuffer();

Loop {

sb.setLength(0);

sb.append(value);

Send(sb);

}

 

it would be more efficient to change the Send method to send a String and so just use:

 

StringBuffer sb = new StringBuffer();

Loop {

Send(value);

}

 

Perhaps you could explain what you are trying to do and why and perhaps we can suggest an optimal approach. 

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: How to reuse stringBuffer..

Well.. ok.. I'l try (I'm not very goo at explaining)

:No:

I have a loop that repeat itself during runtime. During each loop the code creates few stringBuffers (I have eliminated them so shouldn't be a problem) and char arrays with dynamic size... The loop repeat itself until I'm getting stackoverflow... When I changed the code ( now it is reusing stringBuffer instead of creating it in every new loop) , I managed to get further  during runtime.. So my idea was to remove all variables (such as char arrays) from inside the loop and reuse them...

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to reuse stringBuffer..

I would review your code, from your description, you appear to be focusing on the wrong area.

 

for ( int i = 0; i < 100; i++ ) {

StringBuffer sb = new StringBuffer);

// other code

}

 

This code creates 100 StringBuffers, but it also destroys them.  In each iteration of the loop, the dynamically allocated StringBuffer is freed.

 

We have already reviewed some of you code that was recursive, and that was what caused the stack overflow - see

http://supportforums.blackberry.com/t5/Java-Development/How-to-understand-StackTrace-and-fix-StackOv...

 

Recursive calls are the most likely cause of a StackOverflow, so you should focus on getting rid of this rather than optimizing things like StringBuffer usage. 

 

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: How to reuse stringBuffer..

Yes, I thought about changing the code... At the moment three functions calling each other makes an infinite loop.. But even if I would change that to three separate functions they would have to return some value and after the first function  it would go to the next function. And after a third (last) function is implemented, it would go back to the first one..... That makes a loop again...

:Wacko:

Is there a way round that..?

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to reuse stringBuffer..

Sorry to be picky, but you are not coding an infinite loop.  This is an infinite loop:

 

For ( int i = 1; i > 0; i++ ) {

// ...

}

 

What you are using is recursion which you need to flatten out. 

 

It is usually possible to flatten out recursion but it typically involves some additional control mechanism.  A good example of this is a state machine.  As an example, say you currently have this logic:

 

public void a(..) {

// .. some processing

{

if ( somethingA ) {

b(..)

} else {

a(..);

)

{

 

public void b(..) {

// some processing

if ( somethingB) {

c(...);

} else {

a(..);

)

}

 

public void c(..) {

// some processing

a(..); // To continue the loop

}

 

This is using recursion.  But you can flatten this out by doing this:

 

int state = 0; // 1 means need to do b and 2 means need to do c

while ( .. ) {

a(..);

if ( state = 1 ) {

b(..);

}

if ( state = 2 ) {

c(..);

}

}

 

and then the methods become:

 

public void a(..) {

// .. some processing

{

if ( somethingA ) {

state = 1;

} else {

state = 0;

}

{

 

public void b(..) {

// some processing

if ( somethingB) {

state = 2;

} else {

state = 0;

)

 

public void c(..) {

// some processing

state = 0;

}

 

This is really programming rather than BlackBerry Java, so  the techniques involved are probably better explained in other places rather than on a BlackBerry Java forum.  . 

 

But the summary of the situation at present is your current design will never work properly - you need to change it to make sure that it does not chew up memory.  Calling recursively uses up memory - every call to a method uses up some of the stack as it saves the current status.  This does NOT happen if you call one, have it return and then call another, have it return and call the third.  In that circumstance you only ever have one method on the stack.  And this avoids the problem.

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: How to reuse stringBuffer..

Ok... I was trying to avoid it but looks like I will HAVE to rewrite that class.....  

:smileyfrustrated:

Thanks for clear explanation Peter!