View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.saxbenchmark;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  
25  import org.xml.sax.InputSource;
26  
27  /***
28   * @author JMcCrindle
29   */
30  public class FileSourceFactory implements SourceFactory {
31  
32      private String name;
33      private byte[] bytes = null;
34  
35      public FileSourceFactory(String filename) throws IOException {
36          this(new File(filename));
37      }
38      
39      public FileSourceFactory(File file) throws IOException {
40          this.name = file.getName();
41          FileInputStream in = new FileInputStream(file);
42          ByteArrayOutputStream out = new ByteArrayOutputStream();
43          byte[] b = new byte[4096];
44          int i = -1;
45          while((i = in.read(b)) != -1) {
46              out.write(b, 0, i);
47          }
48          in.close();
49          out.flush();
50          out.close();
51          bytes = out.toByteArray();
52      }
53      
54      /* (non-Javadoc)
55       * @see org.saxbenchmark.InputSourceFactory#getName()
56       */
57      public String getName() {
58          return name;
59      }
60  
61      /* (non-Javadoc)
62       * @see org.saxbenchmark.InputSourceFactory#getInputSource()
63       */
64      public InputSource getInputSource() {
65          ByteArrayInputStream in = new ByteArrayInputStream(bytes);
66          InputSource inputSource = new InputSource(new InputStreamReader(in));
67          inputSource.setByteStream(in);
68          return inputSource;
69      }
70      
71  }