001: /*
002: * $Id: ObjectType.java,v 1.2 2002/07/02 21:04:57 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.ByteArrayOutputStream;
045: import java.io.DataInput;
046: import java.io.DataOutput;
047: import java.io.IOException;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052: import java.io.Serializable;
053:
054: import org.axiondb.DataType;
055:
056: /**
057: * A {@link DataType}representing a {@link Serializable}object value.
058: *
059: * @version $Revision: 1.2 $ $Date: 2002/07/02 21:04:57 $
060: * @author James Strachan
061: */
062: public class ObjectType extends BaseDataType {
063:
064: public ObjectType() {
065: }
066:
067: public int getJdbcType() {
068: return java.sql.Types.JAVA_OBJECT;
069: }
070:
071: public String getPreferredValueClassName() {
072: return "java.lang.Object";
073: }
074:
075: /**
076: * Returns <code>"string"</code>
077: *
078: * @return <code>"string"</code>
079: */
080: public String toString() {
081: return "Object";
082: }
083:
084: /**
085: * Returns <code>true</code> iff <i>value </i> is <code>null</code> or a
086: * {@link Serializable}.object
087: */
088: public boolean accepts(Object value) {
089: return value == null || value instanceof Serializable;
090: }
091:
092: /**
093: * Returns a {@link String}converted from the given <i>value </i>, or throws
094: * {@link IllegalArgumentException}if the given <i>value </i> isn't
095: * {@link #accepts acceptable}.
096: */
097: public Object convert(Object value) throws IllegalArgumentException {
098: if (value == null) {
099: return null;
100: } else if (value instanceof Serializable) {
101: return value;
102: } else {
103: throw new IllegalArgumentException(
104: "The object must implement Serializable: " + value);
105: }
106: }
107:
108: public int getColumnDisplaySize() {
109: return 0;
110: }
111:
112: public boolean supportsSuccessor() {
113: return false;
114: }
115:
116: public Object successor(Object value)
117: throws IllegalArgumentException {
118: throw new UnsupportedOperationException();
119: }
120:
121: /**
122: * @see #write
123: */
124: public Object read(DataInput in) throws IOException {
125: try {
126: ObjectInput objectInput = null;
127: if (in instanceof ObjectInput) {
128: objectInput = (ObjectInput) in;
129: } else {
130: int size = in.readInt();
131: byte[] data = new byte[size];
132: in.readFully(data);
133: ByteArrayInputStream buffer = new ByteArrayInputStream(
134: data);
135: objectInput = new ObjectInputStream(buffer);
136: }
137: return objectInput.readObject();
138: } catch (ClassNotFoundException e) {
139: throw new IOException("ClassNotFound: " + e.getMessage());
140: }
141: }
142:
143: /**
144: * Writes the given <i>value </i> to the given <code>DataOutput</code>.
145: * <code>Null</code> values are written as <code>"null", false</code>, the
146: * non-null String <code>"null"</code> is written as <code>"null", true</code>,
147: * all other values are written directly.
148: *
149: * @param value the value to write, which must be {@link #accepts acceptable}
150: */
151: public void write(Object value, DataOutput out) throws IOException {
152: if (out instanceof ObjectOutput) {
153: ObjectOutput objectOutput = (ObjectOutput) out;
154: objectOutput.writeObject(value);
155: } else {
156: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
157: ObjectOutputStream objectOut = new ObjectOutputStream(
158: buffer);
159: objectOut.writeObject(value);
160: objectOut.close();
161: byte[] data = buffer.toByteArray();
162: out.writeInt(data.length);
163: out.write(data);
164: }
165: }
166:
167: public DataType makeNewInstance() {
168: return new ObjectType();
169: }
170:
171: private static final long serialVersionUID = -8691318615765776179L;
172: }
|