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 org.apache.camel.Converter;
020
021 import java.io.BufferedInputStream;
022 import java.io.File;
023 import java.io.FileInputStream;
024 import java.io.IOException;
025 import java.io.InputStream;
026 import java.nio.ByteBuffer;
027
028 /**
029 * Some core java.nio based
030 * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
031 *
032 * @version $Revision: 564495 $
033 */
034 @Converter
035 public class NIOConverter {
036
037 /**
038 * Utility classes should not have a public constructor.
039 */
040 private NIOConverter() {
041 }
042
043 @Converter
044 public static byte[] toByteArray(ByteBuffer buffer) {
045 return buffer.array();
046 }
047
048 @Converter
049 public static String toString(ByteBuffer buffer) {
050 return IOConverter.toString(buffer.array());
051 }
052
053 @Converter
054 public static ByteBuffer toByteBuffer(byte[] data) {
055 return ByteBuffer.wrap(data);
056 }
057
058 @Converter
059 public static ByteBuffer toByteBuffer(File file) throws IOException {
060 byte[] buf = new byte[(int) file.length()];
061 InputStream in = new BufferedInputStream(new FileInputStream(file));
062 in.read(buf);
063 return ByteBuffer.wrap(buf);
064 }
065
066 @Converter
067 public static ByteBuffer toByteBuffer(String value) {
068 ByteBuffer buf = ByteBuffer.allocate(value.length());
069 byte[] bytes = value.getBytes();
070 buf.put(bytes);
071 return buf;
072 }
073 @Converter
074 public static ByteBuffer toByteBuffer(Short value) {
075 ByteBuffer buf = ByteBuffer.allocate(2);
076 buf.putShort(value);
077 return buf;
078 }
079 @Converter
080 public static ByteBuffer toByteBuffer(Integer value) {
081 ByteBuffer buf = ByteBuffer.allocate(4);
082 buf.putInt(value);
083 return buf;
084 }
085 @Converter
086 public static ByteBuffer toByteBuffer(Long value) {
087 ByteBuffer buf = ByteBuffer.allocate(8);
088 buf.putLong(value);
089 return buf;
090 }
091 @Converter
092 public static ByteBuffer toByteBuffer(Float value) {
093 ByteBuffer buf = ByteBuffer.allocate(4);
094 buf.putFloat(value);
095 return buf;
096 }
097 @Converter
098 public static ByteBuffer toByteBuffer(Double value) {
099 ByteBuffer buf = ByteBuffer.allocate(8);
100 buf.putDouble(value);
101 return buf;
102 }
103
104 @Converter
105 public static InputStream toInputStream(ByteBuffer bufferbuffer) {
106 return IOConverter.toInputStream(toByteArray(bufferbuffer));
107 }
108 }