001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.converter;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.UnsupportedEncodingException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.camel.Converter;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.util.IOHelper;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     * Some core java.nio based
034     * <a href="http://camel.apache.org/type-converter.html">Type Converters</a>
035     *
036     * @version 
037     */
038    @Converter
039    public final class NIOConverter {
040        private static final transient Logger LOG = LoggerFactory.getLogger(NIOConverter.class);
041    
042        /**
043         * Utility classes should not have a public constructor.
044         */
045        private NIOConverter() {
046        }
047    
048        @Converter
049        public static byte[] toByteArray(ByteBuffer buffer) {
050            return buffer.array();
051        }
052    
053        @Converter
054        public static String toString(ByteBuffer buffer, Exchange exchange) throws IOException {
055            return IOConverter.toString(buffer.array(), exchange);
056        }
057    
058        @Converter
059        public static ByteBuffer toByteBuffer(byte[] data) {
060            return ByteBuffer.wrap(data);
061        }
062    
063        @Converter
064        public static ByteBuffer toByteBuffer(File file) throws IOException {
065            InputStream in = null;
066            try {
067                byte[] buf = new byte[(int)file.length()];
068                in = IOHelper.buffered(new FileInputStream(file));
069                int sizeLeft = (int)file.length();
070                int offset = 0;
071                while (sizeLeft > 0) {
072                    int readSize = in.read(buf, offset, sizeLeft);
073                    sizeLeft -= readSize;
074                    offset += readSize;
075                }
076                return ByteBuffer.wrap(buf);
077            } finally {
078                IOHelper.close(in, "Failed to close file stream: " + file.getPath(), LOG);
079            }
080        }
081    
082        @Converter
083        public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
084            ByteBuffer buf = ByteBuffer.allocate(value.length());
085            byte[] bytes = null;
086            if (exchange != null) {
087                String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
088                if (charsetName != null) {
089                    try {
090                        bytes = value.getBytes(charsetName);
091                    } catch (UnsupportedEncodingException e) {
092                        LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e);
093                    }
094                }
095            }
096            if (bytes == null) {
097                bytes = value.getBytes();
098            }
099            buf.put(bytes);
100            return buf;
101        }
102    
103        @Converter
104        public static ByteBuffer toByteBuffer(Short value) {
105            ByteBuffer buf = ByteBuffer.allocate(2);
106            buf.putShort(value);
107            return buf;
108        }
109    
110        @Converter
111        public static ByteBuffer toByteBuffer(Integer value) {
112            ByteBuffer buf = ByteBuffer.allocate(4);
113            buf.putInt(value);
114            return buf;
115        }
116    
117        @Converter
118        public static ByteBuffer toByteBuffer(Long value) {
119            ByteBuffer buf = ByteBuffer.allocate(8);
120            buf.putLong(value);
121            return buf;
122        }
123    
124        @Converter
125        public static ByteBuffer toByteBuffer(Float value) {
126            ByteBuffer buf = ByteBuffer.allocate(4);
127            buf.putFloat(value);
128            return buf;
129        }
130    
131        @Converter
132        public static ByteBuffer toByteBuffer(Double value) {
133            ByteBuffer buf = ByteBuffer.allocate(8);
134            buf.putDouble(value);
135            return buf;
136        }
137    
138        @Converter
139        public static InputStream toInputStream(ByteBuffer bufferbuffer) {
140            return IOConverter.toInputStream(toByteArray(bufferbuffer));
141        }
142    }