KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.dbunit.dataset.datatype;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 public class BinaryStreamDataType extends BytesDataType
12 {
13     public BinaryStreamDataType(String JavaDoc name, int sqlType)
14     {
15         super(name, sqlType);
16     }
17
18     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
19             throws SQLException JavaDoc, TypeCastException
20     {
21         InputStream JavaDoc in = resultSet.getBinaryStream(column);
22         if (in == null || resultSet.wasNull())
23         {
24             return null;
25         }
26
27         try
28         {
29             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
30             byte[] buffer = new byte[32];
31             int length = in.read(buffer);
32             while (length != -1)
33             {
34                 out.write(buffer, 0, length);
35                 length = in.read(buffer);
36             }
37             return out.toByteArray();
38         }
39         catch (IOException JavaDoc e)
40         {
41             throw new TypeCastException(e);
42         }
43     }
44
45     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
46             throws SQLException JavaDoc, TypeCastException
47     {
48         byte[] bytes = (byte[])typeCast(value);
49         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(bytes);
50         statement.setBinaryStream(column, in, bytes.length);
51     }
52
53 }
Popular Tags