001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset.datatype;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.ITable;
028: import org.dbunit.util.Base64;
029:
030: import java.io.*;
031: import java.net.MalformedURLException;
032: import java.net.URL;
033: import java.sql.Blob;
034: import java.sql.PreparedStatement;
035: import java.sql.ResultSet;
036: import java.sql.SQLException;
037:
038: /**
039: * @author Manuel Laflamme
040: * @version $Revision: 554 $
041: * @since Mar 20, 2002
042: */
043: public class BytesDataType extends AbstractDataType {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(BytesDataType.class);
050:
051: private static final int MAX_URI_LENGTH = 256;
052:
053: BytesDataType(String name, int sqlType) {
054: super (name, sqlType, byte[].class, false);
055: }
056:
057: private byte[] toByteArray(InputStream in, int length)
058: throws IOException {
059: logger.debug("toByteArray(in=" + in + ", length=" + length
060: + ") - start");
061:
062: ByteArrayOutputStream out = new ByteArrayOutputStream(length);
063: in = new BufferedInputStream(in);
064: int i = in.read();
065: while (i != -1) {
066: out.write(i);
067: i = in.read();
068: }
069: return out.toByteArray();
070: }
071:
072: ////////////////////////////////////////////////////////////////////////////
073: // DataType class
074:
075: public Object typeCast(Object value) throws TypeCastException {
076: logger.debug("typeCast(value=" + value + ") - start");
077:
078: if (value == null || value == ITable.NO_VALUE) {
079: return null;
080: }
081:
082: if (value instanceof byte[]) {
083: return value;
084: }
085:
086: if (value instanceof String) {
087: String stringValue = (String) value;
088:
089: // Assume not an uri if length greater than max uri length
090: if (stringValue.length() == 0
091: || stringValue.length() > MAX_URI_LENGTH) {
092: return Base64.decode((String) value);
093: }
094:
095: try {
096: try {
097: // Try value as URL
098: URL url = new URL(stringValue);
099: return toByteArray(url.openStream(), 0);
100: } catch (MalformedURLException e1) {
101: logger.error("typeCast()", e1);
102:
103: try {
104: // Not an URL, try as file name
105: File file = new File(stringValue);
106: return toByteArray(new FileInputStream(file),
107: (int) file.length());
108: } catch (FileNotFoundException e2) {
109: logger.error("typeCast()", e2);
110:
111: // Not a file name either
112: return Base64.decode((String) value);
113: }
114: }
115: } catch (IOException e) {
116: logger.error("typeCast()", e);
117:
118: throw new TypeCastException(value, this , e);
119: }
120: }
121:
122: if (value instanceof Blob) {
123: try {
124: Blob blobValue = (Blob) value;
125: return blobValue.getBytes(1, (int) blobValue.length());
126: } catch (SQLException e) {
127: logger.error("typeCast()", e);
128:
129: throw new TypeCastException(value, this , e);
130: }
131: }
132:
133: if (value instanceof URL) {
134: try {
135: return toByteArray(((URL) value).openStream(), 0);
136: } catch (IOException e) {
137: logger.error("typeCast()", e);
138:
139: throw new TypeCastException(value, this , e);
140: }
141: }
142:
143: if (value instanceof File) {
144: try {
145: File file = (File) value;
146: return toByteArray(new FileInputStream(file),
147: (int) file.length());
148: } catch (IOException e) {
149: logger.error("typeCast()", e);
150:
151: throw new TypeCastException(value, this , e);
152: }
153: }
154:
155: throw new TypeCastException(value, this );
156: }
157:
158: public int compare(Object o1, Object o2) throws TypeCastException {
159: logger.debug("compare(o1=" + o1 + ", o2=" + o2 + ") - start");
160:
161: try {
162: byte[] value1 = (byte[]) typeCast(o1);
163: byte[] value2 = (byte[]) typeCast(o2);
164:
165: if (value1 == null && value2 == null) {
166: return 0;
167: }
168:
169: if (value1 == null && value2 != null) {
170: return -1;
171: }
172:
173: if (value1 != null && value2 == null) {
174: return 1;
175: }
176:
177: return compare(value1, value2);
178: } catch (ClassCastException e) {
179: logger.error("compare()", e);
180:
181: throw new TypeCastException(e);
182: }
183: }
184:
185: public int compare(byte[] v1, byte[] v2) throws TypeCastException {
186: logger.debug("compare(v1=" + v1 + ", v2=" + v2 + ") - start");
187:
188: int len1 = v1.length;
189: int len2 = v2.length;
190: int n = Math.min(len1, len2);
191: int i = 0;
192: int j = 0;
193:
194: if (i == j) {
195: int k = i;
196: int lim = n + i;
197: while (k < lim) {
198: byte c1 = v1[k];
199: byte c2 = v2[k];
200: if (c1 != c2) {
201: return c1 - c2;
202: }
203: k++;
204: }
205: } else {
206: while (n-- != 0) {
207: byte c1 = v1[i++];
208: byte c2 = v2[j++];
209: if (c1 != c2) {
210: return c1 - c2;
211: }
212: }
213: }
214: return len1 - len2;
215: }
216:
217: public Object getSqlValue(int column, ResultSet resultSet)
218: throws SQLException, TypeCastException {
219: logger.debug("getSqlValue(column=" + column + ", resultSet="
220: + resultSet + ") - start");
221:
222: byte[] value = resultSet.getBytes(column);
223: if (value == null || resultSet.wasNull()) {
224: return null;
225: }
226: return value;
227: }
228:
229: public void setSqlValue(Object value, int column,
230: PreparedStatement statement) throws SQLException,
231: TypeCastException {
232: logger.debug("setSqlValue(value=" + value + ", column="
233: + column + ", statement=" + statement + ") - start");
234:
235: super.setSqlValue(value, column, statement);
236: }
237:
238: }
|