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