1 /* 2 * Copyright 2009-2021 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.ibatis.type; 17 18 import java.io.InputStream; 19 import java.sql.Blob; 20 import java.sql.CallableStatement; 21 import java.sql.PreparedStatement; 22 import java.sql.ResultSet; 23 import java.sql.SQLException; 24 25 /** 26 * The {@link TypeHandler} for {@link Blob}/{@link InputStream} using method supported at JDBC 4.0. 27 * @since 3.4.0 28 * @author Kazuki Shimizu 29 */ 30 public class BlobInputStreamTypeHandler extends BaseTypeHandler<InputStream> { 31 32 /** 33 * Set an {@link InputStream} into {@link PreparedStatement}. 34 * @see PreparedStatement#setBlob(int, InputStream) 35 */ 36 @Override 37 public void setNonNullParameter(PreparedStatement ps, int i, InputStream parameter, JdbcType jdbcType) 38 throws SQLException { 39 ps.setBlob(i, parameter); 40 } 41 42 /** 43 * Get an {@link InputStream} that corresponds to a specified column name from {@link ResultSet}. 44 * @see ResultSet#getBlob(String) 45 */ 46 @Override 47 public InputStream getNullableResult(ResultSet rs, String columnName) 48 throws SQLException { 49 return toInputStream(rs.getBlob(columnName)); 50 } 51 52 /** 53 * Get an {@link InputStream} that corresponds to a specified column index from {@link ResultSet}. 54 * @see ResultSet#getBlob(int) 55 */ 56 @Override 57 public InputStream getNullableResult(ResultSet rs, int columnIndex) 58 throws SQLException { 59 return toInputStream(rs.getBlob(columnIndex)); 60 } 61 62 /** 63 * Get an {@link InputStream} that corresponds to a specified column index from {@link CallableStatement}. 64 * @see CallableStatement#getBlob(int) 65 */ 66 @Override 67 public InputStream getNullableResult(CallableStatement cs, int columnIndex) 68 throws SQLException { 69 return toInputStream(cs.getBlob(columnIndex)); 70 } 71 72 private InputStream toInputStream(Blob blob) throws SQLException { 73 if (blob == null) { 74 return null; 75 } else { 76 return blob.getBinaryStream(); 77 } 78 } 79 80 }