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);
309 public static long writeToFile(Content content, java.io.File outputFile,
310 Supplier<Boolean> cancelCheck,
long startingOffset,
long endingOffset)
throws IOException {
312 InputStream in =
new ReadContentInputStream(content);
314 try (FileOutputStream out =
new FileOutputStream(outputFile,
false)) {
315 long offsetSkipped = in.skip(startingOffset);
316 if (offsetSkipped != startingOffset) {
318 throw new IOException(String.format(
"Skipping file to starting offset {0} was not successful only skipped to offset {1}.", startingOffset, offsetSkipped));
321 int len = in.read(buffer);
322 long writeFileLength = endingOffset - startingOffset;
324 while (len != -1 && writeFileLength != 0) {
325 out.write(buffer, 0, len);
327 if (cancelCheck.get()) {
330 if (writeFileLength > TO_FILE_BUFFER_SIZE) {
331 len = in.read(buffer);
334 int writeLength = (int)writeFileLength;
335 byte[] lastBuffer =
new byte[writeLength];
336 len = in.read(lastBuffer);
337 out.write(lastBuffer, 0, len);
357 String name = dir.getName();
358 return name.equals(
".") || name.equals(
"..");
369 ProgressHandle progress;
370 SwingWorker<T, V> worker;
371 boolean source =
false;
386 ProgressHandle progress, SwingWorker<T, V> worker,
boolean source) {
388 this.progress = progress;
389 this.worker = worker;
390 this.source = source;
401 public static <T, V>
void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker<T, V> worker) {
409 }
catch (ReadContentInputStreamException ex) {
410 logger.log(Level.WARNING,
411 String.format(
"Error reading file '%s' (id=%d).",
412 file.getName(), file.getId()), ex);
413 }
catch (IOException ex) {
414 logger.log(Level.SEVERE,
415 String.format(
"Error extracting file '%s' (id=%d) to '%s'.",
416 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
422 public Void
visit(LayoutFile file) {
425 }
catch (ReadContentInputStreamException ex) {
426 logger.log(Level.WARNING,
427 String.format(
"Error reading file '%s' (id=%d).",
428 file.getName(), file.getId()), ex);
429 }
catch (IOException ex) {
430 logger.log(Level.SEVERE,
431 String.format(
"Error extracting unallocated content file '%s' (id=%d) to '%s'.",
432 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
438 public Void
visit(DerivedFile file) {
441 }
catch (ReadContentInputStreamException ex) {
442 logger.log(Level.WARNING,
443 String.format(
"Error reading file '%s' (id=%d).",
444 file.getName(), file.getId()), ex);
445 }
catch (IOException ex) {
446 logger.log(Level.SEVERE,
447 String.format(
"Error extracting derived file '%s' (id=%d) to '%s'.",
448 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
457 }
catch (ReadContentInputStreamException ex) {
458 logger.log(Level.WARNING,
459 String.format(
"Error reading file '%s' (id=%d).",
460 file.getName(), file.getId()), ex);
461 }
catch (IOException ex) {
462 logger.log(Level.SEVERE,
463 String.format(
"Error extracting local file '%s' (id=%d) to '%s'.",
464 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
473 }
catch (ReadContentInputStreamException ex) {
474 logger.log(Level.WARNING,
475 String.format(
"Error reading file '%s' (id=%d).",
476 file.getName(), file.getId()), ex);
477 }
catch (IOException ex) {
478 logger.log(Level.SEVERE,
479 String.format(
"Error extracting slack file '%s' (id=%d) to '%s'.",
480 file.getName(), file.getId(), dest.getAbsolutePath()), ex);
491 public Void
visit(VirtualDirectory dir) {
496 public Void
visit(LocalDirectory dir) {
501 String path = dest.getAbsolutePath() + java.io.File.separator
503 return new java.io.File(path);
516 int numProcessed = 0;
518 for (Content child : dir.getChildren()) {
519 if (child instanceof AbstractFile) {
526 if (worker != null && worker.isCancelled()) {
529 if (progress != null && source) {
530 progress.progress(child.getName(), numProcessed);
532 child.accept(childVisitor);
536 }
catch (TskCoreException ex) {
537 logger.log(Level.SEVERE,
538 "Trouble fetching children to extract.", ex);
546 throw new UnsupportedOperationException(NbBundle.getMessage(
this.getClass(),
547 "ContentUtils.exception.msg",
548 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 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)