Coverage Report - org.codehaus.plexus.archiver.util.Streams
 
Classes in this File Line Coverage Branch Coverage Complexity
Streams
48%
17/35
83%
5/6
2,667
 
 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  0
 public class Streams
 33  
 {
 34  
 
 35  
     public static BufferedInputStream bufferedInputStream( InputStream is )
 36  
     {
 37  69
         return is instanceof BufferedInputStream
 38  
                    ? (BufferedInputStream) is
 39  
                    : new BufferedInputStream( is, 65536 );
 40  
 
 41  
     }
 42  
 
 43  
     public static BufferedOutputStream bufferedOutputStream( OutputStream os )
 44  
     {
 45  74
         return os instanceof BufferedOutputStream
 46  
                    ? (BufferedOutputStream) os
 47  
                    : new BufferedOutputStream( os, 65536 );
 48  
 
 49  
     }
 50  
 
 51  
     public static byte[] cacheBuffer()
 52  
     {
 53  2782
         return new byte[ 8 * 1024 ];
 54  
     }
 55  
 
 56  
     public static FileInputStream fileInputStream( File file )
 57  
         throws FileNotFoundException
 58  
     {
 59  8
         return new FileInputStream( file );
 60  
     }
 61  
 
 62  
     public static FileInputStream fileInputStream( File file, String operation )
 63  
         throws ArchiverException
 64  
     {
 65  
         try
 66  
         {
 67  0
             return new FileInputStream( file );
 68  
         }
 69  0
         catch ( IOException e )
 70  
         {
 71  0
             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  18
         return new FileOutputStream( file );
 81  
     }
 82  
 
 83  
     public static FileOutputStream fileOutputStream( File file, String operation )
 84  
         throws ArchiverException
 85  
     {
 86  
         try
 87  
         {
 88  48
             return new FileOutputStream( file );
 89  
         }
 90  0
         catch ( IOException e )
 91  
         {
 92  0
             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  0
             copyFullyDontCloseOutput( zIn, out, gzip );
 105  0
             out.close();
 106  0
             out = null;
 107  
         }
 108  0
         catch ( final IOException e )
 109  
         {
 110  0
             throw new ArchiverException( "Failure copying.", e );
 111  
         }
 112  
         finally
 113  
         {
 114  0
             IOUtil.close( out );
 115  0
         }
 116  0
     }
 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  2782
             byte[] buffer = cacheBuffer();
 126  2782
             int count = 0;
 127  
             do
 128  
             {
 129  
                 try
 130  
                 {
 131  6157
                     out.write( buffer, 0, count );
 132  
                 }
 133  0
                 catch ( IOException e )
 134  
                 {
 135  0
                     throw new ArchiverException(
 136  
                         "Problem writing to output in " + gzip + " operation " + e.getMessage() );
 137  
 
 138  6157
                 }
 139  6157
                 count = zIn.read( buffer, 0, buffer.length );
 140  
             }
 141  6157
             while ( count != -1 );
 142  2782
             zIn.close();
 143  2782
             zIn = null;
 144  
         }
 145  0
         catch ( IOException e )
 146  
         {
 147  0
             throw new ArchiverException(
 148  
                 "Problem reading from source file in " + gzip + " operation " + e.getMessage() );
 149  
 
 150  
         }
 151  
         finally
 152  
         {
 153  2782
             IOUtil.close( zIn );
 154  2782
         }
 155  2782
     }
 156  
 
 157  
 }