View Javadoc
1   /*
2   
3       dsh-compress  Compression utility classes.
4       Copyright (c) 2014-2017 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.compress;
25  
26  import static com.google.common.base.Preconditions.checkNotNull;
27  
28  import static org.dishevelled.compress.Compress.isBgzfFile;
29  import static org.dishevelled.compress.Compress.isBgzfInputStream;
30  import static org.dishevelled.compress.Compress.isBzip2File;
31  import static org.dishevelled.compress.Compress.isGzipFile;
32  
33  import java.io.BufferedInputStream;
34  import java.io.BufferedReader;
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileReader;
38  import java.io.InputStream;
39  import java.io.InputStreamReader;
40  import java.io.IOException;
41  
42  import javax.annotation.Nullable;
43  
44  import htsjdk.samtools.util.BlockCompressedInputStream;
45  
46  import org.apache.commons.compress.compressors.CompressorException;
47  import org.apache.commons.compress.compressors.CompressorStreamFactory;
48  
49  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
50  
51  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
52  
53  /**
54   * File and input stream readers with support for bgzf, gzip, and bzip2 compression.
55   *
56   * @author  Michael Heuer
57   */
58  public final class Readers
59  {
60  
61      /**
62       * Private no-arg constructor.
63       */
64      private Readers()
65      {
66          // empty
67      }
68  
69  
70      /**
71       * Create and return a new buffered reader for the specified input stream.
72       *
73       * @since 1.1
74       * @param inputStream input stream, must not be null
75       * @return a new buffered reader for the specified input stream
76       * @throws IOException if an I/O error occurs
77       */
78      public static BufferedReader inputStreamReader(final InputStream inputStream) throws IOException
79      {
80          checkNotNull(inputStream);
81          return new BufferedReader(new InputStreamReader(inputStream));
82      }
83  
84      /**
85       * Create and return a new buffered reader for the specified compressed file,
86       * autodetecting the compression type from the first few bytes of the file.
87       *
88       * @since 1.1
89       * @param file file, must not be null
90       * @return a new buffered reader for the specified file
91       * @throws IOException if an I/O error occurs
92       */
93      public static BufferedReader compressedFileReader(final File file) throws IOException
94      {
95          checkNotNull(file);
96          return compressedInputStreamReader(new FileInputStream(file));
97      }
98  
99      /**
100      * Create and return a new buffered reader for the specified compressed input stream,
101      * autodetecting the compression type from the first few bytes of the stream.
102      *
103      * @since 1.1
104      * @param inputStream input stream, must not be null
105      * @return a new buffered reader for the specified input stream
106      * @throws IOException if an I/O error occurs
107      */
108     public static BufferedReader compressedInputStreamReader(final InputStream inputStream) throws IOException
109     {
110         checkNotNull(inputStream);
111         BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream);
112         try
113         {
114             if (isBgzfInputStream(bufferedInputStream))
115             {
116                 return bgzfInputStreamReader(bufferedInputStream);
117             }
118             return new BufferedReader(new InputStreamReader(new CompressorStreamFactory().createCompressorInputStream(bufferedInputStream)));
119         }
120         catch (CompressorException e)
121         {
122             // fall back to uncompressed input stream reader
123             return inputStreamReader(bufferedInputStream);
124         }
125     }
126 
127     /**
128      * Create and return a new buffered reader for the specified block compressed gzip (BGZF) compressed file.
129      *
130      * @since 1.2
131      * @param file bgzf compressed file, must not be null
132      * @return a new buffered reader for the specified block compressed gzip (BGZF) compressed file
133      * @throws IOException if an I/O error occurs
134      */
135     public static BufferedReader bgzfFileReader(final File file) throws IOException
136     {
137         checkNotNull(file);
138         return new BufferedReader(new InputStreamReader(new BlockCompressedInputStream(file)));
139     }
140 
141     /**
142      * Create and return a new buffered reader for the specified block compressed gzip (BGZF) compressed input stream.
143      *
144      * @since 1.2
145      * @param inputStream bgzf compressed input stream, must not be null
146      * @return a new buffered reader for the specified block compressed gzip (BGZF) compressed input stream
147      * @throws IOException if an I/O error occurs
148      */
149     public static BufferedReader bgzfInputStreamReader(final InputStream inputStream) throws IOException
150     {
151         checkNotNull(inputStream);
152         return new BufferedReader(new InputStreamReader(new BlockCompressedInputStream(inputStream)));
153     }
154 
155     /**
156      * Create and return a new buffered reader for the specified gzip compressed file.
157      *
158      * @since 1.1
159      * @param file gzip compressed file, must not be null
160      * @return a new buffered reader for the specified gzip compressed file
161      * @throws IOException if an I/O error occurs
162      */
163     public static BufferedReader gzipFileReader(final File file) throws IOException
164     {
165         checkNotNull(file);
166         return gzipInputStreamReader(new FileInputStream(file));
167     }
168 
169     /**
170      * Create and return a new buffered reader for the specified gzip compressed input stream.
171      *
172      * @param inputStream gzip compressed input stream, must not be null
173      * @return a new buffered reader for the specified gzip compressed input stream
174      * @throws IOException if an I/O error occurs
175      */
176     public static BufferedReader gzipInputStreamReader(final InputStream inputStream) throws IOException
177     {
178         checkNotNull(inputStream);
179         return new BufferedReader(new InputStreamReader(new GzipCompressorInputStream(inputStream)));
180     }
181 
182     /**
183      * Create and return a new buffered reader for the specified bzip2 compressed file.
184      *
185      * @since 1.1
186      * @param file bzip2 compressed file, must not be null
187      * @return a new buffered reader for the specified bzip2 compressed file
188      * @throws IOException if an I/O error occurs
189      */
190     public static BufferedReader bzip2FileReader(final File file) throws IOException
191     {
192         checkNotNull(file);
193         return bzip2InputStreamReader(new FileInputStream(file));
194     }
195 
196     /**
197      * Create and return a new buffered reader for the specified bzip2 compressed input stream.
198      *
199      * @param inputStream bzip2 compressed input stream, must not be null
200      * @return a new buffered reader for the specified bzip2 compressed input stream
201      * @throws IOException if an I/O error occurs
202      */
203     public static BufferedReader bzip2InputStreamReader(final InputStream inputStream) throws IOException
204     {
205         checkNotNull(inputStream);
206         return new BufferedReader(new InputStreamReader(new BZip2CompressorInputStream(inputStream)));
207     }
208 
209     /**
210      * Create and return a new buffered reader with support for bgzf, gzip, or bzip2 compression for the specified file
211      * or <code>stdin</code> if the file is null.
212      *
213      * @param file file, if any
214      * @return a new buffered reader with support for bgzf, gzip, or bzip2 compression for the specified file
215      *    or <code>stdin</code> if the file is null
216      * @throws IOException if an I/O error occurs
217      */
218     public static BufferedReader reader(@Nullable final File file) throws IOException
219     {
220         if (file == null)
221         {
222             return compressedInputStreamReader(System.in);
223         }
224         else if (isBgzfFile(file))
225         {
226             return bgzfFileReader(file);
227         }
228         else if (isGzipFile(file))
229         {
230             return gzipFileReader(file);
231         }
232         else if (isBzip2File(file))
233         {
234             return bzip2FileReader(file);
235         }
236         return new BufferedReader(new FileReader(file));
237     }
238 }