001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.util;
007:
008: import java.io.ByteArrayInputStream;
009: import java.io.ByteArrayOutputStream;
010: import java.io.ObjectInputStream;
011: import java.io.ObjectOutputStream;
012: import java.sql.SQLException;
013:
014: import org.h2.constant.ErrorCode;
015: import org.h2.message.Message;
016:
017: /**
018: * Utility class for object creation and serialization.
019: */
020: public class ObjectUtils {
021:
022: public static Integer getInteger(int x) {
023: //#ifdef JDK16
024: /*
025: if(true)
026: return Integer.valueOf(x);
027: */
028: //#endif
029: //#ifdef JDK14
030: return new Integer(x); // NOPMD
031: //#endif
032: }
033:
034: public static Character getCharacter(char x) {
035: //#ifdef JDK16
036: /*
037: if(true)
038: return Character.valueOf(x);
039: */
040: //#endif
041: //#ifdef JDK14
042: return new Character(x);
043: //#endif
044: }
045:
046: public static Long getLong(long x) {
047: //#ifdef JDK16
048: /*
049: if(true)
050: return Long.valueOf(x);
051: */
052: //#endif
053: //#ifdef JDK14
054: return new Long(x); // NOPMD
055: //#endif
056: }
057:
058: public static Short getShort(short x) {
059: //#ifdef JDK16
060: /*
061: if(true)
062: return Short.valueOf(x);
063: */
064: //#endif
065: //#ifdef JDK14
066: return new Short(x); // NOPMD
067: //#endif
068: }
069:
070: public static Byte getByte(byte x) {
071: //#ifdef JDK16
072: /*
073: if(true)
074: return Byte.valueOf(x);
075: */
076: //#endif
077: //#ifdef JDK14
078: return new Byte(x); // NOPMD
079: //#endif
080: }
081:
082: public static Float getFloat(float x) {
083: //#ifdef JDK16
084: /*
085: if(true)
086: return Float.valueOf(x);
087: */
088: //#endif
089: //#ifdef JDK14
090: return new Float(x);
091: //#endif
092: }
093:
094: public static Double getDouble(double x) {
095: //#ifdef JDK16
096: /*
097: if(true)
098: return Double.valueOf(x);
099: */
100: //#endif
101: //#ifdef JDK14
102: return new Double(x);
103: //#endif
104: }
105:
106: public static byte[] serialize(Object obj) throws SQLException {
107: try {
108: ByteArrayOutputStream out = new ByteArrayOutputStream();
109: ObjectOutputStream os = new ObjectOutputStream(out);
110: os.writeObject(obj);
111: return out.toByteArray();
112: } catch (Throwable e) {
113: throw Message.getSQLException(
114: ErrorCode.SERIALIZATION_FAILED_1, new String[] { e
115: .toString() }, e);
116: }
117: }
118:
119: public static Object deserialize(byte[] data) throws SQLException {
120: try {
121: ByteArrayInputStream in = new ByteArrayInputStream(data);
122: ObjectInputStream is = new ObjectInputStream(in);
123: Object obj = is.readObject();
124: return obj;
125: } catch (Throwable e) {
126: throw Message.getSQLException(
127: ErrorCode.DESERIALIZATION_FAILED_1,
128: new String[] { e.toString() }, e);
129: }
130: }
131:
132: }
|