Sleuth Kit Java Bindings (JNI) 4.14.0
Java bindings for using The Sleuth Kit
Loading...
Searching...
No Matches
AbstractAttribute.java
Go to the documentation of this file.
1/*
2 * Sleuth Kit Data Model
3 *
4 * Copyright 2021 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 */
19package org.sleuthkit.datamodel;
20
21import java.util.Arrays;
22import java.util.Objects;
23
28public abstract class AbstractAttribute {
29
30 private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
31
32 private final BlackboardAttribute.Type attributeType;
33
34 private final int valueInt;
35 private final long valueLong;
36 private final double valueDouble;
37 private final String valueString;
38 private final byte[] valueBytes;
39
40 private SleuthkitCase sleuthkitCase;
41
52 public AbstractAttribute(BlackboardAttribute.Type attributeType, int valueInt) {
53 if (attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.INTEGER) {
54 throw new IllegalArgumentException("Type mismatched with value type");
55 }
56 this.attributeType = attributeType;
57 this.valueInt = valueInt;
58 this.valueLong = 0;
59 this.valueDouble = 0;
60 this.valueString = "";
61 this.valueBytes = new byte[0];
62 }
63
76 public AbstractAttribute(BlackboardAttribute.Type attributeType, long valueLong) {
77 if (attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.LONG
78 && attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.DATETIME) {
79 throw new IllegalArgumentException("Type mismatched with value type");
80 }
81 this.attributeType = attributeType;
82 this.valueInt = 0;
83 this.valueLong = valueLong;
84 this.valueDouble = 0;
85 this.valueString = "";
86 this.valueBytes = new byte[0];
87 }
88
99 public AbstractAttribute(BlackboardAttribute.Type attributeType, double valueDouble) {
100 if (attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.DOUBLE) {
101 throw new IllegalArgumentException("Type mismatched with value type");
102 }
103 this.attributeType = attributeType;
104 this.valueInt = 0;
105 this.valueLong = 0;
106 this.valueDouble = valueDouble;
107 this.valueString = "";
108 this.valueBytes = new byte[0];
109 }
110
121 public AbstractAttribute(BlackboardAttribute.Type attributeType, String valueString) {
122 if (attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.STRING
123 && attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON) {
124 throw new IllegalArgumentException("Type mismatched with value type");
125 }
126 this.attributeType = attributeType;
127 this.valueInt = 0;
128 this.valueLong = 0;
129 this.valueDouble = 0;
130 if (valueString == null) {
131 this.valueString = "";
132 } else {
133 this.valueString = replaceNulls(valueString).trim();
134 }
135 this.valueBytes = new byte[0];
136 }
137
148 public AbstractAttribute(BlackboardAttribute.Type attributeType, byte[] valueBytes) {
149 if (attributeType.getValueType() != BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.BYTE) {
150 throw new IllegalArgumentException("Type mismatched with value type");
151 }
152 this.attributeType = attributeType;
153 this.valueInt = 0;
154 this.valueLong = 0;
155 this.valueDouble = 0;
156 this.valueString = "";
157 if (valueBytes == null) {
158 this.valueBytes = new byte[0];
159 } else {
160 this.valueBytes = valueBytes;
161 }
162 }
163
178 int valueInt, long valueLong, double valueDouble, String valueString, byte[] valueBytes,
179 SleuthkitCase sleuthkitCase) {
180
181 this.attributeType = attributeType;
182 this.valueInt = valueInt;
183 this.valueLong = valueLong;
184 this.valueDouble = valueDouble;
185 if (valueString == null) {
186 this.valueString = "";
187 } else {
188 this.valueString = replaceNulls(valueString).trim();
189 }
190 if (valueBytes == null) {
191 this.valueBytes = new byte[0];
192 } else {
193 this.valueBytes = valueBytes;
194 }
195 this.sleuthkitCase = sleuthkitCase;
196 }
197
203 public String getDisplayString() {
204 switch (attributeType.getValueType()) {
205 case STRING:
206 return getValueString();
207 case INTEGER:
208 if (attributeType.getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_READ_STATUS.getTypeID()) {
209 if (getValueInt() == 0) {
210 return "Unread";
211 } else {
212 return "Read";
213 }
214 }
215 return Integer.toString(getValueInt());
216 case LONG:
217 return Long.toString(getValueLong());
218 case DOUBLE:
219 return Double.toString(getValueDouble());
220 case BYTE:
221 return bytesToHexString(getValueBytes());
222 case DATETIME:
223 // once we have TSK timezone, that should be used here.
225 case JSON: {
226 return getValueString();
227 }
228 }
229 return "";
230 }
231
238 return this.attributeType;
239 }
240
249
256 public int getValueInt() {
257 return valueInt;
258 }
259
266 public long getValueLong() {
267 return valueLong;
268 }
269
276 public double getValueDouble() {
277 return valueDouble;
278 }
279
287 public String getValueString() {
288 return valueString;
289 }
290
297 public byte[] getValueBytes() {
298 return Arrays.copyOf(valueBytes, valueBytes.length);
299 }
300
307 SleuthkitCase getCaseDatabase() {
308 return this.sleuthkitCase;
309 }
310
317 void setCaseDatabase(SleuthkitCase sleuthkitCase) {
318 this.sleuthkitCase = sleuthkitCase;
319 }
320
328 static String bytesToHexString(byte[] bytes) {
329 // from http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java
330 char[] hexChars = new char[bytes.length * 2];
331 for (int j = 0; j < bytes.length; j++) {
332 int v = bytes[j] & 0xFF;
333 hexChars[j * 2] = HEX_ARRAY[v >>> 4];
334 hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
335 }
336 return new String(hexChars);
337 }
338
346 static String replaceNulls(String text) {
347 return text.replace((char) 0x00, (char) 0x1A);
348 }
349
358 boolean areValuesEqual(Object that) {
359 if (that instanceof AbstractAttribute) {
361 Object[] thisObject = new Object[]{this.getAttributeType(), this.getValueInt(), this.getValueLong(), this.getValueDouble(),
362 this.getValueString(), this.getValueBytes()};
363 Object[] otherObject = new Object[]{other.getAttributeType(), other.getValueInt(), other.getValueLong(), other.getValueDouble(),
364 other.getValueString(), other.getValueBytes()};
365 return Objects.deepEquals(thisObject, otherObject);
366 } else {
367 return false;
368 }
369 }
370}
AbstractAttribute(BlackboardAttribute.Type attributeType, String valueString)
AbstractAttribute(BlackboardAttribute.Type attributeType, int valueInt)
AbstractAttribute(BlackboardAttribute.Type attributeType, byte[] valueBytes)
AbstractAttribute(BlackboardAttribute.Type attributeType, double valueDouble)
BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE getValueType()
AbstractAttribute(BlackboardAttribute.Type attributeType, long valueLong)
static String epochToTime(long epoch)

Copyright © 2011-2024 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.