gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToBytearray.java @ 2080
History | View | Annotate | Download (2.86 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
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; |
30 |
import org.gvsig.tools.dataTypes.CoercionException; |
31 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
32 |
|
33 |
public class CoerceToBytearray extends AbstractCoercion { |
34 |
|
35 |
@Override
|
36 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
37 |
if (value == null || value instanceof Byte[]) { |
38 |
return value;
|
39 |
} |
40 |
|
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 |
|
71 |
} |
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); |
81 |
} |
82 |
throw new CoercionException("Can't coerce '" + value.getClass().getName() + "' to byte[]."); |
83 |
|
84 |
} |
85 |
|
86 |
} |