Fixed arabic not rendering, I was using the wrong input stream.... CharSource was the way to go

This commit is contained in:
U-BASIS\dsmyda 2018-09-07 13:27:14 -04:00
parent 341ae826fe
commit a942b87adb
4 changed files with 84 additions and 111 deletions

View File

@ -59,7 +59,7 @@ public abstract class AbstractReader implements AutoCloseable {
final String getLocalDiskPath(Content file) throws FileReaderException { final String getLocalDiskPath(Content file) throws FileReaderException {
try { try {
return Case.getCurrentCaseThrows().getTempDirectory() + return Case.getCurrentCaseThrows().getTempDirectory() +
File.separator + file.getId() + "-" + file.getName(); File.separator + file.getId() + file.getName();
} catch (NoCurrentCaseException ex) { } catch (NoCurrentCaseException ex) {
throw new FileReaderException(ex); throw new FileReaderException(ex);
} }

View File

@ -101,7 +101,7 @@ public final class SQLiteReader extends AbstractReader {
if (metaFiles != null) { if (metaFiles != null) {
for (AbstractFile metaFile : metaFiles) { for (AbstractFile metaFile : metaFiles) {
String tmpMetafilePathName = openCase.getTempDirectory() + String tmpMetafilePathName = openCase.getTempDirectory() +
File.separator + metaFile.getId() + "-" + metaFile.getName(); File.separator + metaFile.getId() + metaFile.getName();
File tmpMetafile = new File(tmpMetafilePathName); File tmpMetafile = new File(tmpMetafilePathName);
ContentUtils.writeToFile(metaFile, tmpMetafile); ContentUtils.writeToFile(metaFile, tmpMetafile);
} }

View File

@ -18,11 +18,9 @@
*/ */
package org.sleuthkit.autopsy.keywordsearch; package org.sleuthkit.autopsy.keywordsearch;
import com.google.common.io.CharSource;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
@ -82,29 +80,7 @@ public class SqliteTextExtractor extends ContentTextExtractor {
*/ */
@Override @Override
public Reader getReader(Content source) throws TextExtractorException { public Reader getReader(Content source) throws TextExtractorException {
return new InputStreamReader(new SqliteTextReader(source)); StringBuilder databaseBuffer = new StringBuilder();
}
@Override
public boolean isDisabled() {
return false;
}
@Override
public void logWarning(String msg, Exception ex) {
logger.log(Level.WARNING, msg, ex); //NON-NLS
}
/**
* InputStream that is returned from the getReader method. This stream opens
* a sqlite file and loads its contents into a buffer that can be read from
* the read function.
*/
private final class SqliteTextReader extends InputStream {
private StringBuilder databaseBuffer;
private int currReadIndex;
private final int NO_CONTENT_LEFT = -1;
/** /**
* The buffer is filled during initialization, meaning the whole sqlite * The buffer is filled during initialization, meaning the whole sqlite
@ -115,12 +91,11 @@ public class SqliteTextExtractor extends ContentTextExtractor {
* @throws * @throws
* org.sleuthkit.autopsy.keywordsearch.TextExtractor.TextExtractorException * org.sleuthkit.autopsy.keywordsearch.TextExtractor.TextExtractorException
*/ */
public SqliteTextReader(Content source) throws TextExtractorException {
try (AbstractReader reader = FileReaderFactory.createReader( try (AbstractReader reader = FileReaderFactory.createReader(
SQLITE_MIMETYPE, source)) { SQLITE_MIMETYPE, source)) {
this.databaseBuffer = new StringBuilder(); databaseBuffer = new StringBuilder();
//Fill the entire buffer upon instantiation //Fill the entire buffer upon instantiation
copyDatabaseIntoBuffer(source, reader); copyDatabaseIntoBuffer(source, reader, databaseBuffer);
} catch (FileReaderInitException ex) { } catch (FileReaderInitException ex) {
throw new TextExtractorException( throw new TextExtractorException(
String.format("Encountered a FileReaderInitException" //NON-NLS String.format("Encountered a FileReaderInitException" //NON-NLS
@ -128,6 +103,13 @@ public class SqliteTextExtractor extends ContentTextExtractor {
+ " for Content with id:[%s], name:[%s].", //NON-NLS + " for Content with id:[%s], name:[%s].", //NON-NLS
source.getId(), source.getName())); source.getId(), source.getName()));
} }
try {
return CharSource.wrap(databaseBuffer.toString()).openStream();
} catch (IOException ex) {
throw new TextExtractorException(String.format("Unable to open CharSource stream on the databaseBuffer"
+ "for content source name: [%s] with id: [%d]", source.getName(), source.getId()));
}
} }
/** /**
@ -137,10 +119,10 @@ public class SqliteTextExtractor extends ContentTextExtractor {
* *
* @param reader * @param reader
*/ */
private void copyDatabaseIntoBuffer(Content source, AbstractReader reader) { private void copyDatabaseIntoBuffer(Content source, AbstractReader reader, StringBuilder databaseBuffer) {
try { try {
Map<String, String> tables = reader.getTableSchemas(); Map<String, String> tables = reader.getTableSchemas();
iterateTablesAndPopulateBuffer(tables, reader, source); iterateTablesAndPopulateBuffer(tables, reader, source, databaseBuffer);
} catch (AbstractReader.FileReaderException ex) { } catch (AbstractReader.FileReaderException ex) {
logger.log(Level.WARNING, String.format( logger.log(Level.WARNING, String.format(
"Error attempting to get tables from file: " //NON-NLS "Error attempting to get tables from file: " //NON-NLS
@ -158,7 +140,7 @@ public class SqliteTextExtractor extends ContentTextExtractor {
* @param source Source database file for logging * @param source Source database file for logging
*/ */
private void iterateTablesAndPopulateBuffer(Map<String, String> tables, private void iterateTablesAndPopulateBuffer(Map<String, String> tables,
AbstractReader reader, Content source) { AbstractReader reader, Content source, StringBuilder databaseBuffer) {
for (String tableName : tables.keySet()) { for (String tableName : tables.keySet()) {
TableBuilder tableBuilder = new TableBuilder(); TableBuilder tableBuilder = new TableBuilder();
@ -166,7 +148,7 @@ public class SqliteTextExtractor extends ContentTextExtractor {
try { try {
List<Map<String, Object>> rowsInTable List<Map<String, Object>> rowsInTable
= reader.getRowsFromTable(tableName); = reader.getRowsFromTable(tableName);
addRowsToTableBuilder(tableBuilder, rowsInTable); addRowsToTableBuilder(tableBuilder, rowsInTable, databaseBuffer);
} catch (AbstractReader.FileReaderException ex) { } catch (AbstractReader.FileReaderException ex) {
logger.log(Level.WARNING, String.format( logger.log(Level.WARNING, String.format(
"Error attempting to read file table: [%s]" //NON-NLS "Error attempting to read file table: [%s]" //NON-NLS
@ -184,7 +166,7 @@ public class SqliteTextExtractor extends ContentTextExtractor {
* @param rowsInTable list of rows from the sqlite table * @param rowsInTable list of rows from the sqlite table
*/ */
private void addRowsToTableBuilder(TableBuilder tableBuilder, private void addRowsToTableBuilder(TableBuilder tableBuilder,
List<Map<String, Object>> rowsInTable) { List<Map<String, Object>> rowsInTable, StringBuilder databaseBuffer) {
if (!rowsInTable.isEmpty()) { if (!rowsInTable.isEmpty()) {
//Create a collection from the header set, so that the TableBuilder //Create a collection from the header set, so that the TableBuilder
//can easily format it //can easily format it
@ -198,22 +180,14 @@ public class SqliteTextExtractor extends ContentTextExtractor {
databaseBuffer.append(tableBuilder); databaseBuffer.append(tableBuilder);
} }
/**
* Returns one byte of the buffer at a time. This buffer was completely
* loaded during construction. Consider a lazy approach or a
* multi-threaded one if too slow.
*
* @return @throws IOException
*/
@Override @Override
public int read() throws IOException { public boolean isDisabled() {
//End of the buffer if true return false;
if (currReadIndex == databaseBuffer.length() - 1) {
return NO_CONTENT_LEFT;
} }
return databaseBuffer.charAt(currReadIndex++); @Override
} public void logWarning(String msg, Exception ex) {
logger.log(Level.WARNING, msg, ex); //NON-NLS
} }
/* /*
@ -279,7 +253,7 @@ public class SqliteTextExtractor extends ContentTextExtractor {
public void addRow(Collection<Object> vals) { public void addRow(Collection<Object> vals) {
List<String> rowValues = new ArrayList<>(); List<String> rowValues = new ArrayList<>();
vals.forEach((val) -> { vals.forEach((val) -> {
rowValues.add(String.valueOf(val)); rowValues.add(val.toString());
}); });
rows.add(rowValues.toArray( rows.add(rowValues.toArray(
new String[rowValues.size()])); new String[rowValues.size()]));

View File

@ -202,7 +202,6 @@ class TikaTextExtractor extends ContentTextExtractor {
) { ) {
return false; return false;
} }
System.out.println(detectedFormat);
return TIKA_SUPPORTED_TYPES.contains(detectedFormat); return TIKA_SUPPORTED_TYPES.contains(detectedFormat);
} }