private void add(String s, boolean insert) throws IOException { if (s == null) { return; } if (lineSeparator.length > 1 || lineSeparator[0] != '\n') { s = StringUtils.replaceAll(s, "\n", lineSeparatorString); } s += ";"; if (out != null) { byte[] buff = s.getBytes(charset); int len = MathUtils.roundUpInt(buff.length + lineSeparator.length, Constants.FILE_BLOCK_SIZE); buffer = Utils.copy(buff, buffer); if (len > buffer.length) { buffer = new byte[len]; } System.arraycopy(buff, 0, buffer, 0, buff.length); for (int i = buff.length; i < len - lineSeparator.length; i++) { buffer[i] = ' '; } for (int j = 0, i = len - lineSeparator.length; i < len; i++, j++) { buffer[i] = lineSeparator[j]; } out.write(buffer, 0, len); if (!insert) { Value[] row = {ValueString.get(s)}; result.addRow(row); } } else { Value[] row = {ValueString.get(s)}; result.addRow(row); } }
private int writeLobStream(Value v) throws IOException { if (!tempLobTableCreated) { add( "CREATE TABLE IF NOT EXISTS SYSTEM_LOB_STREAM" + "(ID INT NOT NULL, PART INT NOT NULL, CDATA VARCHAR, BDATA BINARY)", true); add( "CREATE PRIMARY KEY SYSTEM_LOB_STREAM_PRIMARY_KEY " + "ON SYSTEM_LOB_STREAM(ID, PART)", true); add( "CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_CLOB FOR \"" + this.getClass().getName() + ".combineClob\"", true); add( "CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_BLOB FOR \"" + this.getClass().getName() + ".combineBlob\"", true); tempLobTableCreated = true; } int id = nextLobId++; switch (v.getType()) { case Value.BLOB: { byte[] bytes = new byte[lobBlockSize]; InputStream input = v.getInputStream(); try { for (int i = 0; ; i++) { StringBuilder buff = new StringBuilder(lobBlockSize * 2); buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", NULL, '"); int len = IOUtils.readFully(input, bytes, lobBlockSize); if (len <= 0) { break; } buff.append(StringUtils.convertBytesToHex(bytes, len)).append("')"); String sql = buff.toString(); add(sql, true); } } finally { IOUtils.closeSilently(input); } break; } case Value.CLOB: { char[] chars = new char[lobBlockSize]; Reader reader = v.getReader(); try { for (int i = 0; ; i++) { StringBuilder buff = new StringBuilder(lobBlockSize * 2); buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", "); int len = IOUtils.readFully(reader, chars, lobBlockSize); if (len < 0) { break; } buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len))).append(", NULL)"); String sql = buff.toString(); add(sql, true); } } finally { IOUtils.closeSilently(reader); } break; } default: DbException.throwInternalError("type:" + v.getType()); } return id; }