提交 48e87d3b authored 作者: noelgrandin's avatar noelgrandin

simplify code - reduce some catching and rethrowing

上级 c82c7ab6
......@@ -197,30 +197,26 @@ public class ValueLob extends Value {
return (int) m;
}
private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) {
private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException {
FileStoreOutputStream out = initLarge(h);
boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null;
try {
FileStoreOutputStream out = initLarge(h);
boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null;
try {
while (true) {
precision += len;
byte[] b = new String(buff, 0, len).getBytes(Constants.UTF8);
out.write(b, 0, b.length);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, len);
if (len <= 0) {
break;
}
while (true) {
precision += len;
byte[] b = new String(buff, 0, len).getBytes(Constants.UTF8);
out.write(b, 0, b.length);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, len);
if (len <= 0) {
break;
}
} finally {
out.close();
}
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
out.close();
}
}
......@@ -400,29 +396,25 @@ public class ValueLob extends Value {
return out;
}
private void createFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h) {
private void createFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h) throws IOException {
FileStoreOutputStream out = initLarge(h);
boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null;
try {
FileStoreOutputStream out = initLarge(h);
boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null;
try {
while (true) {
precision += len;
out.write(buff, 0, len);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, 0, len);
if (len <= 0) {
break;
}
while (true) {
precision += len;
out.write(buff, 0, len);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, 0, len);
if (len <= 0) {
break;
}
} finally {
out.close();
}
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
out.close();
}
}
......@@ -692,19 +684,23 @@ public class ValueLob extends Value {
* @param h the data handler
*/
public void convertToFileIfRequired(DataHandler h) {
if (small != null && small.length > h.getMaxLengthInplaceLob()) {
boolean compress = h.getLobCompressionAlgorithm(type) != null;
int len = getBufferSize(h, compress, Long.MAX_VALUE);
int tabId = tableId;
if (type == Value.BLOB) {
createFromStream(DataUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, h);
} else {
createFromReader(new char[len], 0, getReader(), Long.MAX_VALUE, h);
}
Value v2 = link(h, tabId);
if (SysProperties.CHECK && v2 != this) {
DbException.throwInternalError();
try {
if (small != null && small.length > h.getMaxLengthInplaceLob()) {
boolean compress = h.getLobCompressionAlgorithm(type) != null;
int len = getBufferSize(h, compress, Long.MAX_VALUE);
int tabId = tableId;
if (type == Value.BLOB) {
createFromStream(DataUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, h);
} else {
createFromReader(new char[len], 0, getReader(), Long.MAX_VALUE, h);
}
Value v2 = link(h, tabId);
if (SysProperties.CHECK && v2 != this) {
DbException.throwInternalError();
}
}
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
}
......
......@@ -456,72 +456,60 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
}
}
private void createTempFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) {
private void createTempFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException {
FileStoreOutputStream out = initTemp(h);
try {
FileStoreOutputStream out = initTemp(h);
try {
while (true) {
precision += len;
byte[] b = new String(buff, 0, len).getBytes(Constants.UTF8);
out.write(b, 0, b.length);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, false, remaining);
len = IOUtils.readFully(in, buff, len);
if (len <= 0) {
break;
}
while (true) {
precision += len;
byte[] b = new String(buff, 0, len).getBytes(Constants.UTF8);
out.write(b, 0, b.length);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, false, remaining);
len = IOUtils.readFully(in, buff, len);
if (len <= 0) {
break;
}
} finally {
out.close();
}
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
out.close();
}
}
private void createTempFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h) {
private void createTempFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h) throws IOException {
FileStoreOutputStream out = initTemp(h);
boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null;
try {
FileStoreOutputStream out = initTemp(h);
boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null;
try {
while (true) {
precision += len;
out.write(buff, 0, len);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, 0, len);
if (len <= 0) {
break;
}
while (true) {
precision += len;
out.write(buff, 0, len);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, 0, len);
if (len <= 0) {
break;
}
} finally {
out.close();
}
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
out.close();
}
}
private FileStoreOutputStream initTemp(DataHandler h) {
private FileStoreOutputStream initTemp(DataHandler h) throws IOException {
this.precision = 0;
this.handler = h;
this.lobStorage = h.getLobStorage();
this.small = null;
try {
String path = h.getDatabasePath();
if (path.length() == 0) {
path = SysProperties.PREFIX_TEMP_FILE;
}
fileName = FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
String path = h.getDatabasePath();
if (path.length() == 0) {
path = SysProperties.PREFIX_TEMP_FILE;
}
fileName = FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true);
tempFile = h.openFile(fileName, "rw", false);
tempFile.autoDelete();
FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论