View Javadoc
1   /*
2    * Copyright 2014 The Codehaus 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.codehaus.plexus.archiver.util;
17  
18  import javax.annotation.WillClose;
19  import javax.annotation.WillNotClose;
20  
21  import java.io.BufferedInputStream;
22  import java.io.BufferedOutputStream;
23  import java.io.ByteArrayInputStream;
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.io.CachingOutputStream;
34  
35  public class Streams {
36  
37      public static final InputStream EMPTY_INPUTSTREAM = new ByteArrayInputStream(new byte[0]);
38  
39      public static BufferedInputStream bufferedInputStream(InputStream is) {
40          return is instanceof BufferedInputStream ? (BufferedInputStream) is : new BufferedInputStream(is, 65536);
41      }
42  
43      public static BufferedOutputStream bufferedOutputStream(OutputStream os) {
44          return os instanceof BufferedOutputStream ? (BufferedOutputStream) os : new BufferedOutputStream(os, 65536);
45      }
46  
47      public static byte[] cacheBuffer() {
48          return new byte[8 * 1024];
49      }
50  
51      public static InputStream fileInputStream(File file) throws IOException {
52          return Files.newInputStream(file.toPath());
53      }
54  
55      public static InputStream fileInputStream(File file, String operation) throws ArchiverException {
56          try {
57              return Files.newInputStream(file.toPath());
58          } catch (IOException e) {
59              throw new ArchiverException(
60                      "Problem reading input file for " + operation + " " + file.getParent() + ", " + e.getMessage());
61          }
62      }
63  
64      public static OutputStream fileOutputStream(File file) throws IOException {
65          return new CachingOutputStream(file);
66      }
67  
68      public static OutputStream fileOutputStream(Path file) throws IOException {
69          return new CachingOutputStream(file);
70      }
71  
72      public static OutputStream fileOutputStream(File file, String operation) throws ArchiverException {
73          try {
74              return new CachingOutputStream(file);
75          } catch (IOException e) {
76              throw new ArchiverException(
77                      "Problem creating output file for " + operation + " " + file.getParent() + ", " + e.getMessage());
78          }
79      }
80  
81      public static void copyFully(@WillClose InputStream zIn, @WillClose OutputStream out, String gzip)
82              throws ArchiverException {
83          // There must be 1 million libs out there that do this
84          try {
85              copyFullyDontCloseOutput(zIn, out, gzip);
86              out.close();
87              out = null;
88          } catch (final IOException e) {
89              throw new ArchiverException("Failure copying.", e);
90          } finally {
91              IOUtil.close(out);
92          }
93      }
94  
95      public static void copyFullyDontCloseOutput(@WillClose InputStream zIn, @WillNotClose OutputStream out, String gzip)
96              throws ArchiverException {
97          // There must be 1 million libs out there that do this
98          try {
99              byte[] buffer = cacheBuffer();
100             int count = 0;
101             do {
102                 try {
103                     out.write(buffer, 0, count);
104                 } catch (IOException e) {
105                     throw new ArchiverException(
106                             "Problem writing to output in " + gzip + " operation " + e.getMessage());
107                 }
108                 count = zIn.read(buffer, 0, buffer.length);
109             } while (count != -1);
110             zIn.close();
111             zIn = null;
112         } catch (IOException e) {
113             throw new ArchiverException("Problem reading from source file in " + gzip + " operation " + e.getMessage());
114 
115         } finally {
116             IOUtil.close(zIn);
117         }
118     }
119 }