public static Datatype readFileType(File file) throws IOException {
BinaryFile rf = new BinaryFile( file, "r" );
try {
- Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
- return (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+ return (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
} finally {
rf.close();
}
public static Object readFile(File file, Binding binding) throws IOException {
BinaryFile rf = new BinaryFile( file, "r" );
try {
- Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
- Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+ Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
if (type.equals(binding.type())) {
return Bindings.getSerializerUnchecked( binding ).deserialize(rf);
rf.close();
}
}
+
+ 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();
+ }
+ }
/**
* Read a file to an object.
public static void readFile(File file, RecordBinding binding, Object dst) throws IOException {
BinaryFile rf = new BinaryFile( file, "r" );
try {
- Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
- Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+ Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
if (type.equals(binding.type())) {
Serializer s = Bindings.getSerializerUnchecked( binding );
*/
public static Object readFile(InputStream is, Binding binding) throws IOException {
BinaryReadable readable = InputStreamReadable.readFully( is );
- Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
- Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
+ Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( readable );
if (!type.equals(binding.type())) {
try {
*/
public static Object readFile(InputStream is, long streamLength, Binding binding) throws IOException {
BinaryReadable readable = new InputStreamReadable( is, streamLength );
- Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
- Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
+ Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( readable );
if (!type.equals(binding.type())) {
try {