/** * @throws IllegalArgumentException if {@code scannerContextLines} is empty or fully read. */ public void insert(DbSession dbSession, String taskUuid, CloseableIterator<String> scannerContextLines) { checkArgument(scannerContextLines.hasNext(), "Scanner context can not be empty"); long now = system.now(); Connection connection = dbSession.getConnection(); try (PreparedStatement stmt = connection.prepareStatement( "INSERT INTO ce_scanner_context (task_uuid, created_at, updated_at, context_data) VALUES (?, ?, ?, ?)"); InputStream inputStream = new LogsIteratorInputStream(scannerContextLines, UTF_8)) { stmt.setString(1, taskUuid); stmt.setLong(2, now); stmt.setLong(3, now); stmt.setBinaryStream(4, inputStream); stmt.executeUpdate(); connection.commit(); } catch (SQLException | IOException e) { throw new IllegalStateException("Fail to insert scanner context for task " + taskUuid, e); } }
@Override public int read() { if (nextChar == UNSET || nextChar >= buf.length) { fill(); if (nextChar == UNSET) { return END_OF_STREAM; } } return buf[nextChar++]; }
private static LogsIteratorInputStream create(String... lines) { return new LogsIteratorInputStream(CloseableIterator.from(Arrays.asList(lines).iterator()), UTF_8); }
@Override public int read() throws IOException { if (nextChar == UNSET || nextChar >= buf.length) { fill(); if (nextChar == UNSET) { return END_OF_STREAM; } } return buf[nextChar++]; }
@Test public void constructor_throws_IAE_when_ClosableIterator_has_already_been_read() { CloseableIterator<String> iterator = CloseableIterator.from(Arrays.asList("line1").iterator()); // read iterator to the end iterator.next(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("LogsIterator can't be empty or already read"); new LogsIteratorInputStream(iterator, UTF_8); }
/** * @throws IllegalArgumentException if {@code scannerContextLines} is empty or fully read. */ public void insert(DbSession dbSession, String taskUuid, CloseableIterator<String> scannerContextLines) { checkArgument(scannerContextLines.hasNext(), "Scanner context can not be empty"); long now = system.now(); Connection connection = dbSession.getConnection(); try (PreparedStatement stmt = connection.prepareStatement( "INSERT INTO ce_scanner_context (task_uuid, created_at, updated_at, context_data) VALUES (?, ?, ?, ?)"); InputStream inputStream = new LogsIteratorInputStream(scannerContextLines, UTF_8)) { stmt.setString(1, taskUuid); stmt.setLong(2, now); stmt.setLong(3, now); stmt.setBinaryStream(4, inputStream); stmt.executeUpdate(); connection.commit(); } catch (SQLException | IOException e) { throw new IllegalStateException("Fail to insert scanner context for task " + taskUuid, e); } }