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 java.util.prefs.PreferenceChangeEvent;
30 import java.util.prefs.PreferenceChangeListener;
31 import javax.swing.SwingWorker;
32 import org.netbeans.api.progress.ProgressHandle;
33 import org.openide.util.NbBundle;
47 import org.
sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
59 private static final SimpleDateFormat
dateFormatter =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss z");
60 private static final SimpleDateFormat
dateFormatterISO8601 =
new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
65 public void preferenceChange(PreferenceChangeEvent evt) {
77 throw new AssertionError();
89 String time =
"0000-00-00 00:00:00";
90 if (epochSeconds != 0) {
92 dateFormatter.setTimeZone(tzone);
93 time = dateFormatter.format(
new java.util.Date(epochSeconds * 1000));
108 String time =
"0000-00-00T00:00:00Z";
109 if (epochSeconds != 0) {
111 dateFormatterISO8601.setTimeZone(tzone);
112 time = dateFormatterISO8601.format(
new java.util.Date(epochSeconds * 1000));
150 final Content dataSource = content.getDataSource();
151 if ((dataSource != null) && (dataSource instanceof Image)) {
152 Image image = (Image) dataSource;
153 return TimeZone.getTimeZone(image.getTimeZone());
156 return TimeZone.getDefault();
159 }
catch (TskCoreException ex) {
160 return TimeZone.getDefault();
173 return content.accept(systemName);
187 return cntnt.getName() +
":" + Long.toString(cntnt.getId());
210 public static <T>
long writeToFile(Content content, java.io.File outputFile,
211 ProgressHandle progress, Future<T> worker,
boolean source)
throws IOException {
212 InputStream in =
new ReadContentInputStream(content);
215 int unit = (int) (content.getSize() / 100);
218 try (FileOutputStream out =
new FileOutputStream(outputFile,
false)) {
220 int len = in.read(buffer);
223 if (worker != null && worker.isCancelled()) {
226 out.write(buffer, 0, len);
227 len = in.read(buffer);
231 if (progress != null && source && totalRead >= TO_FILE_BUFFER_SIZE) {
232 int totalProgress = (int) (totalRead / unit);
233 progress.progress(content.getName(), totalProgress);
235 }
else if (progress != null && !source) {
236 progress.progress(content.getName());
253 public static void writeToFile(Content content, java.io.File outputFile) throws IOException {
254 writeToFile(content, outputFile, null, null,
false);
271 public static long writeToFile(Content content, java.io.File outputFile,
272 Supplier<Boolean> cancelCheck)
throws IOException {
273 InputStream in =
new ReadContentInputStream(content);
276 try (FileOutputStream out =
new FileOutputStream(outputFile,
false)) {
278 int len = in.read(buffer);
280 out.write(buffer, 0, len);
282 if (cancelCheck.get()) {
285 len = in.read(buffer);
301 String name = dir.getName();
302 return name.equals(
".") || name.equals(
"..");
313 ProgressHandle progress;
314 SwingWorker<T, V> worker;
315 boolean source =
false;
330 ProgressHandle progress, SwingWorker<T, V> worker,
boolean source) {
332 this.progress = progress;
333 this.worker = worker;
334 this.source = source;
345 public static <T, V>
void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker<T, V> worker) {
353 }
catch (ReadContentInputStreamException ex) {
354 logger.log(Level.WARNING,
355 String.format(
"Error reading file '%s' (id=%d).",
356 file.getName(), file.getId()), ex);
357 }
catch (IOException ex) {
358 logger.log(Level.SEVERE,
359 String.format(
"Error extracting file '%s' (id=%d) to '%s'.",
360 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
366 public Void
visit(LayoutFile file) {
369 }
catch (ReadContentInputStreamException ex) {
370 logger.log(Level.WARNING,
371 String.format(
"Error reading file '%s' (id=%d).",
372 file.getName(), file.getId()), ex);
373 }
catch (IOException ex) {
374 logger.log(Level.SEVERE,
375 String.format(
"Error extracting unallocated content file '%s' (id=%d) to '%s'.",
376 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
382 public Void
visit(DerivedFile file) {
385 }
catch (ReadContentInputStreamException ex) {
386 logger.log(Level.WARNING,
387 String.format(
"Error reading file '%s' (id=%d).",
388 file.getName(), file.getId()), ex);
389 }
catch (IOException ex) {
390 logger.log(Level.SEVERE,
391 String.format(
"Error extracting derived file '%s' (id=%d) to '%s'.",
392 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
401 }
catch (ReadContentInputStreamException ex) {
402 logger.log(Level.WARNING,
403 String.format(
"Error reading file '%s' (id=%d).",
404 file.getName(), file.getId()), ex);
405 }
catch (IOException ex) {
406 logger.log(Level.SEVERE,
407 String.format(
"Error extracting local file '%s' (id=%d) to '%s'.",
408 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
417 }
catch (ReadContentInputStreamException ex) {
418 logger.log(Level.WARNING,
419 String.format(
"Error reading file '%s' (id=%d).",
420 file.getName(), file.getId()), ex);
421 }
catch (IOException ex) {
422 logger.log(Level.SEVERE,
423 String.format(
"Error extracting slack file '%s' (id=%d) to '%s'.",
424 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
435 public Void
visit(VirtualDirectory dir) {
440 public Void
visit(LocalDirectory dir) {
445 String path = dest.getAbsolutePath() + java.io.File.separator
447 return new java.io.File(path);
460 int numProcessed = 0;
462 for (Content child : dir.getChildren()) {
463 if (child instanceof AbstractFile) {
470 if (worker != null && worker.isCancelled()) {
473 if (progress != null && source) {
474 progress.progress(child.getName(), numProcessed);
476 child.accept(childVisitor);
480 }
catch (TskCoreException ex) {
481 logger.log(Level.SEVERE,
482 "Trouble fetching children to extract.", ex);
490 throw new UnsupportedOperationException(NbBundle.getMessage(
this.getClass(),
491 "ContentUtils.exception.msg",
492 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 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 String DISPLAY_TIMES_IN_LOCAL_TIME
static final SystemNameVisitor systemName
synchronized static Logger getLogger(String name)
static String getTimeZoneForDisplays()
static String getStringTimeISO8601(long epochSeconds, Content c)
static void addChangeListener(PreferenceChangeListener listener)
static boolean displayTimesInLocalTime
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)