@@ -32,15 +32,8 @@ private DataUtil() {}
32
32
* @throws IOException on IO error
33
33
*/
34
34
public static Document load (File in , String charsetName , String baseUri ) throws IOException {
35
- FileInputStream inStream = null ;
36
- try {
37
- inStream = new FileInputStream (in );
38
- ByteBuffer byteData = readToByteBuffer (inStream );
39
- return parseByteData (byteData , charsetName , baseUri , Parser .htmlParser ());
40
- } finally {
41
- if (inStream != null )
42
- inStream .close ();
43
- }
35
+ ByteBuffer byteData = readFileToByteBuffer (in );
36
+ return parseByteData (byteData , charsetName , baseUri , Parser .htmlParser ());
44
37
}
45
38
46
39
/**
@@ -160,6 +153,19 @@ static ByteBuffer readToByteBuffer(InputStream inStream) throws IOException {
160
153
return readToByteBuffer (inStream , 0 );
161
154
}
162
155
156
+ static ByteBuffer readFileToByteBuffer (File file ) throws IOException {
157
+ RandomAccessFile randomAccessFile = null ;
158
+ try {
159
+ randomAccessFile = new RandomAccessFile (file , "r" );
160
+ byte [] bytes = new byte [(int ) randomAccessFile .length ()];
161
+ randomAccessFile .readFully (bytes );
162
+ return ByteBuffer .wrap (bytes );
163
+ } finally {
164
+ if (randomAccessFile != null )
165
+ randomAccessFile .close ();
166
+ }
167
+ }
168
+
163
169
/**
164
170
* Parse out a charset from a content type header. If the charset is not supported, returns null (so the default
165
171
* will kick in.)
0 commit comments