-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathPSTFolder.java
More file actions
455 lines (418 loc) · 17.3 KB
/
PSTFolder.java
File metadata and controls
455 lines (418 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* Copyright 2010 Richard Johnson & Orin Eman
*
* 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.
*
* ---
*
* This file is part of java-libpst.
*
* java-libpst is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-libpst is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with java-libpst. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.pff;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
/**
* Represents a folder in the PST File
* Allows you to access child folders or items. Items are accessed through a
* sort of cursor arrangement.
* This allows for incremental reading of a folder which may have _lots_ of
* emails.
*
* @author Richard Johnson
*/
public class PSTFolder extends PSTObject {
/**
* a constructor for the rest of us...
*
* @param theFile the the file
* @param descriptorIndexNode the descriptor index node
* @throws PSTException the pst exception
* @throws IOException the io exception
*/
PSTFolder(final PSTFile theFile, final DescriptorIndexNode descriptorIndexNode) throws PSTException, IOException {
super(theFile, descriptorIndexNode);
}
/**
* For pre-populating a folder object with values.
* Not recommended for use outside this library
*
* @param theFile the the file
* @param folderIndexNode the folder index node
* @param table the table
* @param localDescriptorItems the local descriptor items
*/
PSTFolder(final PSTFile theFile, final DescriptorIndexNode folderIndexNode, final PSTTableBC table,
final HashMap<Integer, PSTDescriptorItem> localDescriptorItems) {
super(theFile, folderIndexNode, table, localDescriptorItems);
}
/**
* get all of the sub folders...
* there are not usually thousands, so we just do it in one big operation.
*
* @return all of the subfolders
* @throws PSTException the pst exception
* @throws IOException the io exception
*/
public Vector<PSTFolder> getSubFolders() throws PSTException, IOException {
final Vector<PSTFolder> output = new Vector<>();
try {
this.initSubfoldersTable();
final List<HashMap<Integer, PSTTable7CItem>> itemMapSet = this.subfoldersTable.getItems();
for (final HashMap<Integer, PSTTable7CItem> itemMap : itemMapSet) {
final PSTTable7CItem item = itemMap.get(26610);
final PSTFolder folder = (PSTFolder) PSTObject.detectAndLoadPSTObject(this.pstFile,
item.entryValueReference);
output.add(folder);
}
} catch (final PSTException err) {
// hierarchy node doesn't exist: This is OK if child count is 0.
// Seen with special search folders at the top of the hierarchy:
// "8739 - SPAM Search Folder 2", "8739 - Content.Filter".
// this.subfoldersTable may remain uninitialized (null) in that case.
if (this.getContentCount() != 0) {
if (err.getMessage().startsWith("Can't get child folders")) {
throw err;
} else {
//err.printStackTrace();
throw new PSTException("Can't get child folders for folder " + this.getDisplayName() + "("
+ this.getDescriptorNodeId() + ") child count: " + this.getContentCount() + " - " + err.toString(), err);
}
}
}
return output;
}
private void initSubfoldersTable() throws IOException, PSTException {
if (this.subfoldersTable != null) {
return;
}
final long folderDescriptorIndex = this.descriptorIndexNode.descriptorIdentifier + 11;
try {
final DescriptorIndexNode folderDescriptor = this.pstFile.getDescriptorIndexNode(folderDescriptorIndex);
HashMap<Integer, PSTDescriptorItem> tmp = null;
if (folderDescriptor.localDescriptorsOffsetIndexIdentifier > 0) {
// tmp = new PSTDescriptor(pstFile,
// folderDescriptor.localDescriptorsOffsetIndexIdentifier).getChildren();
tmp = this.pstFile.getPSTDescriptorItems(folderDescriptor.localDescriptorsOffsetIndexIdentifier);
}
this.subfoldersTable = new PSTTable7C(new PSTNodeInputStream(this.pstFile,
this.pstFile.getOffsetIndexNode(folderDescriptor.dataOffsetIndexIdentifier)), tmp);
} catch (final PSTException err) {
// hierarchy node doesn't exist
throw new PSTException("Can't get child folders for folder " + this.getDisplayName() + "("
+ this.getDescriptorNodeId() + ") child count: " + this.getContentCount() + " - " + err.toString(), err);
}
}
/**
* internal vars for the tracking of things..
*/
private int currentEmailIndex = 0;
private final LinkedHashSet<DescriptorIndexNode> otherItems = null;
private PSTTable7C emailsTable = null;
private LinkedList<DescriptorIndexNode> fallbackEmailsTable = null;
private PSTTable7C subfoldersTable = null;
/**
* this method goes through all of the children and sorts them into one of
* the three hash sets.
*
* @throws PSTException the pst exception
* @throws IOException the io exception
*/
private void initEmailsTable() throws PSTException, IOException {
if (this.emailsTable != null || this.fallbackEmailsTable != null) {
return;
}
// some folder types don't have children:
if (this.getNodeType() == PSTObject.NID_TYPE_SEARCH_FOLDER) {
return;
}
try {
final long folderDescriptorIndex = this.descriptorIndexNode.descriptorIdentifier + 12; // +12
// lists
// emails!
// :D
final DescriptorIndexNode folderDescriptor = this.pstFile.getDescriptorIndexNode(folderDescriptorIndex);
HashMap<Integer, PSTDescriptorItem> tmp = null;
if (folderDescriptor.localDescriptorsOffsetIndexIdentifier > 0) {
// tmp = new PSTDescriptor(pstFile,
// folderDescriptor.localDescriptorsOffsetIndexIdentifier).getChildren();
tmp = this.pstFile.getPSTDescriptorItems(folderDescriptor.localDescriptorsOffsetIndexIdentifier);
}
// PSTTable7CForFolder folderDescriptorTable = new
// PSTTable7CForFolder(folderDescriptor.dataBlock.data,
// folderDescriptor.dataBlock.blockOffsets,tmp, 0x67F2);
this.emailsTable = new PSTTable7C(new PSTNodeInputStream(this.pstFile,
this.pstFile.getOffsetIndexNode(folderDescriptor.dataOffsetIndexIdentifier)), tmp, 0x67F2);
} catch (final Exception err) {
// here we have to attempt to fallback onto the children as listed
// by the descriptor b-tree
final LinkedHashMap<Integer, LinkedList<DescriptorIndexNode>> tree = this.pstFile.getChildDescriptorTree();
this.fallbackEmailsTable = new LinkedList<>();
final LinkedList<DescriptorIndexNode> allChildren = tree.get(this.getDescriptorNode().descriptorIdentifier);
if (allChildren != null) {
// quickly go through and remove those entries that are not
// messages!
for (final DescriptorIndexNode node : allChildren) {
if (node != null
&& PSTObject.getNodeType(node.descriptorIdentifier) == PSTObject.NID_TYPE_NORMAL_MESSAGE) {
this.fallbackEmailsTable.add(node);
}
}
}
System.err.println("Can't get children for folder " + this.getDisplayName() + "("
+ this.getDescriptorNodeId() + ") child count: " + this.getContentCount() + " - " + err.toString()
+ ", using alternate child tree with " + this.fallbackEmailsTable.size() + " items");
}
}
/**
* get some children from the folder
* This is implemented as a cursor of sorts, as there could be thousands
* and that is just too many to process at once.
*
* @param numberToReturn the number to return
* @return bunch of children in this folder
* @throws PSTException the pst exception
* @throws IOException the io exception
*/
public Vector<PSTObject> getChildren(final int numberToReturn) throws PSTException, IOException {
this.initEmailsTable();
final Vector<PSTObject> output = new Vector<>();
if (this.emailsTable != null) {
final List<HashMap<Integer, PSTTable7CItem>> rows = this.emailsTable.getItems(this.currentEmailIndex,
numberToReturn);
for (int x = 0; x < rows.size(); x++) {
if (this.currentEmailIndex >= this.getContentCount()) {
// no more!
break;
}
// get the emails from the rows
final PSTTable7CItem emailRow = rows.get(x).get(0x67F2);
final DescriptorIndexNode childDescriptor = this.pstFile
.getDescriptorIndexNode(emailRow.entryValueReference);
final PSTObject child = PSTObject.detectAndLoadPSTObject(this.pstFile, childDescriptor);
output.add(child);
this.currentEmailIndex++;
}
} else if (this.fallbackEmailsTable != null) {
// we use the fallback
final ListIterator<DescriptorIndexNode> iterator = this.fallbackEmailsTable
.listIterator(this.currentEmailIndex);
for (int x = 0; x < numberToReturn; x++) {
if (this.currentEmailIndex >= this.getContentCount()) {
// no more!
break;
}
final DescriptorIndexNode childDescriptor = iterator.next();
final PSTObject child = PSTObject.detectAndLoadPSTObject(this.pstFile, childDescriptor);
output.add(child);
this.currentEmailIndex++;
}
}
return output;
}
/**
* Gets child descriptor nodes.
*
* @return the child descriptor nodes
* @throws PSTException the pst exception
* @throws IOException the io exception
*/
public LinkedList<Integer> getChildDescriptorNodes() throws PSTException, IOException {
this.initEmailsTable();
if (this.emailsTable == null) {
return new LinkedList<>();
}
final LinkedList<Integer> output = new LinkedList<>();
final List<HashMap<Integer, PSTTable7CItem>> rows = this.emailsTable.getItems();
for (final HashMap<Integer, PSTTable7CItem> row : rows) {
// get the emails from the rows
if (this.currentEmailIndex == this.getContentCount()) {
// no more!
break;
}
final PSTTable7CItem emailRow = row.get(0x67F2);
if (emailRow.entryValueReference == 0) {
break;
}
output.add(emailRow.entryValueReference);
}
return output;
}
/**
* Get the next child of this folder
* As there could be thousands of emails, we have these kind of cursor
* operations
*
* @return the next email in the folder or null if at the end of the folder
* @throws PSTException the pst exception
* @throws IOException the io exception
*/
public PSTObject getNextChild() throws PSTException, IOException {
this.initEmailsTable();
if (this.emailsTable != null) {
final List<HashMap<Integer, PSTTable7CItem>> rows = this.emailsTable.getItems(this.currentEmailIndex, 1);
if (this.currentEmailIndex == this.getContentCount()) {
// no more!
return null;
}
this.currentEmailIndex++;
// get the emails from the rows
final PSTTable7CItem emailRow = rows.get(0).get(0x67F2);
final DescriptorIndexNode childDescriptor = this.pstFile
.getDescriptorIndexNode(emailRow.entryValueReference);
final PSTObject child = PSTObject.detectAndLoadPSTObject(this.pstFile, childDescriptor);
return child;
} else if (this.fallbackEmailsTable != null) {
if (this.currentEmailIndex >= this.getContentCount()
|| this.currentEmailIndex >= this.fallbackEmailsTable.size()) {
// no more!
return null;
}
this.currentEmailIndex++;
// get the emails from the rows
final DescriptorIndexNode childDescriptor = this.fallbackEmailsTable.get(this.currentEmailIndex);
final PSTObject child = PSTObject.detectAndLoadPSTObject(this.pstFile, childDescriptor);
return child;
}
return null;
}
/**
* move the internal folder cursor to the desired position
* position 0 is before the first record.
*
* @param newIndex the new index
* @throws IOException the io exception
* @throws PSTException the pst exception
*/
public void moveChildCursorTo(int newIndex) throws IOException, PSTException {
this.initEmailsTable();
if (newIndex < 1) {
this.currentEmailIndex = 0;
return;
}
if (newIndex > this.getContentCount()) {
newIndex = this.getContentCount();
}
this.currentEmailIndex = newIndex;
}
/**
* the number of child folders in this folder
*
* @return number of subfolders as counted
* @throws IOException the io exception
* @throws PSTException the pst exception
*/
public int getSubFolderCount() throws IOException, PSTException {
this.initSubfoldersTable();
if (this.subfoldersTable != null) {
return this.subfoldersTable.getRowCount();
} else {
return 0;
}
}
/**
* the number of emails in this folder
* this is the count of emails made by the library and will therefore should
* be more accurate than getContentCount
*
* @return number of emails in this folder (as counted)
* @throws IOException the io exception
* @throws PSTException the pst exception
*/
public int getEmailCount() throws IOException, PSTException {
this.initEmailsTable();
if (this.emailsTable == null) {
return -1;
}
return this.emailsTable.getRowCount();
}
/**
* Gets folder type.
*
* @return the folder type
*/
public int getFolderType() {
return this.getIntItem(0x3601);
}
/**
* the number of emails in this folder
* this is as reported by the PST file, for a number calculated by the
* library use getEmailCount
*
* @return number of items as reported by PST File
*/
public int getContentCount() {
return this.getIntItem(0x3602);
}
/**
* Amount of unread content items Integer 32-bit signed
*
* @return the unread count
*/
public int getUnreadCount() {
return this.getIntItem(0x3603);
}
/**
* does this folder have subfolders
* once again, read from the PST, use getSubFolderCount if you want to know
* what the library makes of it all
*
* @return has subfolders as reported by the PST File
*/
public boolean hasSubfolders() {
return (this.getIntItem(0x360a) != 0);
}
/**
* Gets container class.
*
* @return the container class
*/
public String getContainerClass() {
return this.getStringItem(0x3613);
}
/**
* Gets associate content count.
*
* @return the associate content count
*/
public int getAssociateContentCount() {
return this.getIntItem(0x3617);
}
/**
* Container flags Integer 32-bit signed
*
* @return the container flags
*/
public int getContainerFlags() {
return this.getIntItem(0x3600);
}
}
// vim: set noexpandtab: