Is DatatypeConverter thread safe?

In particular, is the method javax.xml.bind.DatatypeConverter.parseBase64Binary(String)

thread safe?


source to share

3 answers

There is nothing in the documentation that suggests that a class is thread safe. Therefore, I recommend that you assume that it is not.

I would recommend Base64

from the Apache Commons Codec, which states it streamed in the documentation.



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

  • The method parseBase64Binary

    calls a method parseBase64Binary

    on a shared object DatatypeConverterImpl

    that is lazy created.

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

  • Analysis DatatypeConverterImpl

    shows that it has no instance variables, so there can be no thread safety issues when accessing / updating the instance state.

  • The method DatatypeConverterImpl.parseBase64Binary

    (in turn) calls the method static



  • The method _parseBase64Binary

    uses its input (which is immutable) and local variables related to stream-limited objects. The only exception is the variable decodeMap

    , which is an array private static final


  • The variable is decodeMap

    initialized and safely published when the class is initialized (static).

  • After initialization, the variable is decodeMap

    read only ever. Thus, there can be no timing problems or memory model "dangers" associated with updates.

Of course, this analysis only applies to the version of the class I'm attached to. It is possible that this method is not thread safe in other versions. (But the source code is freely available for several versions, so you can check this for the JAXP version you are using.)



By looking at the source code javax.xml.bind.DatatypeConverter.parseBase64Binary(String)

( JAXB api ) which is a static method using an immutable class as an argument.

final public class DatatypeConverter {
    // delegate to this instance of DatatypeConverter
    private static volatile DatatypeConverterInterface theConverter = null;


    public static byte[] parseBase64Binary( String lexicalXSDBase64Binary ) {
        if (theConverter == null) initConverter();
        return theConverter.parseBase64Binary( lexicalXSDBase64Binary );


    private static synchronized void initConverter() {
        theConverter = new DatatypeConverterImpl();



We can assume that it is thread safe. The initConverter () method is statically synchronized.



All Articles