+
+ public static Object readFileTypeAdapting(File file, Binding binding) throws IOException {
+ BinaryFile rf = new BinaryFile( file, "r" );
+ try {
+ Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
+
+ if (type.equals(binding.type())) {
+ return Bindings.getSerializerUnchecked( binding ).deserialize(rf);
+ } else {
+ try {
+ Binding fileContentBinding = Bindings.getMutableBinding(type);
+ Adapter adapter = Bindings.getTypeAdapter(fileContentBinding, binding);
+ Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize(rf);
+ return adapter.adapt( value );
+ } catch (AdapterConstructionException e) {
+ throw new IOException(e);
+ } catch (AdaptException e) {
+ throw new IOException(e);
+ }
+ }
+ } finally {
+ rf.close();
+ }
+ }