001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.converter;
019    
020    import org.apache.camel.Converter;
021    
022    import java.io.*;
023    import java.nio.ByteBuffer;
024    
025    /**
026     * Some core java.nio based 
027     * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
028     *
029     * @version $Revision: 533630 $
030     */
031    @Converter
032    public class NIOConverter {
033    
034        @Converter
035        public static byte[] toByteArray(ByteBuffer buffer) {
036            return buffer.array();
037        }
038    
039        @Converter
040        public static ByteBuffer toByteBuffer(byte[] data) {
041            return ByteBuffer.wrap(data);
042        }
043        
044        @Converter
045        public static ByteBuffer toByteBuffer(String value) {
046            ByteBuffer buf = ByteBuffer.allocate(value.length());
047            byte[] bytes = value.getBytes();
048            buf.put(bytes);
049            return buf;
050        }
051        @Converter
052        public static ByteBuffer toByteBuffer(Short value) {
053            ByteBuffer buf = ByteBuffer.allocate(2);
054            buf.putShort(value);
055            return buf;
056        }
057        @Converter
058        public static ByteBuffer toByteBuffer(Integer value) {
059            ByteBuffer buf = ByteBuffer.allocate(4);
060            buf.putInt(value);
061            return buf;
062        }
063        @Converter
064        public static ByteBuffer toByteBuffer(Long value) {
065            ByteBuffer buf = ByteBuffer.allocate(8);
066            buf.putLong(value);
067            return buf;
068        }
069        @Converter
070        public static ByteBuffer toByteBuffer(Float value) {
071            ByteBuffer buf = ByteBuffer.allocate(4);
072            buf.putFloat(value);
073            return buf;
074        }
075        @Converter
076        public static ByteBuffer toByteBuffer(Double value) {
077            ByteBuffer buf = ByteBuffer.allocate(8);
078            buf.putDouble(value);
079            return buf;
080        }
081    }