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.camel.util.ObjectHelper;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * Some core java.nio based
035     * <a href="http://camel.apache.org/type-converter.html">Type Converters</a>
036     *
037     * @version $Revision: 825090 $
038     */
039    @Converter
040    public final class NIOConverter {
041        private static final transient Log LOG = LogFactory.getLog(NIOConverter.class);
042    
043        /**
044         * Utility classes should not have a public constructor.
045         */
046        private NIOConverter() {
047        }
048    
049        @Converter
050        public static byte[] toByteArray(ByteBuffer buffer) {
051            return buffer.array();
052        }
053    
054        @Converter
055        public static String toString(ByteBuffer buffer, Exchange exchange) throws IOException {
056            return IOConverter.toString(buffer.array(), exchange);
057        }
058    
059        @Converter
060        public static ByteBuffer toByteBuffer(byte[] data) {
061            return ByteBuffer.wrap(data);
062        }
063    
064        @Converter
065        public static ByteBuffer toByteBuffer(File file) throws IOException {
066            InputStream in = null;
067            try {
068                byte[] buf = new byte[(int)file.length()];
069                in = new BufferedInputStream(new FileInputStream(file));
070                int sizeLeft = (int)file.length();
071                int offset = 0;
072                while (sizeLeft > 0) {
073                    int readSize = in.read(buf, offset, sizeLeft);
074                    sizeLeft -= readSize;
075                    offset += readSize;
076                }
077                return ByteBuffer.wrap(buf);
078            } finally {
079                ObjectHelper.close(in, "Failed to close file stream: " + file.getPath(), LOG);
080            }
081        }
082    
083        @Converter
084        public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
085            ByteBuffer buf = ByteBuffer.allocate(value.length());
086            byte[] bytes = null;
087            if (exchange != null) {
088                String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
089                if (charsetName != null) {
090                    try {
091                        bytes = value.getBytes(charsetName);
092                    } catch (UnsupportedEncodingException e) {
093                        LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e);
094                    }
095                }
096            }
097            if (bytes == null) {
098                bytes = value.getBytes();
099            }
100            buf.put(bytes);
101            return buf;
102        }
103    
104        @Converter
105        public static ByteBuffer toByteBuffer(Short value) {
106            ByteBuffer buf = ByteBuffer.allocate(2);
107            buf.putShort(value);
108            return buf;
109        }
110    
111        @Converter
112        public static ByteBuffer toByteBuffer(Integer value) {
113            ByteBuffer buf = ByteBuffer.allocate(4);
114            buf.putInt(value);
115            return buf;
116        }
117    
118        @Converter
119        public static ByteBuffer toByteBuffer(Long value) {
120            ByteBuffer buf = ByteBuffer.allocate(8);
121            buf.putLong(value);
122            return buf;
123        }
124    
125        @Converter
126        public static ByteBuffer toByteBuffer(Float value) {
127            ByteBuffer buf = ByteBuffer.allocate(4);
128            buf.putFloat(value);
129            return buf;
130        }
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    }