View Javadoc
1   /**
2    *    Copyright 2009-2015 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.ByteArrayInputStream;
19  import java.sql.*;
20  
21  /**
22   * @author Clinton Begin
23   */
24  public class BlobByteObjectArrayTypeHandler extends BaseTypeHandler<Byte[]> {
25  
26    @Override
27    public void setNonNullParameter(PreparedStatement ps, int i, Byte[] parameter, JdbcType jdbcType)
28        throws SQLException {
29      ByteArrayInputStream bis = new ByteArrayInputStream(ByteArrayUtils.convertToPrimitiveArray(parameter));
30      ps.setBinaryStream(i, bis, parameter.length);
31    }
32  
33    @Override
34    public Byte[] getNullableResult(ResultSet rs, String columnName)
35        throws SQLException {
36      Blob blob = rs.getBlob(columnName);
37      return getBytes(blob);
38    }
39  
40    @Override
41    public Byte[] getNullableResult(ResultSet rs, int columnIndex)
42        throws SQLException {
43      Blob blob = rs.getBlob(columnIndex);
44      return getBytes(blob);
45    }
46  
47    @Override
48    public Byte[] getNullableResult(CallableStatement cs, int columnIndex)
49        throws SQLException {
50      Blob blob = cs.getBlob(columnIndex);
51      return getBytes(blob);
52    }
53  
54    private Byte[] getBytes(Blob blob) throws SQLException {
55      Byte[] returnValue = null;
56      if (blob != null) {
57        returnValue = ByteArrayUtils.convertToObjectArray(blob.getBytes(1, (int) blob.length()));
58      }
59      return returnValue;
60    }
61  }