19 package org.sleuthkit.autopsy.textreaders;
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;
42 final class SqliteTextExtractor
extends TextExtractor {
44 private static final String SQLITE_MIMETYPE =
"application/x-sqlite3";
45 private static final Logger logger = Logger.getLogger(SqliteTextExtractor.class.getName());
46 private final AbstractFile file;
48 public SqliteTextExtractor(Content file) {
49 this.file = (AbstractFile) file;
60 public boolean isSupported(Content file, String detectedFormat) {
61 return SQLITE_MIMETYPE.equals(detectedFormat);
74 public Reader getReader() throws ExtractionException {
75 return new SQLiteStreamReader(file);
86 private final AbstractFile
file;
124 return new Consumer<Object>() {
125 private int columnIndex = 0;
128 public void accept(Object value) {
131 String objectStr = (value instanceof byte[]) ?
"" : Objects.toString(value,
"");
133 if (columnIndex > 1 && columnIndex < totalColumns) {
136 if (columnIndex == 1) {
137 objectStr =
"\t" + objectStr +
" ";
139 if (columnIndex == totalColumns) {
162 return new Consumer<String>() {
163 private int columnIndex = 0;
166 public void accept(String columnName) {
167 if (columnIndex == 0) {
168 fillBuffer(
"\n" + currentTableName +
"\n\n\t");
172 fillBuffer(columnName + ((columnIndex == totalColumns) ?
"\n" :
" "));
188 for (
int i = 0; i < val.length(); i++) {
189 if (bufIndex != buf.length) {
190 buf[bufIndex++] = val.charAt(i);
207 public int read(
char[] cbuf,
int off,
int len)
throws IOException {
213 if (Objects.isNull(tableNames)) {
223 if (Objects.nonNull(leftOvers) && !leftOvers.
isFinished()) {
224 bufIndex += leftOvers.
read(cbuf, off, len);
229 while (bufIndex != len) {
230 if (Objects.isNull(currentTableName) || reader.
isFinished()) {
231 if (tableNames.hasNext()) {
232 currentTableName = tableNames.next();
235 reader.
read(currentTableName, () -> bufIndex == len);
237 logger.log(Level.WARNING, String.format(
238 "Error attempting to read file table: [%s]"
239 +
" for file: [%s] (id=%d).", currentTableName,
240 file.getName(), file.getId()), ex.getMessage());
243 if (bufIndex == off) {
250 reader.
read(currentTableName, () -> bufIndex == len);
252 logger.log(Level.WARNING, String.format(
253 "Error attempting to read file table: [%s]"
254 +
" for file: [%s] (id=%d).", currentTableName,
255 file.getName(), file.getId()), ex.getMessage());
264 public void close() throws IOException {
268 logger.log(Level.WARNING,
"Could not close SQliteTableReader.", ex.getMessage());
287 return entity.length() ==
pointer;
300 public int read(
char[] buf,
int off,
int len) {
301 for (
int i = off; i < len; i++) {
306 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)