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 java.io.BufferedInputStream;
19  import java.io.BufferedOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import javax.annotation.WillClose;
28  import javax.annotation.WillNotClose;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.util.IOUtil;
31  
32  public class Streams
33  {
34  
35      public static BufferedInputStream bufferedInputStream( InputStream is )
36      {
37          return is instanceof BufferedInputStream
38                     ? (BufferedInputStream) is
39                     : new BufferedInputStream( is, 65536 );
40  
41      }
42  
43      public static BufferedOutputStream bufferedOutputStream( OutputStream os )
44      {
45          return os instanceof BufferedOutputStream
46                     ? (BufferedOutputStream) os
47                     : new BufferedOutputStream( os, 65536 );
48  
49      }
50  
51      public static byte[] cacheBuffer()
52      {
53          return new byte[ 8 * 1024 ];
54      }
55  
56      public static FileInputStream fileInputStream( File file )
57          throws FileNotFoundException
58      {
59          return new FileInputStream( file );
60      }
61  
62      public static FileInputStream fileInputStream( File file, String operation )
63          throws ArchiverException
64      {
65          try
66          {
67              return new FileInputStream( file );
68          }
69          catch ( IOException e )
70          {
71              throw new ArchiverException(
72                  "Problem reading input file for " + operation + " " + file.getParent() + ", " + e.getMessage() );
73  
74          }
75      }
76  
77      public static FileOutputStream fileOutputStream( File file )
78          throws FileNotFoundException
79      {
80          return new FileOutputStream( file );
81      }
82  
83      public static FileOutputStream fileOutputStream( File file, String operation )
84          throws ArchiverException
85      {
86          try
87          {
88              return new FileOutputStream( file );
89          }
90          catch ( IOException e )
91          {
92              throw new ArchiverException(
93                  "Problem creating output file for " + operation + " " + file.getParent() + ", " + e.getMessage() );
94  
95          }
96      }
97  
98      public static void copyFully( @WillClose InputStream zIn, @WillClose OutputStream out, String gzip )
99          throws ArchiverException
100     {
101         // There must be 1 million libs out there that do this
102         try
103         {
104             copyFullyDontCloseOutput( zIn, out, gzip );
105             out.close();
106             out = null;
107         }
108         catch ( final IOException e )
109         {
110             throw new ArchiverException( "Failure copying.", e );
111         }
112         finally
113         {
114             IOUtil.close( out );
115         }
116     }
117 
118     public static void copyFullyDontCloseOutput( @WillClose InputStream zIn, @WillNotClose OutputStream out,
119                                                  String gzip )
120         throws ArchiverException
121     {
122         // There must be 1 million libs out there that do this
123         try
124         {
125             byte[] buffer = cacheBuffer();
126             int count = 0;
127             do
128             {
129                 try
130                 {
131                     out.write( buffer, 0, count );
132                 }
133                 catch ( IOException e )
134                 {
135                     throw new ArchiverException(
136                         "Problem writing to output in " + gzip + " operation " + e.getMessage() );
137 
138                 }
139                 count = zIn.read( buffer, 0, buffer.length );
140             }
141             while ( count != -1 );
142             zIn.close();
143             zIn = null;
144         }
145         catch ( IOException e )
146         {
147             throw new ArchiverException(
148                 "Problem reading from source file in " + gzip + " operation " + e.getMessage() );
149 
150         }
151         finally
152         {
153             IOUtil.close( zIn );
154         }
155     }
156 
157 }