KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > BytesDataType


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.datatype;
23
24 import org.dbunit.dataset.ITable;
25 import org.dbunit.util.Base64;
26
27 import java.io.*;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.sql.Blob JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 /**
36  * @author Manuel Laflamme
37  * @version $Revision: 1.18 $
38  * @since Mar 20, 2002
39  */

40 public class BytesDataType extends AbstractDataType
41 {
42     private static final int MAX_URI_LENGTH = 256;
43
44     BytesDataType(String JavaDoc name, int sqlType)
45     {
46         super(name, sqlType, byte[].class, false);
47     }
48
49     private byte[] toByteArray(InputStream in, int length) throws IOException
50     {
51         ByteArrayOutputStream out = new ByteArrayOutputStream(length);
52         in = new BufferedInputStream(in);
53         int i = in.read();
54         while (i != -1)
55         {
56             out.write(i);
57             i = in.read();
58         }
59         return out.toByteArray();
60     }
61
62     ////////////////////////////////////////////////////////////////////////////
63
// DataType class
64

65     public Object JavaDoc typeCast(Object JavaDoc value) throws TypeCastException
66     {
67         if (value == null || value == ITable.NO_VALUE)
68         {
69             return null;
70         }
71
72         if (value instanceof byte[])
73         {
74             return value;
75         }
76
77         if (value instanceof String JavaDoc)
78         {
79             String JavaDoc stringValue = (String JavaDoc)value;
80
81             // Assume not an uri if length greater than max uri length
82
if (stringValue.length() == 0 || stringValue.length() > MAX_URI_LENGTH)
83             {
84                 return Base64.decode((String JavaDoc)value);
85             }
86
87             try
88             {
89                 try
90                 {
91                     // Try value as URL
92
URL JavaDoc url = new URL JavaDoc(stringValue);
93                     return toByteArray(url.openStream(), 0);
94                 }
95                 catch (MalformedURLException JavaDoc e1)
96                 {
97                     try
98                     {
99                         // Not an URL, try as file name
100
File file = new File(stringValue);
101                         return toByteArray(new FileInputStream(file),
102                                 (int)file.length());
103                     }
104                     catch (FileNotFoundException e2)
105                     {
106                         // Not a file name either
107
return Base64.decode((String JavaDoc)value);
108                     }
109                 }
110             }
111             catch (IOException e)
112             {
113                 throw new TypeCastException(value, this, e);
114             }
115         }
116
117         if (value instanceof Blob JavaDoc)
118         {
119             try
120             {
121                 Blob JavaDoc blobValue = (Blob JavaDoc)value;
122                 return blobValue.getBytes(1, (int)blobValue.length());
123             }
124             catch (SQLException JavaDoc e)
125             {
126                 throw new TypeCastException(value, this, e);
127             }
128         }
129
130         if (value instanceof URL JavaDoc)
131         {
132             try
133             {
134                 return toByteArray(((URL JavaDoc)value).openStream(), 0);
135             }
136             catch (IOException e)
137             {
138                 throw new TypeCastException(value, this, e);
139             }
140         }
141
142         if (value instanceof File)
143         {
144             try
145             {
146                 File file = (File)value;
147                 return toByteArray(new FileInputStream(file),
148                         (int)file.length());
149             }
150             catch (IOException e)
151             {
152                 throw new TypeCastException(value, this, e);
153             }
154         }
155
156         throw new TypeCastException(value, this);
157     }
158
159     public int compare(Object JavaDoc o1, Object JavaDoc o2) throws TypeCastException
160     {
161         try
162         {
163             byte[] value1 = (byte[])typeCast(o1);
164             byte[] value2 = (byte[])typeCast(o2);
165
166             if (value1 == null && value2 == null)
167             {
168                 return 0;
169             }
170
171             if (value1 == null && value2 != null)
172             {
173                 return -1;
174             }
175
176             if (value1 != null && value2 == null)
177             {
178                 return 1;
179             }
180
181             return compare(value1, value2);
182         }
183         catch (ClassCastException JavaDoc e)
184         {
185             throw new TypeCastException(e);
186         }
187     }
188
189     public int compare(byte[] v1, byte[] v2) throws TypeCastException
190     {
191         int len1 = v1.length;
192         int len2 = v2.length;
193         int n = Math.min(len1, len2);
194         int i = 0;
195         int j = 0;
196
197         if (i == j)
198         {
199             int k = i;
200             int lim = n + i;
201             while (k < lim)
202             {
203                 byte c1 = v1[k];
204                 byte c2 = v2[k];
205                 if (c1 != c2)
206                 {
207                     return c1 - c2;
208                 }
209                 k++;
210             }
211         }
212         else
213         {
214             while (n-- != 0)
215             {
216                 byte c1 = v1[i++];
217                 byte c2 = v2[j++];
218                 if (c1 != c2)
219                 {
220                     return c1 - c2;
221                 }
222             }
223         }
224         return len1 - len2;
225     }
226
227     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
228             throws SQLException JavaDoc, TypeCastException
229     {
230         byte[] value = resultSet.getBytes(column);
231         if (value == null || resultSet.wasNull())
232         {
233             return null;
234         }
235         return value;
236     }
237
238     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
239             throws SQLException JavaDoc, TypeCastException
240     {
241         super.setSqlValue(value, column, statement);
242     }
243
244 }
245
246
247
248
249
250
251
Popular Tags