Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DatatypeConverter thread-safe?

In particular, is the method javax.xml.bind.DatatypeConverter.parseBase64Binary(String) thread-safe?

like image 599
Alex Avatar asked Mar 18 '23 06:03

Alex


2 Answers

There is nothing in the documentation that suggests the class is thread-safe. Therefore I recommend you assume it isn't.

I'd recommend Base64 from Apache Commons Codec which states it's thread-safe in the documentation.

like image 53
Duncan Jones Avatar answered Mar 26 '23 03:03

Duncan Jones


My reading of the source code is that that implementation is thread-safe.

  • The parseBase64Binary method calls a parseBase64Binary method on a shared DatatypeConverterImpl object that is lazily created.

  • It is easy to see that the lazy creation is thread safe. (The code is in another Answer ...)

  • An examination of DatatypeConverterImpl shows that it has no instance variables, so there cannot be thread-safety concerns over access / update to the state of the instance.

  • The DatatypeConverterImpl.parseBase64Binary method (in turn) calls the static _parseBase64Binary method.

  • The _parseBase64Binary method uses its input (which is immutable) and local variables that refer to thread-confined objects. The only exception is the decodeMap variable which is a private static final array.

  • The decodeMap variable is initialized and safely published during class (static) initialization.

  • Once initialized, the decodeMap variable is only ever read. Thus, there can be no synchronization issues or memory model "hazards" related to updates.

Of course, this analysis only applies to the version of the class that I linked to. It is conceivable that the method is not thread-safe in other versions. (But the source code is freely available for multiple versions, so you should be able to check this for the version of JAXP that you are using.)

like image 32
Stephen C Avatar answered Mar 26 '23 02:03

Stephen C