19 package org.sleuthkit.autopsy.textextractors;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Iterator;
24 import java.util.Objects;
25 import java.util.function.Consumer;
26 import java.util.logging.Level;
41 final class SqliteTextExtractor
implements TextExtractor {
43 private static final String SQLITE_MIMETYPE =
"application/x-sqlite3";
44 private static final Logger logger = Logger.getLogger(SqliteTextExtractor.class.getName());
45 private final AbstractFile file;
47 SqliteTextExtractor(AbstractFile file) {
59 public boolean isSupported() {
60 return SQLITE_MIMETYPE.equals(file.getMIMEType());
73 public Reader getReader() throws InitReaderException {
74 return new SQLiteStreamReader(file);
85 private final AbstractFile
file;
123 return new Consumer<Object>() {
124 private int columnIndex = 0;
127 public void accept(Object value) {
130 String objectStr = (value instanceof byte[]) ?
"" : Objects.toString(value,
"");
132 if (columnIndex > 1 && columnIndex < totalColumns) {
135 if (columnIndex == 1) {
136 objectStr =
"\t" + objectStr +
" ";
138 if (columnIndex == totalColumns) {
161 return new Consumer<String>() {
162 private int columnIndex = 0;
165 public void accept(String columnName) {
166 if (columnIndex == 0) {
167 fillBuffer(
"\n" + currentTableName +
"\n\n\t");
171 fillBuffer(columnName + ((columnIndex == totalColumns) ?
"\n" :
" "));
187 for (
int i = 0; i < val.length(); i++) {
188 if (bufIndex != buf.length) {
189 buf[bufIndex++] = val.charAt(i);
206 public int read(
char[] cbuf,
int off,
int len)
throws IOException {
212 if (Objects.isNull(tableNames)) {
222 if (Objects.nonNull(leftOvers) && !leftOvers.isFinished()) {
223 bufIndex += leftOvers.read(cbuf, off, len);
228 while (bufIndex != len) {
229 if (Objects.isNull(currentTableName) || reader.
isFinished()) {
230 if (tableNames.hasNext()) {
231 currentTableName = tableNames.next();
234 reader.
read(currentTableName, () -> bufIndex == len);
236 logger.log(Level.WARNING, String.format(
237 "Error attempting to read file table: [%s]"
238 +
" for file: [%s] (id=%d).", currentTableName,
239 file.getName(), file.getId()), ex.getMessage());
242 if (bufIndex == off) {
249 reader.
read(currentTableName, () -> bufIndex == len);
251 logger.log(Level.WARNING, String.format(
252 "Error attempting to read file table: [%s]"
253 +
" for file: [%s] (id=%d).", currentTableName,
254 file.getName(), file.getId()), ex.getMessage());
263 public void close() throws IOException {
267 logger.log(Level.WARNING,
"Could not close SQliteTableReader.", ex.getMessage());
285 boolean isFinished() {
286 return entity.length() ==
pointer;
299 int read(
char[] buf,
int off,
int len) {
300 for (
int i = off; i < len; i++) {
305 buf[i] = entity.charAt(pointer++);
List< String > getTableNames()
int getColumnCount(String tableName)
Builder forAllColumnNames(Consumer< String > action)
Builder forAllTableValues(Consumer< Object > action)
SQLiteTableReader build()
void read(String tableName)