19 package org.sleuthkit.autopsy.datamodel;
 
   21 import java.io.FileOutputStream;
 
   22 import java.io.IOException;
 
   23 import java.io.InputStream;
 
   24 import java.text.SimpleDateFormat;
 
   25 import java.util.TimeZone;
 
   26 import java.util.concurrent.Future;
 
   27 import java.util.function.Supplier;
 
   28 import java.util.logging.Level;
 
   29 import javax.swing.SwingWorker;
 
   30 import org.netbeans.api.progress.ProgressHandle;
 
   31 import org.openide.util.NbBundle;
 
   44 import org.
sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
 
   55     private static final SimpleDateFormat 
dateFormatter = 
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss z");
 
   56     private static final SimpleDateFormat 
dateFormatterISO8601 = 
new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
 
   62         throw new AssertionError();
 
   75         String time = 
"0000-00-00 00:00:00";
 
   76         if (epochSeconds != 0) {
 
   78                 dateFormatter.setTimeZone(tzone);
 
   79                 time = dateFormatter.format(
new java.util.Date(epochSeconds * 1000));
 
   95         String time = 
"0000-00-00T00:00:00Z"; 
 
   96         if (epochSeconds != 0) {
 
   98                 dateFormatterISO8601.setTimeZone(tzone);
 
   99                 time = dateFormatterISO8601.format(
new java.util.Date(epochSeconds * 1000));
 
  151             return TimeZone.getDefault();
 
  164         return content.accept(systemName);
 
  178             return cntnt.getName() + 
":" + Long.toString(cntnt.getId());
 
  201     public static <T> 
long writeToFile(Content content, java.io.File outputFile,
 
  202             ProgressHandle progress, Future<T> worker, 
boolean source) 
throws IOException {
 
  203         InputStream in = 
new ReadContentInputStream(content);
 
  206         int unit = (int) (content.getSize() / 100);
 
  209         try (FileOutputStream out = 
new FileOutputStream(outputFile, 
false)) {
 
  211             int len = in.read(buffer);
 
  214                 if (worker != null && worker.isCancelled()) {
 
  217                 out.write(buffer, 0, len);
 
  218                 len = in.read(buffer);
 
  222                 if (progress != null && source && totalRead >= TO_FILE_BUFFER_SIZE) {
 
  223                     int totalProgress = (int) (totalRead / unit);
 
  224                     progress.progress(content.getName(), totalProgress);
 
  226                 } 
else if (progress != null && !source) {
 
  227                     progress.progress(content.getName());
 
  244     public static void writeToFile(Content content, java.io.File outputFile) throws IOException {
 
  245         writeToFile(content, outputFile, null, null, 
false);
 
  262     public static long writeToFile(Content content, java.io.File outputFile,
 
  263             Supplier<Boolean> cancelCheck) 
throws IOException {
 
  264         InputStream in = 
new ReadContentInputStream(content);
 
  267         try (FileOutputStream out = 
new FileOutputStream(outputFile, 
false)) {
 
  269             int len = in.read(buffer);
 
  271                 out.write(buffer, 0, len);
 
  273                 if (cancelCheck.get()) {
 
  276                 len = in.read(buffer);
 
  300     public static long writeToFile(Content content, java.io.File outputFile,
 
  301             Supplier<Boolean> cancelCheck, 
long startingOffset, 
long endingOffset) 
throws IOException {
 
  303         InputStream in = 
new ReadContentInputStream(content);
 
  305         try (FileOutputStream out = 
new FileOutputStream(outputFile, 
false)) {
 
  306             long offsetSkipped = in.skip(startingOffset); 
 
  307             if (offsetSkipped != startingOffset) {
 
  309                 throw new IOException(String.format(
"Skipping file to starting offset {0} was not successful only skipped to offset {1}.", startingOffset, offsetSkipped));            
 
  312             int len = in.read(buffer);
 
  313             long writeFileLength = endingOffset - startingOffset;
 
  315             while (len != -1 && writeFileLength != 0) {
 
  316                 out.write(buffer, 0, len);
 
  318                 if (cancelCheck.get()) {
 
  321                 if (writeFileLength > TO_FILE_BUFFER_SIZE) {
 
  322                     len = in.read(buffer);
 
  325                     int writeLength = (int)writeFileLength;
 
  326                     byte[] lastBuffer = 
new byte[writeLength];
 
  327                     len = in.read(lastBuffer);
 
  328                     out.write(lastBuffer, 0, len);
 
  348         String name = dir.getName();
 
  349         return name.equals(
".") || name.equals(
"..");
 
  360         ProgressHandle progress;
 
  361         SwingWorker<T, V> worker;
 
  362         boolean source = 
false;
 
  377                 ProgressHandle progress, SwingWorker<T, V> worker, 
boolean source) {
 
  379             this.progress = progress;
 
  380             this.worker = worker;
 
  381             this.source = source;
 
  392         public static <T, V> 
void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker<T, V> worker) {
 
  400             } 
catch (ReadContentInputStreamException ex) {
 
  401                 logger.log(Level.WARNING,
 
  402                         String.format(
"Error reading file '%s' (id=%d).",
 
  403                                 file.getName(), file.getId()), ex); 
 
  404             } 
catch (IOException ex) {
 
  405                 logger.log(Level.SEVERE,
 
  406                         String.format(
"Error extracting file '%s' (id=%d) to '%s'.",
 
  407                                 file.getName(), file.getId(), dest.getAbsolutePath()), ex); 
 
  413         public Void 
visit(LayoutFile file) {
 
  416             } 
catch (ReadContentInputStreamException ex) {
 
  417                 logger.log(Level.WARNING,
 
  418                         String.format(
"Error reading file '%s' (id=%d).",
 
  419                                 file.getName(), file.getId()), ex); 
 
  420             } 
catch (IOException ex) {
 
  421                 logger.log(Level.SEVERE,
 
  422                         String.format(
"Error extracting unallocated content file '%s' (id=%d) to '%s'.",
 
  423                                 file.getName(), file.getId(), dest.getAbsolutePath()), ex); 
 
  429         public Void 
visit(DerivedFile file) {
 
  432             } 
catch (ReadContentInputStreamException ex) {
 
  433                 logger.log(Level.WARNING,
 
  434                         String.format(
"Error reading file '%s' (id=%d).",
 
  435                                 file.getName(), file.getId()), ex); 
 
  436             } 
catch (IOException ex) {
 
  437                 logger.log(Level.SEVERE,
 
  438                         String.format(
"Error extracting derived file '%s' (id=%d) to '%s'.",
 
  439                                 file.getName(), file.getId(), dest.getAbsolutePath()), ex); 
 
  448             } 
catch (ReadContentInputStreamException ex) {
 
  449                 logger.log(Level.WARNING,
 
  450                         String.format(
"Error reading file '%s' (id=%d).",
 
  451                                 file.getName(), file.getId()), ex); 
 
  452             } 
catch (IOException ex) {
 
  453                 logger.log(Level.SEVERE,
 
  454                         String.format(
"Error extracting local file '%s' (id=%d) to '%s'.",
 
  455                                 file.getName(), file.getId(), dest.getAbsolutePath()), ex); 
 
  464             } 
catch (ReadContentInputStreamException ex) {
 
  465                 logger.log(Level.WARNING,
 
  466                         String.format(
"Error reading file '%s' (id=%d).",
 
  467                                 file.getName(), file.getId()), ex); 
 
  468             } 
catch (IOException ex) {
 
  469                 logger.log(Level.SEVERE,
 
  470                         String.format(
"Error extracting slack file '%s' (id=%d) to '%s'.",
 
  471                                 file.getName(), file.getId(), dest.getAbsolutePath()), ex); 
 
  482         public Void 
visit(VirtualDirectory dir) {
 
  487         public Void 
visit(LocalDirectory dir) {
 
  492             String path = dest.getAbsolutePath() + java.io.File.separator
 
  494             return new java.io.File(path);
 
  507                 int numProcessed = 0;
 
  509                 for (Content child : dir.getChildren()) {
 
  510                     if (child instanceof AbstractFile) { 
 
  517                         if (worker != null && worker.isCancelled()) {
 
  520                         if (progress != null && source) {
 
  521                             progress.progress(child.getName(), numProcessed);
 
  523                         child.accept(childVisitor);
 
  527             } 
catch (TskCoreException ex) {
 
  528                 logger.log(Level.SEVERE,
 
  529                         "Trouble fetching children to extract.", ex); 
 
  537             throw new UnsupportedOperationException(NbBundle.getMessage(
this.getClass(),
 
  538                     "ContentUtils.exception.msg",
 
  539                     content.getClass().getSimpleName()));
 
static String getStringTime(long epochSeconds, TimeZone tzone)
static final SimpleDateFormat dateFormatter
String defaultVisit(Content cntnt)
static boolean shouldDisplayTimesInLocalTime()
static final Logger logger
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static long writeToFile(Content content, java.io.File outputFile, Supplier< Boolean > cancelCheck, long startingOffset, long endingOffset)
static final int TO_FILE_BUFFER_SIZE
static String getSystemName(Content content)
static String getStringTime(long epochSeconds, Content content)
static TimeZone getTimeZone(Content content)
static String getStringTimeISO8601(long epochSeconds, TimeZone tzone)
static final SimpleDateFormat dateFormatterISO8601
static final SystemNameVisitor systemName
synchronized static Logger getLogger(String name)
static String getTimeZoneForDisplays()
static String getStringTimeISO8601(long epochSeconds, Content c)
static void writeToFile(Content content, java.io.File outputFile)
static boolean displayTimesInLocalTime()
static long writeToFile(Content content, java.io.File outputFile, Supplier< Boolean > cancelCheck)
static boolean isDotDirectory(AbstractFile dir)