Coverage Report - org.apache.camel.converter.NIOConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
NIOConverter
0% 
N/A 
0
 
 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  
 package org.apache.camel.converter;
 18  
 
 19  
 import org.apache.camel.Converter;
 20  
 
 21  
 import java.io.BufferedInputStream;
 22  
 import java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.nio.ByteBuffer;
 27  
 
 28  
 /**
 29  
  * Some core java.nio based 
 30  
  * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
 31  
  *
 32  
  * @version $Revision: 564495 $
 33  
  */
 34  
 @Converter
 35  
 public class NIOConverter {
 36  
 
 37  
     /**
 38  
      * Utility classes should not have a public constructor.
 39  
      */
 40  0
     private NIOConverter() {        
 41  0
     }
 42  
 
 43  
     @Converter
 44  
     public static byte[] toByteArray(ByteBuffer buffer) {
 45  0
         return buffer.array();
 46  
     }
 47  
 
 48  
     @Converter
 49  
     public static String toString(ByteBuffer buffer) {
 50  0
         return IOConverter.toString(buffer.array());
 51  
     }
 52  
 
 53  
     @Converter
 54  
     public static ByteBuffer toByteBuffer(byte[] data) {
 55  0
         return ByteBuffer.wrap(data);
 56  
     }
 57  
     
 58  
     @Converter
 59  
     public static ByteBuffer toByteBuffer(File file) throws IOException {
 60  0
        byte[] buf = new byte[(int) file.length()];
 61  0
        InputStream in = new BufferedInputStream(new FileInputStream(file));
 62  0
        in.read(buf);
 63  0
        return ByteBuffer.wrap(buf);
 64  
     }
 65  
 
 66  
     @Converter
 67  
     public static ByteBuffer toByteBuffer(String value) {
 68  0
         ByteBuffer buf = ByteBuffer.allocate(value.length());
 69  0
         byte[] bytes = value.getBytes();
 70  0
         buf.put(bytes);
 71  0
         return buf;
 72  
     }
 73  
     @Converter
 74  
     public static ByteBuffer toByteBuffer(Short value) {
 75  0
         ByteBuffer buf = ByteBuffer.allocate(2);
 76  0
         buf.putShort(value);
 77  0
         return buf;
 78  
     }
 79  
     @Converter
 80  
     public static ByteBuffer toByteBuffer(Integer value) {
 81  0
         ByteBuffer buf = ByteBuffer.allocate(4);
 82  0
         buf.putInt(value);
 83  0
         return buf;
 84  
     }
 85  
     @Converter
 86  
     public static ByteBuffer toByteBuffer(Long value) {
 87  0
         ByteBuffer buf = ByteBuffer.allocate(8);
 88  0
         buf.putLong(value);
 89  0
         return buf;
 90  
     }
 91  
     @Converter
 92  
     public static ByteBuffer toByteBuffer(Float value) {
 93  0
         ByteBuffer buf = ByteBuffer.allocate(4);
 94  0
         buf.putFloat(value);
 95  0
         return buf;
 96  
     }
 97  
     @Converter
 98  
     public static ByteBuffer toByteBuffer(Double value) {
 99  0
         ByteBuffer buf = ByteBuffer.allocate(8);
 100  0
         buf.putDouble(value);
 101  0
         return buf;
 102  
     }
 103  
 
 104  
     @Converter
 105  
     public static InputStream toInputStream(ByteBuffer bufferbuffer) {
 106  0
         return IOConverter.toInputStream(toByteArray(bufferbuffer));
 107  
     }
 108  
 }