From 1fa69220ed10e769819d13c155d271eb28a06640 Mon Sep 17 00:00:00 2001 From: Richard Cordovano Date: Thu, 21 Jul 2016 15:11:13 -0400 Subject: [PATCH] Add new drive and time zone utility classes --- .../autopsy/coreutils/DriveUtils.java | 91 +++++++++++++++++++ .../autopsy/coreutils/TimeZoneUtils.java | 69 ++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 Core/src/org/sleuthkit/autopsy/coreutils/DriveUtils.java create mode 100644 Core/src/org/sleuthkit/autopsy/coreutils/TimeZoneUtils.java diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/DriveUtils.java b/Core/src/org/sleuthkit/autopsy/coreutils/DriveUtils.java new file mode 100644 index 0000000000..950ffb5bcd --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/coreutils/DriveUtils.java @@ -0,0 +1,91 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2011-2016 Basis Technology Corp. + * Contact: carrier sleuthkit org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.coreutils; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +/** + * Utility methods for working with drives. + */ +public class DriveUtils { + + private static final String PDISK = "\\\\.\\physicaldrive"; //NON-NLS + private static final String DEV = "/dev/"; //NON-NLS + private static final String PARTITION = "\\\\.\\"; + private static final String COLON = ":"; + + /** + * Determines whether or not a given path is for a physical drive. + * + * @param path The path to test. + * + * @return True or false. + */ + public static boolean isPhysicalDrive(String path) { + return path.toLowerCase().startsWith(PDISK) || path.toLowerCase().startsWith(DEV); + } + + /** + * Determines whether or not a given path is for a local drive or partition. + * + * @param path The path to test. + * + * @return True or false. + */ + public static boolean isPartition(String path) { + return path.toLowerCase().startsWith(PARTITION) && path.toLowerCase().endsWith(COLON); + } + + /** + * Determines whether or not a drive exists by eading the first byte and + * checking if it is a -1. + * + * @param path The path to test. + * + * @return True or false. + */ + public static boolean driveExists(String path) { + BufferedInputStream br = null; + try { + File tmp = new File(path); + br = new BufferedInputStream(new FileInputStream(tmp)); + int b = br.read(); + return b != -1; + } catch (Exception ex) { + return false; + } finally { + try { + if (br != null) { + br.close(); + } + } catch (IOException ex) { + } + } + } + + /** + * Prevents instantiation. + */ + private DriveUtils() { + + } +} diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/TimeZoneUtils.java b/Core/src/org/sleuthkit/autopsy/coreutils/TimeZoneUtils.java new file mode 100644 index 0000000000..fa175a995a --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/coreutils/TimeZoneUtils.java @@ -0,0 +1,69 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2011-2016 Basis Technology Corp. + * Contact: carrier sleuthkit org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.coreutils; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.GregorianCalendar; + +/** + * Utility methods for workig with time zones. + */ +public class TimeZoneUtils { + + /** + * Converts a Java timezone id to a coded string with only alphanumeric + * characters. Example: "America/New_York" is converted to "EST5EDT" by this + * method. + * + * @param timeZoneId The time zone id. + * + * @return The converted time zone string. + */ + public static String convertToAlphaNumericFormat(String timeZoneId) { + + java.util.TimeZone zone = java.util.TimeZone.getTimeZone(timeZoneId); + int offset = zone.getRawOffset() / 1000; + int hour = offset / 3600; + int min = (offset % 3600) / 60; + + DateFormat dfm = new SimpleDateFormat("z"); + dfm.setTimeZone(zone); + boolean hasDaylight = zone.useDaylightTime(); + String first = dfm.format(new GregorianCalendar(2010, 1, 1).getTime()).substring(0, 3); + String second = dfm.format(new GregorianCalendar(2011, 6, 6).getTime()).substring(0, 3); + int mid = hour * -1; + String result = first + Integer.toString(mid); + if (min != 0) { + result = result + ":" + Integer.toString(min); + } + if (hasDaylight) { + result += second; + } + + return result; + } + + /** + * Prevents instantiation. + */ + private TimeZoneUtils() { + + } +}