提交 8a749024 authored 作者: Noel Grandin's avatar Noel Grandin

attempt to fix file not closed when using FILE_READ

and remove now unused AutoCloseInputStream
上级 be45032b
...@@ -44,7 +44,6 @@ import org.h2.table.Table; ...@@ -44,7 +44,6 @@ import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.tools.CompressTool; import org.h2.tools.CompressTool;
import org.h2.tools.Csv; import org.h2.tools.Csv;
import org.h2.util.AutoCloseInputStream;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
...@@ -1637,8 +1636,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -1637,8 +1636,8 @@ public class Function extends Expression implements FunctionCall {
boolean blob = args.length == 1; boolean blob = args.length == 1;
try { try {
long fileLength = FileUtils.size(fileName); long fileLength = FileUtils.size(fileName);
InputStream in = new AutoCloseInputStream( final InputStream in = FileUtils.newInputStream(fileName);
FileUtils.newInputStream(fileName)); try {
if (blob) { if (blob) {
result = database.getLobStorage().createBlob(in, fileLength); result = database.getLobStorage().createBlob(in, fileLength);
} else { } else {
...@@ -1650,6 +1649,9 @@ public class Function extends Expression implements FunctionCall { ...@@ -1650,6 +1649,9 @@ public class Function extends Expression implements FunctionCall {
} }
result = database.getLobStorage().createClob(reader, fileLength); result = database.getLobStorage().createClob(reader, fileLength);
} }
} finally {
IOUtils.closeSilently(in);
}
session.addTemporaryLob(result); session.addTemporaryLob(result);
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, fileName); throw DbException.convertIOException(e, fileName);
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.util;
import java.io.IOException;
import java.io.InputStream;
/**
* This input stream wrapper closes the base input stream when fully read.
*/
public class AutoCloseInputStream extends InputStream {
private final InputStream in;
private boolean closed;
/**
* Create a new input stream.
*
* @param in the input stream
*/
public AutoCloseInputStream(InputStream in) {
this.in = in;
}
private int autoClose(int x) throws IOException {
if (x < 0) {
close();
}
return x;
}
@Override
public void close() throws IOException {
if (!closed) {
in.close();
closed = true;
}
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return closed ? -1 : autoClose(in.read(b, off, len));
}
@Override
public int read(byte[] b) throws IOException {
return closed ? -1 : autoClose(in.read(b));
}
@Override
public int read() throws IOException {
return closed ? -1 : autoClose(in.read());
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论