Add new drive and time zone utility classes

This commit is contained in:
Richard Cordovano 2016-07-21 15:11:13 -04:00
parent 7f8035fd3f
commit 1fa69220ed
2 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,91 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2011-2016 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> 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() {
}
}

View File

@ -0,0 +1,69 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2011-2016 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> 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() {
}
}