Coverage Report - org.apache.any23.util.StreamUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamUtils
0%
0/24
0%
0/10
2.8
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *  http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.any23.util;
 19  
 
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import java.io.BufferedReader;
 24  
 import java.io.Closeable;
 25  
 import java.io.IOException;
 26  
 import java.io.InputStream;
 27  
 import java.io.InputStreamReader;
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * Contains general utility functions for handling streams.
 33  
  *
 34  
  * @author Michele Mostarda (mostarda@fbk.eu)
 35  
  */
 36  
 public class StreamUtils {
 37  
 
 38  0
     private static final Logger logger = LoggerFactory.getLogger(StreamUtils.class);
 39  
 
 40  0
     private StreamUtils(){}
 41  
 
 42  
     /**
 43  
      * Returns all the lines read from an input stream.
 44  
      *
 45  
      * @param is input stream.
 46  
      * @return list of not <code>null</code> lines.
 47  
      * @throws IOException
 48  
      */
 49  
     public static String[] asLines(InputStream is) throws IOException {
 50  0
         final BufferedReader br = new BufferedReader(new InputStreamReader(is));
 51  0
         final List<String> lines = new ArrayList<String>();
 52  
         try {
 53  
             String line;
 54  0
             while ((line = br.readLine()) != null) {
 55  0
                 lines.add(line);
 56  
             }
 57  0
             return lines.toArray( new String[ lines.size() ] );
 58  
         } finally {
 59  0
             closeGracefully(br);
 60  
         }
 61  
     }
 62  
 
 63  
     /**
 64  
      * Returns the string content of a stream.
 65  
      *
 66  
      * @param is input stream.
 67  
      * @param preserveNL preserves new line chars.
 68  
      * @return the string content.
 69  
      * @throws IOException if an error occurs while consuming the <code>is</code> stream.
 70  
      */
 71  
     public static String asString(InputStream is, boolean preserveNL) throws IOException {
 72  0
         if (is == null) {
 73  0
             throw new NullPointerException("input stream is null.");
 74  
         }
 75  0
         final BufferedReader br = new BufferedReader(new InputStreamReader(is));
 76  
         try {
 77  0
             final StringBuilder content = new StringBuilder();
 78  
             String line;
 79  0
             while ((line = br.readLine()) != null) {
 80  0
                 content.append(line);
 81  0
                 if(preserveNL) content.append('\n');
 82  
             }
 83  0
             return content.toString();
 84  
         } finally {
 85  0
             closeGracefully(br);
 86  
         }
 87  
     }
 88  
 
 89  
     /**
 90  
      * Returns the string content of a stream, new line chars will be removed.
 91  
      *
 92  
      * @param is input stream.
 93  
      * @return the string content.
 94  
      * @throws IOException if an error occurs while consuming the <code>is</code> stream.
 95  
      */
 96  
      public static String asString(InputStream is) throws IOException {
 97  0
          return asString(is, false);
 98  
      }
 99  
 
 100  
     /**
 101  
      * Closes the closable interface and reports error if any.
 102  
      *
 103  
      * @param closable the closable object to be closed.
 104  
      */
 105  
     public static void closeGracefully(Closeable closable) {
 106  0
         if (closable != null) {
 107  
             try {
 108  0
                 closable.close();
 109  0
             } catch (Exception e) {
 110  0
                 logger.error("Error while closing object " + closable, e);
 111  0
             }
 112  
         }
 113  0
     }
 114  
 
 115  
 }