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.BufferedInputStream;
020    import java.io.File;
021    import java.io.FileInputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.UnsupportedEncodingException;
025    import java.nio.ByteBuffer;
026    
027    import org.apache.camel.Converter;
028    import org.apache.camel.Exchange;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    /**
033     * Some core java.nio based
034     * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
035     *
036     * @version $Revision: 51083 $
037     */
038    @Converter
039    public final class NIOConverter {
040        private static final transient Log LOG = LogFactory.getLog(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) {
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 = new BufferedInputStream(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                try {
079                    if (in != null) {
080                        in.close();
081                    }
082                } catch (IOException e) {
083                    LOG.warn("Failed to close file stream: " + file.getPath(), e);
084                }
085            }
086        }
087    
088        @Converter
089        public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
090            ByteBuffer buf = ByteBuffer.allocate(value.length());
091            byte[] bytes = null;
092            if (exchange != null) {
093                String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
094                if (charsetName != null) {
095                    try {
096                        bytes = value.getBytes(charsetName);
097                    } catch (UnsupportedEncodingException e) {
098                        LOG.warn("Can't convert the byte to String with the charset " + charsetName, e);
099                    }
100                }
101            }
102            if (bytes == null) {
103                bytes = value.getBytes();
104            }
105            buf.put(bytes);
106            return buf;
107        }
108        @Converter
109        public static ByteBuffer toByteBuffer(Short value) {
110            ByteBuffer buf = ByteBuffer.allocate(2);
111            buf.putShort(value);
112            return buf;
113        }
114        @Converter
115        public static ByteBuffer toByteBuffer(Integer value) {
116            ByteBuffer buf = ByteBuffer.allocate(4);
117            buf.putInt(value);
118            return buf;
119        }
120        @Converter
121        public static ByteBuffer toByteBuffer(Long value) {
122            ByteBuffer buf = ByteBuffer.allocate(8);
123            buf.putLong(value);
124            return buf;
125        }
126        @Converter
127        public static ByteBuffer toByteBuffer(Float value) {
128            ByteBuffer buf = ByteBuffer.allocate(4);
129            buf.putFloat(value);
130            return buf;
131        }
132        @Converter
133        public static ByteBuffer toByteBuffer(Double value) {
134            ByteBuffer buf = ByteBuffer.allocate(8);
135            buf.putDouble(value);
136            return buf;
137        }
138    
139        @Converter
140        public static InputStream toInputStream(ByteBuffer bufferbuffer) {
141            return IOConverter.toInputStream(toByteArray(bufferbuffer));
142        }
143    }