1 package nom.tam.fits.compress;
2
3 /*
4 * #%L
5 * nom.tam FITS library
6 * %%
7 * Copyright (C) 1996 - 2024 nom-tam-fits
8 * %%
9 * This is free and unencumbered software released into the public domain.
10 *
11 * Anyone is free to copy, modify, publish, use, compile, sell, or
12 * distribute this software, either in source code form or as a compiled
13 * binary, for any purpose, commercial or non-commercial, and by any
14 * means.
15 *
16 * In jurisdictions that recognize copyright laws, the author or authors
17 * of this software dedicate any and all copyright interest in the
18 * software to the public domain. We make this dedication for the benefit
19 * of the public at large and to the detriment of our heirs and
20 * successors. We intend this dedication to be an overt act of
21 * relinquishment in perpetuity of all present and future rights to this
22 * software under copyright law.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 * #L%
32 */
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.logging.Logger;
37
38 import nom.tam.fits.FitsException;
39
40 /**
41 * (<i>for internal use</i>) BZIP2 (<code>.bz2</code>) input stream decompression with a preference for using an
42 * external system command. You can use this class to decompress files that have been compressed with the UNIX
43 * <b>bzip2</b> tool and have the characteristic <code>.bz2</code> file name extension. It effectively provides the same
44 * functionality as {@link BZip2CompressionProvider}, but has a preference for calling on the system <b>bzip2</b>
45 * command first to do the lifting. If that fails it will call on {@link CompressionManager} to provide a suitable
46 * decompressor (which will give it {@link BZip2CompressionProvider}). Since the <b>bzip2</b> tool is UNIX-specific, it
47 * is not entirely portable. It also requires the environment variable <code>BZIP_DECOMPRESSOR</code> to be set to
48 * provide the system executable to use. As a result, you are probably better off relying on the mentioned other classes
49 * directly for this functionality.
50 *
51 * @see CompressionManager
52 *
53 * @deprecated Use {@link ZCompressionProvider}, or the more generic {@link CompressionManager} with a preference toward
54 * using the system command if possible, instead.
55 */
56 @Deprecated
57 public class ExternalBZip2CompressionProvider implements ICompressProvider {
58
59 private static final int PRIORITY = 10;
60
61 private static final Logger LOG = Logger.getLogger(ExternalBZip2CompressionProvider.class.getName());
62
63 private InputStream bunzipper(final InputStream compressed) throws IOException, FitsException {
64 String cmd = getBzip2Cmd();
65 // Allow the user to have already specified the - option.
66 if (cmd.indexOf(" -") < 0) {
67 cmd += " -";
68 }
69 String[] flds = cmd.split(" +");
70 Process proc;
71 try {
72 proc = new ProcessBuilder(flds).start();
73 return new CloseIS(proc, compressed);
74 } catch (Exception e) {
75 ICompressProvider next = CompressionManager.nextCompressionProvider('B', 'Z', this);
76 if (next != null) {
77 LOG.warning("Error initiating BZIP decompression: " + e.getMessage() + " trying alternative decompressor");
78 return next.decompress(compressed);
79 }
80 throw new FitsException("Error initiating BZIP decompression: " + e);
81 }
82 }
83
84 /**
85 * Returns the system command to use for decompressing <code>.bz2</code> compressed files. It requires the
86 * <code>BZIP_DECOMPRESSOR</code> environment variable to be set to inform us as to what executable (including path)
87 * should be used. If there is no such environment variable set, it will return <code>null</code>
88 *
89 * @return The system command for decompressing <code>.bz2</code> files, or <code>null</code> if there is no
90 * <code>BZIP_DECOMPRESSOR</code> environment variable that could inform us.
91 */
92 public String getBzip2Cmd() {
93 return System.getProperty("BZIP_DECOMPRESSOR", System.getenv("BZIP_DECOMPRESSOR"));
94 }
95
96 @Override
97 public InputStream decompress(InputStream in) throws IOException, FitsException {
98 return bunzipper(in);
99 }
100
101 @Override
102 public int priority() {
103 return PRIORITY;
104 }
105
106 @Override
107 public boolean provides(int mag1, int mag2) {
108 return mag1 == 'B' && mag2 == 'Z' && getBzip2Cmd() != null;
109 }
110 }