|
| 1 | +package com.pff; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.List; |
| 6 | +import java.util.UUID; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class to hold decoded PidTagConversationIndex |
| 10 | + * |
| 11 | + * @author Nick Buller |
| 12 | + */ |
| 13 | +public class PSTConversationIndex { |
| 14 | + private static final int HUNDRED_NS_TO_MS = 1000; |
| 15 | + private static final int MINIMUM_HEADER_SIZE = 22; |
| 16 | + private static final int RESPONSE_LEVEL_SIZE = 5; |
| 17 | + |
| 18 | + private Date deliveryTime; |
| 19 | + private UUID guid; |
| 20 | + private List<ResponseLevel> responseLevels = new ArrayList<ResponseLevel>(); |
| 21 | + |
| 22 | + public Date getDeliveryTime() { |
| 23 | + return deliveryTime; |
| 24 | + } |
| 25 | + |
| 26 | + public UUID getGuid() { |
| 27 | + return guid; |
| 28 | + } |
| 29 | + |
| 30 | + public List<ResponseLevel> getResponseLevels() { |
| 31 | + return responseLevels; |
| 32 | + } |
| 33 | + |
| 34 | + public String toString() { |
| 35 | + return guid + "@" + deliveryTime + " " + responseLevels.size() + " ResponseLevels"; |
| 36 | + } |
| 37 | + |
| 38 | + public class ResponseLevel { |
| 39 | + short deltaCode; |
| 40 | + long timeDelta; |
| 41 | + short random; |
| 42 | + |
| 43 | + public ResponseLevel(short deltaCode, long timeDelta, short random) { |
| 44 | + this.deltaCode = deltaCode; |
| 45 | + this.timeDelta = timeDelta; |
| 46 | + this.random = random; |
| 47 | + } |
| 48 | + |
| 49 | + public short getDeltaCode() { |
| 50 | + return deltaCode; |
| 51 | + } |
| 52 | + |
| 53 | + public long getTimeDelta() { |
| 54 | + return timeDelta; |
| 55 | + } |
| 56 | + |
| 57 | + public short getRandom() { |
| 58 | + return random; |
| 59 | + } |
| 60 | + |
| 61 | + public Date withOffset(Date anchorDate) { |
| 62 | + return new Date(anchorDate.getTime() + timeDelta); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + protected PSTConversationIndex(byte[] rawConversationIndex) { |
| 67 | + if (rawConversationIndex != null && rawConversationIndex.length >= MINIMUM_HEADER_SIZE) { |
| 68 | + decodeHeader(rawConversationIndex); |
| 69 | + if (rawConversationIndex.length >= MINIMUM_HEADER_SIZE + RESPONSE_LEVEL_SIZE) { |
| 70 | + decodeResponseLevel(rawConversationIndex); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void decodeHeader(byte[] rawConversationIndex) { |
| 76 | + // According to the Spec the first byte is not included, but I believe the spec is incorrect! |
| 77 | + //int reservedheaderMarker = (int) PSTObject.convertBigEndianBytesToLong(rawConversationIndex, 0, 1); |
| 78 | + |
| 79 | + long deliveryTimeHigh = PSTObject.convertBigEndianBytesToLong(rawConversationIndex, 0, 4); |
| 80 | + long deliveryTimeLow = PSTObject.convertBigEndianBytesToLong(rawConversationIndex, 4, 6) << 16; |
| 81 | + deliveryTime = PSTObject.filetimeToDate((int) deliveryTimeHigh, (int) deliveryTimeLow); |
| 82 | + |
| 83 | + long guidHigh = PSTObject.convertBigEndianBytesToLong(rawConversationIndex, 6, 14); |
| 84 | + long guidLow = PSTObject.convertBigEndianBytesToLong(rawConversationIndex, 14, 22); |
| 85 | + |
| 86 | + guid = new UUID(guidHigh, guidLow); |
| 87 | + } |
| 88 | + |
| 89 | + private void decodeResponseLevel(byte[] rawConversationIndex) { |
| 90 | + int responseLevelCount = (rawConversationIndex.length - MINIMUM_HEADER_SIZE) / RESPONSE_LEVEL_SIZE; |
| 91 | + responseLevels = new ArrayList<ResponseLevel>(responseLevelCount); |
| 92 | + |
| 93 | + for (int responseLevelIndex = 0, position = 22; responseLevelIndex < responseLevelCount; responseLevelIndex++, position += RESPONSE_LEVEL_SIZE) { |
| 94 | + |
| 95 | + long responseLevelValue = PSTObject.convertBigEndianBytesToLong(rawConversationIndex, position, position + 5); |
| 96 | + short deltaCode = (short) (responseLevelValue >> 39); |
| 97 | + short random = (short) (responseLevelValue & 0xFF); |
| 98 | + |
| 99 | + // shift by 1 byte (remove the random) and mask off the deltaCode |
| 100 | + long deltaTime = (responseLevelValue >> 8) & 0x7FFFFFFF; |
| 101 | + |
| 102 | + if (deltaCode == 0) { |
| 103 | + deltaTime <<= 18; |
| 104 | + } else { |
| 105 | + deltaTime <<= 23; |
| 106 | + } |
| 107 | + |
| 108 | + deltaTime /= HUNDRED_NS_TO_MS; |
| 109 | + |
| 110 | + responseLevels.add(responseLevelIndex, new ResponseLevel(deltaCode, deltaTime, random)); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments