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) {
410 protected void writeFile(Content file, java.io.File dest, ProgressHandle progress, SwingWorker<T, V> worker,
boolean source)
throws IOException {
420 protected Void
visitFile(Content file, String fileType) {
422 writeFile(file, dest, progress, worker, source);
423 }
catch (ReadContentInputStreamException ex) {
424 logger.log(Level.WARNING,
425 String.format(
"Error reading file '%s' (id=%d).",
426 file.getName(), file.getId()), ex);
427 }
catch (IOException ex) {
428 logger.log(Level.SEVERE,
429 String.format(
"Error extracting %s '%s' (id=%d) to '%s'.",
430 fileType, file.getName(), file.getId(), dest.getAbsolutePath()), ex);
441 public Void
visit(LayoutFile file) {
442 return visitFile(file,
"unallocated content file");
446 public Void
visit(DerivedFile file) {
466 public Void
visit(VirtualDirectory dir) {
471 public Void
visit(LocalDirectory dir) {
476 String path = dest.getAbsolutePath() + java.io.File.separator
478 return new java.io.File(path);
505 int numProcessed = 0;
507 for (Content child : dir.getChildren()) {
508 if (child instanceof AbstractFile) {
514 if (worker != null && worker.isCancelled()) {
517 if (progress != null && source) {
518 progress.progress(child.getName(), numProcessed);
520 child.accept(childVisitor);
524 }
catch (TskCoreException ex) {
525 logger.log(Level.SEVERE,
526 "Trouble fetching children to extract.", ex);
534 throw new UnsupportedOperationException(NbBundle.getMessage(
this.getClass(),
535 "ContentUtils.exception.msg",
536 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)