KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfInputFile


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8 import java.nio.*;
9 import java.nio.channels.*;
10 import java.nio.charset.*;
11
12 /**
13    Provides low-level methods for reading portions of a PDF document.
14    {@link PdfReader PdfReader} accesses PDF documents through the
15    {@link PdfInput PdfInput} interface, which is implemented by this
16    class. The portions of the document are read from the file system
17    as they are requested, which reduces memory consumption as compared
18    to {@link PdfInputBuffer PdfInputBuffer} but is a bit slower. <p>
19    This class is synchronized; however, note that since it acts as a
20    wrapper around a file that is kept open, it is the calling method's
21    responsibility to ensure that the file is not modified externally
22    to this class. If that is a problem, use {@link
23    PdfInputBuffer#PdfInputBuffer(File)
24    PdfInputBuffer.PdfInputBuffer(File)}, which reads the entire file
25    into memory and closes it immediately.
26    @author Nassib Nassar
27  */

28 public class PdfInputFile
29     implements PdfInput {
30
31     /**
32        The file channel associated with the PDF document.
33     */

34     protected FileChannel _fileChannel;
35
36     /**
37        The length of the input file.
38      */

39     protected long _length;
40
41     /**
42        The input file name.
43      */

44     protected String JavaDoc _name;
45
46     /**
47        The random access file containing the PDF document.
48      */

49     protected RandomAccessFile _randomAccessFile;
50
51     /**
52       Constructs a PDF input source based on a specified file.
53       The file is kept open, and portions of it are read as they
54       are requested. It is the calling method's responsibility to
55       ensure that the file is not modified externally to this
56       class.
57       @param pdfFile the source file.
58       @throws IOException
59      */

60     public PdfInputFile(File pdfFile) throws IOException {
61
62         _randomAccessFile = new RandomAccessFile(pdfFile, "r");
63         _fileChannel = _randomAccessFile.getChannel();
64         _length = _randomAccessFile.length();
65         _name = pdfFile.getPath();
66
67     }
68     
69         /**
70            Closes the PDF document and releases any system resources
71            associated with it.
72            @throws IOException
73         */

74         public void close() throws IOException {
75         synchronized (this) {
76             if (_fileChannel != null) {
77                 _fileChannel.close();
78                 _fileChannel = null;
79             }
80             if (_randomAccessFile != null) {
81                 _randomAccessFile.close();
82                 _randomAccessFile = null;
83             }
84         }
85         }
86
87     public long getLength() {
88         synchronized (this) {
89         
90             return _length;
91
92         }
93     }
94
95     public ByteBuffer readBytes(long start, long end) throws IOException {
96         synchronized (this) {
97
98             ByteBuffer bbuf = ByteBuffer.allocateDirect((int)(end - start));
99             _fileChannel.read(bbuf, start);
100             bbuf.position(0);
101             return bbuf;
102             
103         }
104     }
105
106     public CharBuffer readChars(long start, long end) throws IOException {
107         synchronized (this) {
108
109             
110             try {
111                 return Charset.forName("ISO-8859-1").newDecoder().decode(
112                     readBytes(start, end) );
113                 // Unicode is "UTF-16"
114
}
115             catch (CharacterCodingException e) {
116                 e.printStackTrace();
117                 Runtime.getRuntime().exit(-1);
118             }
119
120             return null; // should never happen
121

122         }
123     }
124
125     public String JavaDoc getName() {
126         synchronized (this) {
127
128             return _name;
129
130         }
131     }
132
133 }
134
Popular Tags