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 */
017package org.apache.camel.converter;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.UnsupportedEncodingException;
024import java.nio.ByteBuffer;
025
026import org.apache.camel.Converter;
027import org.apache.camel.Exchange;
028import org.apache.camel.util.IOHelper;
029import org.slf4j.Logger;
030import 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
039public final class NIOConverter {
040
041    private static final Logger LOG = LoggerFactory.getLogger(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        byte[] bArray = new byte[buffer.limit()];
052        buffer.get(bArray);
053        return bArray;
054    }
055
056    @Converter
057    public static String toString(ByteBuffer buffer, Exchange exchange) throws IOException {
058        return IOConverter.toString(toByteArray(buffer), exchange);
059    }
060
061    @Converter
062    public static ByteBuffer toByteBuffer(byte[] data) {
063        return ByteBuffer.wrap(data);
064    }
065
066    @Converter
067    public static ByteBuffer toByteBuffer(File file) throws IOException {
068        InputStream in = null;
069        try {
070            byte[] buf = new byte[(int)file.length()];
071            in = IOHelper.buffered(new FileInputStream(file));
072            int sizeLeft = (int)file.length();
073            int offset = 0;
074            while (sizeLeft > 0) {
075                int readSize = in.read(buf, offset, sizeLeft);
076                sizeLeft -= readSize;
077                offset += readSize;
078            }
079            return ByteBuffer.wrap(buf);
080        } finally {
081            IOHelper.close(in, "Failed to close file stream: " + file.getPath(), LOG);
082        }
083    }
084
085    @Converter
086    public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
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        return ByteBuffer.wrap(bytes);
102    }
103
104    @Converter
105    public static ByteBuffer toByteBuffer(Short value) {
106        ByteBuffer buf = ByteBuffer.allocate(2);
107        buf.putShort(value);
108        buf.flip();
109        return buf;
110    }
111
112    @Converter
113    public static ByteBuffer toByteBuffer(Integer value) {
114        ByteBuffer buf = ByteBuffer.allocate(4);
115        buf.putInt(value);
116        buf.flip();
117        return buf;
118    }
119
120    @Converter
121    public static ByteBuffer toByteBuffer(Long value) {
122        ByteBuffer buf = ByteBuffer.allocate(8);
123        buf.putLong(value);
124        buf.flip();
125        return buf;
126    }
127
128    @Converter
129    public static ByteBuffer toByteBuffer(Float value) {
130        ByteBuffer buf = ByteBuffer.allocate(4);
131        buf.putFloat(value);
132        buf.flip();
133        return buf;
134    }
135
136    @Converter
137    public static ByteBuffer toByteBuffer(Double value) {
138        ByteBuffer buf = ByteBuffer.allocate(8);
139        buf.putDouble(value);
140        buf.flip();
141        return buf;
142    }
143
144    @Converter
145    public static InputStream toInputStream(ByteBuffer bufferbuffer) {
146        return IOConverter.toInputStream(toByteArray(bufferbuffer));
147    }
148}