Coverage Report - org.apache.camel.converter.IOConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
IOConverter
64% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.converter;
 19  
 
 20  
 import org.apache.camel.Converter;
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 import java.io.*;
 25  
 import java.nio.ByteBuffer;
 26  
 
 27  
 /**
 28  
  * Some core java.io based
 29  
  * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
 30  
  *
 31  
  * @version $Revision: 544115 $
 32  
  */
 33  
 @Converter
 34  0
 public class IOConverter {
 35  1
     private static final transient Log log = LogFactory.getLog(IOConverter.class);
 36  
 
 37  
     @Converter
 38  
     public static InputStream toInputStream(File file) throws FileNotFoundException {
 39  0
         return new BufferedInputStream(new FileInputStream(file));
 40  
     }
 41  
 
 42  
     @Converter
 43  
     public static BufferedReader toReader(File file) throws FileNotFoundException {
 44  0
         return new BufferedReader(new FileReader(file));
 45  
     }
 46  
 
 47  
     @Converter
 48  
     public static OutputStream toOutputStream(File file) throws FileNotFoundException {
 49  0
         return new BufferedOutputStream(new FileOutputStream(file));
 50  
     }
 51  
 
 52  
     @Converter
 53  
     public static BufferedWriter toWriter(File file) throws IOException {
 54  0
         return new BufferedWriter(new FileWriter(file));
 55  
     }
 56  
 
 57  
     @Converter
 58  
     public static Reader toReader(InputStream in) throws FileNotFoundException {
 59  1
         return new InputStreamReader(in);
 60  
     }
 61  
 
 62  
     @Converter
 63  
     public static Writer toWriter(OutputStream out) throws FileNotFoundException {
 64  0
         return new OutputStreamWriter(out);
 65  
     }
 66  
 
 67  
 
 68  
     @Converter
 69  
     public static StringReader toReader(String text) {
 70  
         // TODO could we automatically find this?
 71  0
         return new StringReader(text);
 72  
     }
 73  
 
 74  
     @Converter
 75  
     public static InputStream toInputStream(String text) {
 76  1
         return toInputStream(text.getBytes());
 77  
     }
 78  
 
 79  
     @Converter
 80  
     public static byte[] toByteArray(String text) {
 81  
         // TODO could we automatically find this?
 82  1
         return text.getBytes();
 83  
     }
 84  
 
 85  
     @Converter
 86  
     public static String toString(byte[] data) {
 87  1
         return new String(data);
 88  
     }
 89  
 
 90  
     @Converter
 91  
     public static String toString(Reader reader) throws IOException {
 92  1
         if (reader instanceof BufferedReader) {
 93  0
             return toString((BufferedReader) reader);
 94  
         }
 95  
         else {
 96  1
             return toString(new BufferedReader(reader));
 97  
         }
 98  
     }
 99  
 
 100  
     @Converter
 101  
     public static String toString(BufferedReader reader) throws IOException {
 102  1
         if (reader == null) {
 103  0
             return null;
 104  
         }
 105  
         try {
 106  1
             StringBuilder builder = new StringBuilder();
 107  1
             boolean first = true;
 108  
             while (true) {
 109  2
                 String line = reader.readLine();
 110  2
                 if (line == null) {
 111  1
                     return builder.toString();
 112  
                 }
 113  1
                 if (first) {
 114  1
                     first = false;
 115  1
                 }
 116  
                 else {
 117  0
                     builder.append("\n");
 118  
                 }
 119  1
                 builder.append(line);
 120  1
             }
 121  
         }
 122  
         finally {
 123  0
             try {
 124  1
                 reader.close();
 125  
             }
 126  0
             catch (IOException e) {
 127  0
                 log.warn("Failed to close stream: "+ e, e);
 128  1
             }
 129  1
         }
 130  
     }
 131  
     
 132  
     @Converter
 133  
     public static String toString(InputStream in) throws IOException {
 134  1
         return toString(toReader(in));
 135  
     }
 136  
 
 137  
     @Converter
 138  
     public static InputStream toInputStream(byte[] data) {
 139  1
         return new ByteArrayInputStream(data);
 140  
     }
 141  
 }