Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToBytearray.java

View differences:

CoerceToBytearray.java
23 23
 */
24 24
package org.gvsig.tools.dataTypes.impl.coercion;
25 25

  
26
import java.nio.ByteBuffer;
27
import java.util.Date;
28
import org.gvsig.tools.dataTypes.AbstractCoercion;
29
import org.gvsig.tools.dataTypes.CoercionContext;
26 30
import org.gvsig.tools.dataTypes.CoercionException;
27
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
31
import org.gvsig.tools.dataTypes.DataTypesManager;
28 32

  
29
public class CoerceToBytearray implements Coercion {
33
public class CoerceToBytearray extends AbstractCoercion {
30 34

  
31
    @Override
32
    public Object coerce(Object value) throws CoercionException {
33
        if (value == null) {
34
            return null;
35
        }
35
  @Override
36
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
37
    if (value == null || value instanceof Byte[]) {
38
      return value;
39
    }
36 40

  
37
        try {
38
            if ( value instanceof Byte[] ) {
39
                return value;
40
            }
41
            if( value instanceof CharSequence ) {
42
                return value.toString().getBytes();
43
            }
44
        } catch (Exception e) {
45
            throw new CoercionException(e);
41
    try {
42
      if (value instanceof CharSequence) {
43
        return value.toString().getBytes();
44
      }
45
      if( value instanceof Number ) {
46
        if( value instanceof Short ) {
47
          ByteBuffer buffer = ByteBuffer.allocate(2);
48
          buffer.putShort((short) value);
49
          return buffer.array();
50
          
51
        } else if( value instanceof Integer ) {
52
          ByteBuffer buffer = ByteBuffer.allocate(4);
53
          buffer.putInt((int) value);
54
          return buffer.array();
55
          
56
        } else if( value instanceof Long ) {
57
          ByteBuffer buffer = ByteBuffer.allocate(8);
58
          buffer.putLong((long) value);
59
          return buffer.array();
60
          
61
        } else if( value instanceof Float ) {
62
          ByteBuffer buffer = ByteBuffer.allocate(4);
63
          buffer.putFloat((float) value);
64
          return buffer.array();
65
          
66
        } else {
67
          ByteBuffer buffer = ByteBuffer.allocate(8);
68
          buffer.putDouble((double) value);
69
          return buffer.array();
70
          
46 71
        }
47
        throw new CoercionException("Can't coerce '"+value.getClass().getName()+"' to byte[].");
48

  
72
  
73
      } else if( value instanceof Date ) {
74
          ByteBuffer buffer = ByteBuffer.allocate(8);
75
          buffer.putLong(((Date)value).getTime());
76
          return buffer.array();
77
      }
78
      
79
    } catch (Exception e) {
80
      throw new CoercionException(e);
49 81
    }
82
    throw new CoercionException("Can't coerce '" + value.getClass().getName() + "' to byte[].");
50 83

  
84
  }
85

  
51 86
}

Also available in: Unified diff