Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DriveListUtils.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.logicalimager.dsp;
20 
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.util.Arrays;
25 import java.util.List;
26 
30 public final class DriveListUtils {
31 
41  public static String humanReadableByteCount(long bytes, boolean si) {
42  int unit = si ? 1000 : 1024;
43  if (bytes < unit) {
44  return bytes + " B"; //NON-NLS
45  }
46  int exp = (int) (Math.log(bytes) / Math.log(unit));
47  String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); //NON-NLS
48  return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); //NON-NLS
49  }
50 
54  private DriveListUtils() {
55  //empty private constructor for util class
56  }
57 
61  public static boolean isNetworkDrive(String driveLetter) {
62  List<String> cmd = Arrays.asList("cmd", "/c", "net", "use", driveLetter + ":");
63 
64  try {
65  Process p = new ProcessBuilder(cmd)
66  .redirectErrorStream(true)
67  .start();
68 
69  p.getOutputStream().close();
70 
71  StringBuilder consoleOutput = new StringBuilder();
72 
73  String line;
74  try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
75  while ((line = in.readLine()) != null) {
76  consoleOutput.append(line).append("\r\n");
77  }
78  }
79 
80  int rc = p.waitFor();
81  return rc == 0;
82  } catch(IOException | InterruptedException e) {
83  return false; // assume not a network drive
84  }
85  }
86 }
static boolean isNetworkDrive(String driveLetter)
static String humanReadableByteCount(long bytes, boolean si)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.