pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / utils / StreamUtils.java
1 package org.apache.lucene.benchmark.byTask.utils;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.BufferedInputStream;
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import org.apache.commons.compress.compressors.CompressorException;
33 import org.apache.commons.compress.compressors.CompressorStreamFactory;
34
35 /**
36  * Stream utilities.
37  */
38 public class StreamUtils {
39
40         /** Buffer size used across the benchmark package */
41         public static final int BUFFER_SIZE = 1 << 16; // 64K
42         
43         /** File format type */
44         public enum Type {
45                 /** BZIP2 is automatically used for <b>.bz2</b> and <b>.bzip2</b> extensions. */
46                 BZIP2(CompressorStreamFactory.BZIP2),
47                 /** GZIP is automatically used for <b>.gz</b> and <b>.gzip</b> extensions. */
48                 GZIP(CompressorStreamFactory.GZIP),
49                 /** Plain text is used for anything which is not GZIP or BZIP. */
50                 PLAIN(null);
51                 private final String csfType;
52                 Type(String csfType) {
53                         this.csfType = csfType;
54                 }
55                 InputStream inputStream(InputStream in) throws IOException {
56                         try {
57                                 return csfType==null ? in : new CompressorStreamFactory().createCompressorInputStream(csfType, in);
58                         } catch (CompressorException e) {
59                 IOException ioe = new IOException(e.getMessage());
60                 ioe.initCause(e);
61                 throw ioe;                      }  
62                 }
63                 OutputStream outputStream(OutputStream os) throws IOException {
64                         try {
65                                 return csfType==null ? os : new CompressorStreamFactory().createCompressorOutputStream(csfType, os);
66                         } catch (CompressorException e) {
67                                 IOException ioe = new IOException(e.getMessage());
68                                 ioe.initCause(e);
69                                 throw ioe;  
70                         }  
71                 }
72         }
73         
74   private static final Map<String,Type> extensionToType = new HashMap<String,Type>();
75   static {
76         // these in are lower case, we will lower case at the test as well
77     extensionToType.put(".bz2", Type.BZIP2);
78     extensionToType.put(".bzip", Type.BZIP2);
79     extensionToType.put(".gz", Type.GZIP);
80     extensionToType.put(".gzip", Type.GZIP);
81   }
82   
83   /**
84    * Returns an {@link InputStream} over the requested file. This method
85    * attempts to identify the appropriate {@link InputStream} instance to return
86    * based on the file name (e.g., if it ends with .bz2 or .bzip, return a
87    * 'bzip' {@link InputStream}).
88    */
89   public static InputStream inputStream(File file) throws IOException {
90     // First, create a FileInputStream, as this will be required by all types.
91     // Wrap with BufferedInputStream for better performance
92     InputStream in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
93     return fileType(file).inputStream(in);
94   }
95
96   /** Return the type of the file, or null if unknown */
97   private static Type fileType(File file) {
98         Type type = null;
99     String fileName = file.getName();
100     int idx = fileName.lastIndexOf('.');
101     if (idx != -1) {
102       type = extensionToType.get(fileName.substring(idx).toLowerCase(Locale.ENGLISH));
103     }
104     return type==null ? Type.PLAIN : type;
105         }
106   
107   /**
108    * Returns an {@link OutputStream} over the requested file, identifying
109    * the appropriate {@link OutputStream} instance similar to {@link #inputStream(File)}.
110    */
111   public static OutputStream outputStream(File file) throws IOException {
112     // First, create a FileInputStream, as this will be required by all types.
113     // Wrap with BufferedInputStream for better performance
114     OutputStream os = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
115     return fileType(file).outputStream(os);
116   }
117 }