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

simplify code - reduce some catching and rethrowing

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