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 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            byte[] bArray = new byte[buffer.limit()];
051            buffer.get(bArray);
052            return bArray;
053        }
054    
055        @Converter
056        public static String toString(ByteBuffer buffer, Exchange exchange) throws IOException {
057            return IOConverter.toString(toByteArray(buffer), exchange);
058        }
059    
060        @Converter
061        public static ByteBuffer toByteBuffer(byte[] data) {
062            return ByteBuffer.wrap(data);
063        }
064    
065        @Converter
066        public static ByteBuffer toByteBuffer(File file) throws IOException {
067            InputStream in = null;
068            try {
069                byte[] buf = new byte[(int)file.length()];
070                in = IOHelper.buffered(new FileInputStream(file));
071                int sizeLeft = (int)file.length();
072                int offset = 0;
073                while (sizeLeft > 0) {
074                    int readSize = in.read(buf, offset, sizeLeft);
075                    sizeLeft -= readSize;
076                    offset += readSize;
077                }
078                return ByteBuffer.wrap(buf);
079            } finally {
080                IOHelper.close(in, "Failed to close file stream: " + file.getPath(), LOG);
081            }
082        }
083    
084        @Converter
085        public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
086            ByteBuffer buf = ByteBuffer.allocate(value.length());
087            byte[] bytes = null;
088            if (exchange != null) {
089                String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
090                if (charsetName != null) {
091                    try {
092                        bytes = value.getBytes(charsetName);
093                    } catch (UnsupportedEncodingException e) {
094                        LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e);
095                    }
096                }
097            }
098            if (bytes == null) {
099                bytes = value.getBytes();
100            }
101            buf.put(bytes);
102            buf.flip();
103            return buf;
104        }
105    
106        @Converter
107        public static ByteBuffer toByteBuffer(Short value) {
108            ByteBuffer buf = ByteBuffer.allocate(2);
109            buf.putShort(value);
110            buf.flip();
111            return buf;
112        }
113    
114        @Converter
115        public static ByteBuffer toByteBuffer(Integer value) {
116            ByteBuffer buf = ByteBuffer.allocate(4);
117            buf.putInt(value);
118            buf.flip();
119            return buf;
120        }
121    
122        @Converter
123        public static ByteBuffer toByteBuffer(Long value) {
124            ByteBuffer buf = ByteBuffer.allocate(8);
125            buf.putLong(value);
126            buf.flip();
127            return buf;
128        }
129    
130        @Converter
131        public static ByteBuffer toByteBuffer(Float value) {
132            ByteBuffer buf = ByteBuffer.allocate(4);
133            buf.putFloat(value);
134            buf.flip();
135            return buf;
136        }
137    
138        @Converter
139        public static ByteBuffer toByteBuffer(Double value) {
140            ByteBuffer buf = ByteBuffer.allocate(8);
141            buf.putDouble(value);
142            buf.flip();
143            return buf;
144        }
145    
146        @Converter
147        public static InputStream toInputStream(ByteBuffer bufferbuffer) {
148            return IOConverter.toInputStream(toByteArray(bufferbuffer));
149        }
150    }