8

I'm implementing my custom filter:

public class MyFilter implements javax.servlet.Filter

Which should I use in this doFilter method - StringBuffer or StringBuilder?

I would like to use it in this way:

StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(MY_CODE_HERE);
response.sendRedirect(stringBuffer.toString());

or...

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(MY_CODE_HERE);
response.sendRedirect(stringBuilder.toString());

I know that StringBuffer is thread safe, but would a StringBuilder be enough?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Piotr0123456
  • 638
  • 3
  • 7
  • 21
  • http://stackoverflow.com/questions/12961254/stringbuffer-stringbuilder-when-to-use?lq=1 – kosa Jul 29 '13 at 14:00
  • 1
    As the Javadoc suggests, you should always use StringBuilder if you can. Note: StringBuffer is only thread safe if you only perform one operation on it, it not thread safe for multiple operations making it pretty useless IMHO. – Peter Lawrey Jul 29 '13 at 14:26
  • 1
    This doesn't feel like a duplicate at all - this refers to the implications of a `StringBuffer` in a filter, as opposed to what the difference between the two actually *is*. – Makoto Jan 23 '14 at 20:47

5 Answers5

10

Local variables are thread-safe and variables declared inside the doFilter() method will be thread safe. Use StringBuilder for your purpose, as you shouldn't unnecessarily incur the overhead of synchronization used in StringBuffer.

Moreover , The Servlet request and response objects are created afresh for every new request and response and so by their nature they are thread safe. The doFilter() method will be executed in separate threads for each request.

Suggested Reading:

  1. Why are local variables thread safe in Java.
  2. StringBuilder and StringBuffer in Java
  3. servlet-filters tag wiki
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
3

Since it will be a local variable and not shared by threads, StringBuilder can be used.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

StringBuilder is a better choice as it is faster because of its non-synchronized nature. ServletFilter doFilter for each request run in its own thread so you don;t need a synchronized datastructure. And if it is simply a local varibale then StringBuilder is the choice.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

StringBuilder is designed as drop-in replacement for StringBuffer without the synchronization.

Simply use StringBuilder unless you are really trying to share a buffer between threads. StringBuilder is the unsynchronized relative of the original synchronized StringBuffer class.

In your case StringBuilder makes perfect sense. Even in other case, consider synchronizing method then using StringBuffer

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
0

As long as it is in the method doFilter(), its thread-safe and request scoped. So, StringBuilder is the obvious choice.

Ofcourse, unless there are multiple threads spawning within that method which modify it concurrently :)

rocketboy
  • 9,573
  • 2
  • 34
  • 36