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