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 java.io.BufferedInputStream;
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.InputStream;
32  import java.io.IOException;
33  
34  import javax.annotation.Nullable;
35  
36  import com.google.common.io.Files;
37  
38  import htsjdk.samtools.util.BlockCompressedInputStream;
39  
40  import org.apache.commons.compress.compressors.bzip2.BZip2Utils;
41  
42  import org.apache.commons.compress.compressors.gzip.GzipUtils;
43  
44  /**
45   * Compression utility methods.
46   *
47   * @since 1.3
48   * @author  Michael Heuer
49   */
50  public final class Compress
51  {
52  
53      /**
54       * Private no-arg constructor.
55       */
56      private Compress()
57      {
58          // empty
59      }
60  
61  
62      /**
63       * Return true if the specified file is a block compressed gzip (BGZF) file name.
64       *
65       * @since 1.3
66       * @param fileName file name, must not be null
67       * @return true if the specified file is a block compressed gzip (BGZF) file name
68       */
69      public static boolean isBgzfFilename(final String fileName)
70      {
71          checkNotNull(fileName);
72          return BgzfUtils.isCompressedFilename(fileName);
73      }
74  
75      /**
76       * Return true if the specified file is a block compressed gzip (BGZF) file.
77       *
78       * @param file file, if any
79       * @return true if the specified file is a block compressed gzip (BGZF) file
80       */
81      public static boolean isBgzfFile(@Nullable final File file)
82      {
83          if (file == null)
84          {
85              return false;
86          }
87          if (isBgzfFilename(file.getName()))
88          {
89              return true;
90          }
91          // block compressed gzip files may also have .gz file extension, check the stream
92          try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)))
93          {
94              return isBgzfInputStream(inputStream);
95          }
96          catch (IOException e)
97          {
98              return false;
99          }
100     }
101 
102     /**
103      * Return true if the specified file is a block compressed gzip (BGZF) input stream.
104      *
105      * @param inputStream input stream, must not be null
106      * @return true if the specified file is a block compressed gzip (BGZF) input stream
107      */
108     public static boolean isBgzfInputStream(final InputStream inputStream)
109     {
110         checkNotNull(inputStream);
111         BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream);
112         try
113         {
114             return BlockCompressedInputStream.isValidFile(bufferedInputStream);
115         }
116         catch (IOException e)
117         {
118             return false;
119         }
120     }
121 
122     /**
123      * Return true if the specified file is a gzip file name.
124      *
125      * @since 1.3
126      * @param fileName file name, must not be null
127      * @return true if the specified file is a gzip file name
128      */
129     public static boolean isGzipFilename(final String fileName)
130     {
131         checkNotNull(fileName);
132         return GzipUtils.isCompressedFilename(fileName);
133     }
134 
135     /**
136      * Return true if the specified file is a gzip file.  Block compressed gzip (BGZF)
137      * files are also gzip files, so <code>isBgzfFile(File)</code> should be called before
138      * this method.
139      *
140      * @param file file, if any
141      * @return true if the specified file is a gzip file
142      */
143     public static boolean isGzipFile(@Nullable final File file)
144     {
145         if (file == null)
146         {
147             return false;
148         }
149         return isGzipFilename(file.getName());
150     }
151 
152     /**
153      * Return true if the specified file is a gzip input stream.
154      *
155      * @since 1.3
156      * @param inputStream input stream, must not be null
157      * @return true if the specified file is a gzip input stream
158      */
159     public static boolean isGzipInputStream(final InputStream inputStream)
160     {
161         checkNotNull(inputStream);
162         InputStream in = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream);
163         try
164         {
165             in.mark(2);
166             return in.read() == 31 && in.read() == 139;
167         }
168         catch (IOException e)
169         {
170             return false;
171         }
172         finally
173         {
174             try
175             {
176                 in.reset();
177             }
178             catch (IOException e)
179             {
180                 // ignore
181             }
182         }
183     }
184 
185     /**
186      * Return true if the specified file is a bzip2 file name.
187      *
188      * @since 1.3
189      * @param fileName file name, must not be null
190      * @return true if the specified file is a bzip2 file name
191      */
192     public static boolean isBzip2Filename(final String fileName)
193     {
194         return BZip2Utils.isCompressedFilename(fileName);
195     }
196 
197     /**
198      * Return true if the specified file is a bzip2 file.
199      *
200      * @param file file, if any
201      * @return true if the specified file is a bzip2 file
202      */
203     public static boolean isBzip2File(@Nullable final File file)
204     {
205         if (file == null)
206         {
207             return false;
208         }
209         return isBzip2Filename(file.getName());
210     }
211 
212     /**
213      * Return true if the specified file is a bzip2 input stream.
214      *
215      * @since 1.3
216      * @param inputStream input stream, must not be null
217      * @return true if the specified file is a bzip2 input stream
218      */
219     public static boolean isBzip2InputStream(final InputStream inputStream)
220     {
221         checkNotNull(inputStream);
222         InputStream in = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream);
223         try
224         {
225             in.mark(3);
226             return in.read() == 'B' && in.read() == 'Z' && in.read() == 'h';
227         }
228         catch (IOException e)
229         {
230             return false;
231         }
232         finally
233         {
234             try
235             {
236                 in.reset();
237             }
238             catch (IOException e)
239             {
240                 // ignore
241             }
242         }
243     }
244 }