NIFI-4830: Fixed logic errors in BLOB/CLOB processing in JdbcCommon

This closes #2459.

Signed-off-by: Koji Kawamura <ijokarumawak@apache.org>
This commit is contained in:
Matthew Burgess 2018-02-08 14:02:58 -05:00 committed by Koji Kawamura
parent 4428fe28bf
commit 7d6bbce123
2 changed files with 15 additions and 5 deletions

View File

@ -280,7 +280,7 @@ public class JdbcCommon {
InputStream is = clob.getAsciiStream(); InputStream is = clob.getAsciiStream();
int index = 0; int index = 0;
int c = is.read(); int c = is.read();
while (c > 0) { while (c >= 0) {
buffer[index++] = (char) c; buffer[index++] = (char) c;
c = is.read(); c = is.read();
} }
@ -316,7 +316,7 @@ public class JdbcCommon {
InputStream is = blob.getBinaryStream(); InputStream is = blob.getBinaryStream();
int index = 0; int index = 0;
int c = is.read(); int c = is.read();
while (c > 0) { while (c >= 0) {
buffer[index++] = (byte) c; buffer[index++] = (byte) c;
c = is.read(); c = is.read();
} }

View File

@ -476,11 +476,13 @@ public class TestJdbcCommon {
public void testClob() throws Exception { public void testClob() throws Exception {
try (final Statement stmt = con.createStatement()) { try (final Statement stmt = con.createStatement()) {
stmt.executeUpdate("CREATE TABLE clobtest (id INT, text CLOB(64 K))"); stmt.executeUpdate("CREATE TABLE clobtest (id INT, text CLOB(64 K))");
stmt.execute("INSERT INTO blobtest VALUES (41, NULL)"); stmt.execute("INSERT INTO clobtest VALUES (41, NULL)");
PreparedStatement ps = con.prepareStatement("INSERT INTO clobtest VALUES (?, ?)"); PreparedStatement ps = con.prepareStatement("INSERT INTO clobtest VALUES (?, ?)");
ps.setInt(1, 42); ps.setInt(1, 42);
final char[] buffer = new char[4002]; final char[] buffer = new char[4002];
IntStream.range(0, 4002).forEach((i) -> buffer[i] = String.valueOf(i % 10).charAt(0)); IntStream.range(0, 4002).forEach((i) -> buffer[i] = String.valueOf(i % 10).charAt(0));
// Put a zero-byte in to test the buffer building logic
buffer[1] = 0;
ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset()); ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset());
// - set the value of the input parameter to the input stream // - set the value of the input parameter to the input stream
@ -513,7 +515,10 @@ public class TestJdbcCommon {
assertNull(o); assertNull(o);
} else { } else {
assertNotNull(o); assertNotNull(o);
assertEquals(4002, o.toString().length()); final String text = o.toString();
assertEquals(4002, text.length());
// Third character should be '2'
assertEquals('2', text.charAt(2));
} }
} }
} }
@ -529,6 +534,8 @@ public class TestJdbcCommon {
ps.setInt(1, 42); ps.setInt(1, 42);
final byte[] buffer = new byte[4002]; final byte[] buffer = new byte[4002];
IntStream.range(0, 4002).forEach((i) -> buffer[i] = (byte) ((i % 10) + 65)); IntStream.range(0, 4002).forEach((i) -> buffer[i] = (byte) ((i % 10) + 65));
// Put a zero-byte in to test the buffer building logic
buffer[1] = 0;
ByteArrayInputStream bais = new ByteArrayInputStream(buffer); ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
// - set the value of the input parameter to the input stream // - set the value of the input parameter to the input stream
@ -562,7 +569,10 @@ public class TestJdbcCommon {
} else { } else {
assertNotNull(o); assertNotNull(o);
assertTrue(o instanceof ByteBuffer); assertTrue(o instanceof ByteBuffer);
assertEquals(4002, ((ByteBuffer) o).array().length); final byte[] blob = ((ByteBuffer) o).array();
assertEquals(4002, blob.length);
// Third byte should be 67 ('C')
assertEquals('C', blob[2]);
} }
} }
} }