Note that there are some explanatory texts on larger screens.

plurals
  1. POMybatis Custom Type handler: call FileInputStream.close() after query being executed
    text
    copied!<p>I am trying to implement MyBatis custom type handler for File using FileInputStream.</p> <p>here is my code for setting:</p> <pre><code>@MappedJdbcTypes(JdbcType.LONGVARBINARY) public class FileByteaHandler extends BaseTypeHandler&lt;File&gt; { @Override public void setNonNullParameter(PreparedStatement ps, int i, File file, JdbcType jdbcType) throws SQLException{ try { FileInputStream fis = new FileInputStream(file); ps.setBinaryStream(1, fis, (int) file.length()); } catch(FileNotFoundException ex) { Logger.getLogger(FileByteaHandler.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>}</p> <p>My question is:</p> <p>I can not close this FileInputStream at the end of this method, otherwise MyBatis will not be able to read the data from it. In fact, I do not know where I can close the FileInputStream. Is there a way to call close() after the query being excuted in MyBatis.</p> <p>Thanks in advance,</p> <p><strong>UPDATE</strong></p> <p>Thanks for Jarandinor's help. Here is my code for this type handler. and hopefully it can help someone:</p> <pre><code>@MappedJdbcTypes(JdbcType.LONGVARBINARY) public class FileByteaHandler extends BaseTypeHandler&lt;File&gt; { @Override public void setNonNullParameter(PreparedStatement ps, int i, File file, JdbcType jdbcType) throws SQLException { try { AutoCloseFileInputStream fis = new AutoCloseFileInputStream(file); ps.setBinaryStream(1, fis, (int) file.length()); } catch(FileNotFoundException ex) { Logger.getLogger(FileByteaHandler.class.getName()).log(Level.SEVERE, null, ex); } } @Override public File getNullableResult(ResultSet rs, String columnName) throws SQLException { File file = null; try(InputStream input = rs.getBinaryStream(columnName)) { file = getResult(rs, input); } catch(IOException e) { System.out.println(e.getMessage()); } return file; } public File creaetFile() { File file = new File("e:/target-file"); //your temp file path return file; } private File getResult(ResultSet rs, InputStream input) throws SQLException { File file = creaetFile(); try(OutputStream output = new FileOutputStream(file)) { int bufSize = 0x8000000; byte buf[] = new byte[bufSize]; int s = 0; int tl = 0; while( (s = input.read(buf, 0, bufSize)) &gt; 0 ) { output.write(buf, 0, s); tl += s; } output.flush(); } catch(IOException e) { System.out.println(e.getMessage()); } return file; } @Override public File getNullableResult(ResultSet rs, int columnIndex) throws SQLException { File file = null; try(InputStream input = rs.getBinaryStream(columnIndex)) { file = getResult(rs, input); } catch(IOException e) { System.out.println(e.getMessage()); } return file; } @Override public File getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { throw new SQLException("getNullableResult(CallableStatement cs, int columnIndex) is called"); } private class AutoCloseFileInputStream extends FileInputStream { public AutoCloseFileInputStream(File file) throws FileNotFoundException { super(file); } @Override public int read() throws IOException { int c = super.read(); if(available() &lt;= 0) { close(); } return c; } public int read(byte[] b) throws IOException { int c = super.read(b); if(available() &lt;= 0) { close(); } return c; } public int read(byte[] b, int off, int len) throws IOException { int c = super.read(b, off, len); if(available() &lt;= 0) { close(); } return c; } } } public AutoCloseFileInputStream(File file) throws FileNotFoundException { super(file); } @Override public int read() throws IOException { int c = super.read(); if( c == -1 ) { close(); } return c; } public int read(byte[] b) throws IOException { int c = super.read(b); if( c == -1 ) { close(); } return c; } public int read(byte[] b, int off, int len) throws IOException { int c = super.read(b, off, len); if(available() &lt;= 0) { close(); } return c; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload