You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mime4j-dev@james.apache.org by Markus Wiederkehr <ma...@gmail.com> on 2009/08/17 16:22:40 UTC

ThreadLocal in CodecUtil.copy()?

CodecUtil.copy(InputStream, OutputStream) allocates a new byte array
with 1024 bytes on each invocation.

I wonder if we could use a ThreadLocal instead so that the buffer
could be reused within the same thread? Buffers would automatically be
freed once their thread terminates.. I'd reduce the size to 512 bytes
though.

This way methods like DecoderUtil.decodeBase64() could use copy() even
if they need only copy a few bytes.

Opinions?

Markus

Re: ThreadLocal in CodecUtil.copy()?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, Aug 17, 2009 at 04:22:40PM +0200, Markus Wiederkehr wrote:
> CodecUtil.copy(InputStream, OutputStream) allocates a new byte array
> with 1024 bytes on each invocation.
> 
> I wonder if we could use a ThreadLocal instead so that the buffer
> could be reused within the same thread? Buffers would automatically be
> freed once their thread terminates.. I'd reduce the size to 512 bytes
> though.
> 
> This way methods like DecoderUtil.decodeBase64() could use copy() even
> if they need only copy a few bytes.
> 
> Opinions?
> 
> Markus

Here is my personal opinion on the matter. The use of ThreadLocals in a reneral
purpose library ought to be avoided. They can (and usually do) cause enourmous
amount of grief if misused.

ThreadLocals should be used only if there is a reliable way of unsetting them,
for instance by using a try - finally construct in a servlet filter.

Take it for what it is worth to you.

Oleg