Is DatatypeConverter thread safe?
My reading of the source code is that this implementation is thread safe.
-
The method
parseBase64Binary
calls a methodparseBase64Binary
on a shared objectDatatypeConverterImpl
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 methodstatic
_parseBase64Binary
. -
The method
_parseBase64Binary
uses its input (which is immutable) and local variables related to stream-limited objects. The only exception is the variabledecodeMap
, which is an arrayprivate 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.)
source to share
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.
source to share