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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="text/html; charset=ISO-8859-1">
<title></title>
<style type="text/css">
body { background-color: white; font-size: normal; color: black ; }
a { color: #525d76; }
a.black { color: #000000;}
table {border-width: 0; width: 100%}
table.centered {text-align: center}
table.title {text-align: center; width: 80%}
img{border-width: 0;}
span.s1 {font-family: Helvetica, Arial, sans-serif; font-weight: bold; color: #000000; }
span.s1_white { font-family: Helvetica, Arial, sans-serif; font-weight: bold; color: #ffffff; }
span.title {font-family: Helvetica, Arial, sans-serif; font-weight: bold; color: #000000; }
span.c1 {color: #000000; font-family: Helvetica, Arial, sans-serif}
tr.left {text-align: left}
hr { width: 100%; size: 2}
</style>
</head>
<body>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td valign="top" align="left"><a href="http://jakarta.apache.org/index.html"><img hspace="0" vspace="0" border="0" src="images/jakarta-logo.gif"></a></td><td width="100%" valign="top" align="left" bgcolor="#ffffff"><img hspace="0" vspace="0" border="0" align="right" src="images/header.gif"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#525d76"><span class="c1"><a class="black" href="http://www.apache.org/">www.apache.org ></a><a class="black" href="http://jakarta.apache.org/">jakarta.apache.org ></a><a href="http://jakarta.apache.org/poi/" class="black">jakarta.apache.org/poi</a></span></td>
</tr>
<tr>
<td height="8"></td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="1%">
<br>
</td><td nowrap="1" valign="top" width="14%">
<br>
<span class="s1">Navigation</span>
<br>
<a class="s1" href="../index.html">Main</a>
<br>
<a class="s1" href="how-to.html">How To</a>
<br>
<a class="s1" href="fileformat.html">File System Documentation</a>
<br>
<a class="s1" href="usecases.html">Use Cases</a>
<br>
</td><td width="1%">
<br>
</td><td align="left" valign="top" width="*"><font size="-2" color="#000000">
<p>
<a href="mailto:"></a>
</p>
</font>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="100%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+1"><font face="Arial,sans-serif"><b>How To Use the POIFS APIs</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">This document describes how to use the POIFS APIs to read, write, and modify files that employ a POIFS-compatible data structure to organize their content.</p>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Revision History</b></font></font></td>
</tr>
<tr>
<td>
<br>
<ul>
<li>02.10.2002 - completely rewritten from original documents on <a href="https://sourceforge.net/cvs/?group_id=32701">sourceforge</a>
</li>
</ul>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Target Audience</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">This document is intended for Java developers who need to use the POIFS APIs to read, write, or modify files that employ a POIFS-compatible data structure to organize their content. It is not necessary for developers to understand the POIFS data structures, and an explanation of those data structures is beyond the scope of this document. It is expected that the members of the target audience will understand the rudiments of a hierarchical file system, and familiarity with the event pattern employed by Java APIs such as AWT would be helpful.</p>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Glossary</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">This document attempts to be consistent in its terminology, which is defined here:</p>
<table width="100%" cellspacing="2" cellpadding="2" border="0">
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Term</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Definition</b></font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Directory</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">A special file that may contain other directories and documents.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">DirectoryEntry</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Representation of a directory within another directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Document</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">A file containing data, such as word processing data or a spreadsheet workbook.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">DocumentEntry</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Representation of a document within a directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Entry</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Representation of a file in a directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">File</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">A named entity, managed and contained by the file system.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">File System</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">The POIFS data structures, plus the contained directories and documents, which are maintained in a hierarchical directory structure.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Root Directory</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">The directory at the base of a file system. All file systems have a root directory. The POIFS APIs will not allow the root directory to be removed or renamed, but it can be accessed for the purpose of reading its contents or adding files (directories and documents) to it.</font></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="100%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+1"><font face="Arial,sans-serif"><b>Reading a File System</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">This section covers reading a file system. There are two ways to read a file system; these techniques are sketched out in the following table, and then explained in greater depth in the sections following the table.</p>
<table width="100%" cellspacing="2" cellpadding="2" border="0">
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Technique</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Advantages</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Disadvantages</b></font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Conventional Reading</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>Simpler API similar to reading a conventional file system.</li>
<li>Can read documents in any order.</li>
</ul>
</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>All files are resident in memory, whether your application needs them or not.</li>
</ul>
</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">Event-Driven Reading</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>Reduced footprint -- only the documents you care about are processed.</li>
<li>Improved performance -- no time is wasted reading the documents you're not interested in.</li>
</ul>
</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>More complicated API.</li>
<li>Need to know in advance which documents you want to read.</li>
<li>No control over the order in which the documents are read.</li>
<li>No way to go back and get additional documents except to re-read the file system, which may not be possible, e.g., if the file system is being read from an input stream that lacks random access support.</li>
</ul>
</font></td>
</tr>
</table>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Conventional Reading</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">In this technique for reading, the entire file system is loaded into memory, and the entire directory tree can be walked by an application, reading specific documents at the application's leisure.</p>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>Preparation</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Before an application can read a file from the file system, the file system needs to be loaded into memory. This is done by using the <code>org.apache.poi.poifs.filesystem.POIFSFileSystem</code> class. Once the file system has been loaded into memory, the application may need the root directory. The following code fragment will accomplish this preparation stage:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
// need an open InputStream; for a file-based system, this would be appropriate:
// InputStream stream = new FileInputStream(fileName);
POIFSFileSystem fs;
try
{
fs = new POIFSFileSystem(inputStream);
}
catch (IOException e)
{
// an I/O error occurred, or the InputStream did not provide a compatible
// POIFS data structure
}
DirectoryEntry root = fs.getRoot();</pre>
</td>
</tr>
</table>
</div>
<p align="justify">Assuming no exception was thrown, the file system can then be read.</p>
<p align="justify">Note: loading the file system can take noticeable time, particularly for large file systems.</p>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>Reading the Directory Tree</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Once the file system has been loaded into memory and the root directory has been obtained, the root directory can be read. The following code fragment shows how to read the entries in an <code>org.apache.poi.poifs.filesystem.DirectoryEntry</code> instance:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
// dir is an instance of DirectoryEntry ...
for (Iterator iter = dir.getEntries(); iter.hasNext(); )
{
Entry entry = (Entry)iter.next();
System.out.println("found entry: " + entry.getName());
if (entry instanceof DirectoryEntry)
{
// .. recurse into this directory
}
else if (entry instanceof DocumentEntry)
{
// entry is a document, which you can read
}
else
{
// currently, either an Entry is a DirectoryEntry or a DocumentEntry,
// but in the future, there may be other entry subinterfaces. The
// internal data structure certainly allows for a lot more entry types.
}
}</pre>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>Reading a Specific Document</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">There are a couple of ways to read a document, depending on whether the document resides in the root directory or in another directory. Either way, you will obtain an <code>org.apache.poi.poifs.filesystem.DocumentInputStream</code> instance.</p>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="97%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-2"><font face="Arial,sans-serif"><b>DocumentInputStream</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">The DocumentInputStream class is a simple implementation of InputStream that makes a few guarantees worth noting:</p>
<ul>
<li>
<code>available()</code> always returns the number of bytes in the document from your current position in the document.</li>
<li>
<code>markSupported()</code> returns <code>true</code>.</li>
<li>
<code>mark(int limit)</code> ignores the limit parameter; basically the method marks the current position in the document.</li>
<li>
<code>reset()</code> takes you back to the position when <code>mark()</code> was last called, or to the beginning of the document if <code>mark()</code> has not been called.</li>
<li>
<code>skip(long n)</code> will take you to your current position + n (but not past the end of the document).</li>
</ul>
<p align="justify">The behavior of <code>available</code> means you can read in a document in a single read call like this:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
byte[] content = new byte[ stream.available() ];
stream.read(content);
stream.close();</pre>
</td>
</tr>
</table>
</div>
<p align="justify">The combination of <code>mark</code>, <code>reset</code>, and <code>skip</code> provide the basic mechanisms needed for random access of the document contents.</p>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="97%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-2"><font face="Arial,sans-serif"><b>Reading a Document From the Root Directory</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">If the document resides in the root directory, you can obtain a <code>DocumentInputStream</code> like this:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
// load file system
try
{
DocumentInputStream stream = filesystem.createDocumentInputStream(documentName);
// process data from stream
}
catch (IOException e)
{
// no such document, or the Entry represented by documentName is not a
// DocumentEntry
}</pre>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="97%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-2"><font face="Arial,sans-serif"><b>Reading a Document From an Arbitrary Directory</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">A more generic technique for reading a document is to obtain an <code>org.apache.poi.poifs.filesystem.DirectoryEntry</code> instance for the directory containing the desired document (recall that you can use <code>getRoot()</code> to obtain the root directory from its file system). From that DirectoryEntry, you can then obtain a <code>DocumentInputStream</code> like this:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
DocumentEntry document = (DocumentEntry)directory.getEntry(documentName);
DocumentInputStream stream = new DocumentInputStream(document);
</pre>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Event-Driven Reading</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">The event-driven API for reading documents is a little more complicated and requires that your application know, in advance, which files it wants to read. The benefit of using this API is that each document is in memory just long enough for your application to read it, and documents that you never read at all are not in memory at all. When you're finished reading the documents you wanted, the file system has no data structures associated with it at all and can be discarded.</p>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>Preparation</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">The preparation phase involves creating an instance of <code>org.apache.poi.poifs.eventfilesystem.POIFSReader</code> and to then register one or more <code>org.apache.poi.poifs.eventfilesystem.POIFSReaderListener</code> instances with the <code>POIFSReader</code>.</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
POIFSReader reader = new POIFSReader();
// register for everything
reader.registerListener(myOmnivorousListener);
// register for selective files
reader.registerListener(myPickyListener, "foo");
reader.registerListener(myPickyListener, "bar");
// register for selective files
reader.registerListener(myOtherPickyListener, new POIFSDocumentPath(),
"fubar");
reader.registerListener(myOtherPickyListener, new POIFSDocumentPath(
new String[] { "usr", "bin" ), "fubar");</pre>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>POIFSReaderListener</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">
<code>org.apache.poi.poifs.eventfilesystem.POIFSReaderListener</code> is an interface used to register for documents. When a matching document is read by the <code>org.apache.poi.poifs.eventfilesystem.POIFSReader</code>, the <code>POIFSReaderListener</code> instance receives an <code>org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent</code> instance, which contains an open <code>DocumentInputStream</code> and information about the document.</p>
<p align="justify">A <code>POIFSReaderListener</code> instance can register for individual documents, or it can register for all documents; once it has registered for all documents, subsequent (and previous!) registration requests for individual documents are ignored. There is no way to unregister a <code>POIFSReaderListener</code>.</p>
<p align="justify">Thus, it is possible to register a single <code>POIFSReaderListener</code> for multiple documents - one, some, or all documents. It is guaranteed that a single <code>POIFSReaderListener</code> will receive exactly one notification per registered document. There is no guarantee as to the order in which it will receive notification of its documents, as future implementations of <code>POIFSReader</code> are free to change the algorithm for walking the file system's directory structure.</p>
<p align="justify">It is also permitted to register more than one <code>POIFSReaderListener</code> for the same document. There is no guarantee of ordering for notification of <code>POIFSReaderListener</code> instances that have registered for the same document when <code>POIFSReader</code> processes that document.</p>
<p align="justify">It is guaranteed that all notifications occur in the same thread. A future enhancement may be made to provide multi-threaded notifications, but such an enhancement would very probably be made in a new reader class, a <code>ThreadedPOIFSReader</code> perhaps.</p>
<p align="justify">The following table describes the three ways to register a <code>POIFSReaderListener</code> for a document or set of documents:</p>
<table width="100%" cellspacing="2" cellpadding="2" border="0">
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Method Signature</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>What it does</b></font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">registerListener(POIFSReaderListener <b>listener</b>)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">registers <b>listener</b> for all documents.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">registerListener(POIFSReaderListener <b>listener</b>, String <b>name</b>)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">registers <b>listener</b> for a document with the specified <b>name</b> in the root directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">registerListener(POIFSReaderListener <b>listener</b>, POIFSDocumentPath <b>path</b>, String <b>name</b>)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">registers <b>listener</b> for a document with the specified <b>name</b> in the directory described by <b>path</b></font></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>POIFSDocumentPath</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">The <code>org.apache.poi.poifs.filesystem.POIFSDocumentPath</code> class is used to describe a directory in a POIFS file system. Since there are no reserved characters in the name of a file in a POIFS file system, a more traditional string-based solution for describing a directory, with special characters delimiting the components of the directory name, is not feasible. The constructors for the class are used as follows:</p>
<table width="100%" cellspacing="2" cellpadding="2" border="0">
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Constructor example</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Directory described</b></font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">new POIFSDocumentPath()</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">The root directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">new POIFSDocumentPath(null)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">The root directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">new POIFSDocumentPath(new String[ 0 ])</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">The root directory.</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">new POIFSDocumentPath(new String[ ] { "foo", "bar"} )</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">in Unix terminology, "/foo/bar".</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">new POIFSDocumentPath(new POIFSDocumentPath(new String[] { "foo" }), new String[ ] { "fu", "bar"} )</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">in Unix terminology, "/foo/fu/bar".</font></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="98%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="-1"><font face="Arial,sans-serif"><b>Processing POIFSReaderEvent Events</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Processing <code>org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent</code> events is relatively easy. After all of the <code>POIFSReaderListener</code> instances have been registered with <code>POIFSReader</code>, the <code>POIFSReader.read(InputStream stream)</code> method is called.</p>
<p align="justify">Assuming that there are no problems with the data, as the <code>POIFSReader</code> processes the documents in the specified <code>InputStream</code>'s data, it calls registered <code>POIFSReaderListener</code> instances' <code>processPOIFSReaderEvent</code> method with a <code>POIFSReaderEvent</code> instance.</p>
<p align="justify">The <code>POIFSReaderEvent</code> instance contains information to identify the document (a <code>POIFSDocumentPath</code> object to identify the directory that the document is in, and the document name), and an open <code>DocumentInputStream</code> instance from which to read the document.</p>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="100%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+1"><font face="Arial,sans-serif"><b>Writing a File System</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Writing a file system is very much like reading a file system in that there are multiple ways to do so. You can load an existing file system into memory and modify it (removing files, renaming files) and/or add new files to it, and write it, or you can start with a new, empty file system:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
POIFSFileSystem fs = new POIFSFileSystem();</pre>
</td>
</tr>
</table>
</div>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>The Naming of Names</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">There are two restrictions on the names of files in a file system that must be considered when creating files:</p>
<ol>
<li>The name of the file must not exceed 31 characters. If it does, the POIFS API will silently truncate the name to fit.</li>
<li>The name of the file must be unique within its containing directory. This seems pretty obvious, but if it isn't spelled out, there'll be hell to pay, to be sure. Uniqueness, of course, is determined <b>after</b> the name has been truncated, if the original name was too long to begin with.</li>
</ol>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Creating a Document</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">A document can be created by acquiring a <code>DirectoryEntry</code> and calling one of the two <code>createDocument</code> methods:</p>
<table width="100%" cellspacing="2" cellpadding="2" border="0">
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Method Signature</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Advantages</b></font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000"><b>Disadvantages</b></font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">CreateDocument(String name, InputStream stream)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>Simple API.</li>
</ul>
</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>Increased memory footprint (document is in memory until file system is written).</li>
</ul>
</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">CreateDocument(String name, int size, POIFSWriterListener writer)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>Decreased memory footprint (only very small documents are held in memory, and then only for a short time).</li>
</ul>
</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">
<ul>
<li>More complex API.</li>
<li>Determining document size in advance may be difficult.</li>
<li>Lose control over when document is to be written.</li>
</ul>
</font></td>
</tr>
</table>
<p align="justify">Unlike reading, you don't have to choose between the in-memory and event-driven writing models; both can co-exist in the same file system.</p>
<p align="justify">Writing is initiated when the <code>POIFSFileSystem</code> instance's <code>writeFilesystem()</code> method is called with an <code>OutputStream</code> to write to.</p>
<p align="justify">The event-driven model is quite similar to the event-driven model for reading, in that the file system calls your <code>org.apache.poi.poifs.filesystem.POIFSWriterListener</code> when it's time to write your document, just as the <code>POIFSReader</code> calls your <code>POIFSReaderListener</code> when it's time to read your document. Internally, when <code>writeFilesystem()</code> is called, the final POIFS data structures are created and are written to the specified <code>OutputStream</code>. When the file system needs to write a document out that was created with the event-driven model, it calls the <code>POIFSWriterListener</code> back, calling its <code>processPOIFSWriterEvent()</code> method, passing an <code>org.apache.poi.poifs.filesystem.POIFSWriterEvent</code> instance. This object contains the <code>POIFSDocumentPath</code> and name of the document, its size, and an open <code>org.apache.poi.poifs.filesystem.DocumentOutputStream</code> to which to write. A <code>DocumentOutputStream</code> is a wrapper over the <code>OutputStream</code> that was provided to the <code>POIFSFileSystem</code> to write to, and has the responsibility of making sure that the document your application writes fits within the size you specified for it.</p>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Creating a Directory</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Creating a directory is similar to creating a document, except that there's only one way to do so:</p>
<div align="center">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<pre>
DirectoryEntry createdDir = existingDir.createDirectory(name);</pre>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Using POIFSFileSystem Directly To Create a Document Or Directory</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">As with reading documents, it is possible to create a new document or directory in the root directory by using convenience methods of POIFSFileSystem.</p>
<table width="100%" cellspacing="2" cellpadding="2" border="0">
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">DirectoryEntry Method Signature</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">POIFSFileSystem Method Signature</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">createDocument(String name, InputStream stream)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">createDocument(InputStream stream, String name)</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">createDocument(String name, int size, POIFSWriterListener writer)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">createDocument(String name, int size, POIFSWriterListener writer)</font></td>
</tr>
<tr>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">createDirectory(String name)</font></td>
<td valign="top" bgcolor="#a0ddf0" align="left"><font size="-1" color="#000000">createDirectory(String name)</font></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="100%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+1"><font face="Arial,sans-serif"><b>Modifying a File System</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">It is possible to modify an existing POIFS file system, whether it's one your application has loaded into memory, or one which you are creating on the fly.</p>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Removing a Document</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Removing a document is simple: you get the <code>Entry</code> corresponding to the document and call its <code>delete()</code> method. This is a boolean method, but should always return <code>true</code>, indicating that the operation succeeded.</p>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Removing a Directory</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Removing a directory is also simple: you get the <code>Entry</code> corresponding to the directory and call its <code>delete()</code> method. This is a boolean method, but, unlike deleting a document, may not always return <code>true</code>, indicating that the operation succeeded. Here are the reasons why the operation may fail:</p>
<ul>
<li>The directory still has files in it (to check, call <code>isEmpty()</code> on its DirectoryEntry; is the return value <code>false</code>?)</li>
<li>The directory is the root directory. You cannot remove the root directory.</li>
</ul>
</td>
</tr>
</table>
</div>
<br>
<div align="right">
<table cellspacing="0" cellpadding="2" border="0" width="99%">
<tr>
<td bgcolor="#525D76"><font color="#ffffff" size="+0"><font face="Arial,sans-serif"><b>Renaming a File</b></font></font></td>
</tr>
<tr>
<td>
<br>
<p align="justify">Regardless of whether the file is a directory or a document, it can be renamed, with one exception - the root directory has a special name that is expected by the components of a major software vendor's office suite, and the POIFS API will not let that name be changed. Renaming is done by acquiring the file's corresponding <code>Entry</code> instance and calling its <code>renameTo</code> method, passing in the new name.</p>
<p align="justify">Like <code>delete</code>, <code>renameTo</code> returns <code>true</code> if the operation succeeded, otherwise <code>false</code>. Reasons for failure include these:</p>
<ul>
<li>The new name is the same as another file in the same directory. And don't forget - if the new name is longer than 31 characters, it <b>will</b> be silently truncated. In its original length, the new name may have been unique, but truncated to 31 characters, it may not be unique any longer.</li>
<li>You tried to rename the root directory.</li>
</ul>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
<br>
</td>
</tr>
</table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<hr noshade="" size="1">
</td>
</tr>
<tr>
<td align="center"><i>Copyright © 2002 Apache Software Foundation</i></td>
</tr>
<tr>
<td align="right" width="100%">
<br>
</td>
</tr>
<tr>
<td align="right" width="100%"><a href="http://krysalis.org/"><img alt="Krysalis Logo" src="images/krysalis-compatible.jpg"></a><a href="http://xml.apache.org/cocoon/"><img alt="Cocoon Logo" src="images/built-with-cocoon.gif"></a></td>
</tr>
</tbody>
</table>
</body>
</html>
|