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: 668062 $ 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 } 080 } catch (IOException e) { 081 LOG.warn("Failed to close file stream: " + file.getPath(), e); 082 } 083 } 084 } 085 086 @Converter 087 public static ByteBuffer toByteBuffer(String value) { 088 ByteBuffer buf = ByteBuffer.allocate(value.length()); 089 byte[] bytes = value.getBytes(); 090 buf.put(bytes); 091 return buf; 092 } 093 @Converter 094 public static ByteBuffer toByteBuffer(Short value) { 095 ByteBuffer buf = ByteBuffer.allocate(2); 096 buf.putShort(value); 097 return buf; 098 } 099 @Converter 100 public static ByteBuffer toByteBuffer(Integer value) { 101 ByteBuffer buf = ByteBuffer.allocate(4); 102 buf.putInt(value); 103 return buf; 104 } 105 @Converter 106 public static ByteBuffer toByteBuffer(Long value) { 107 ByteBuffer buf = ByteBuffer.allocate(8); 108 buf.putLong(value); 109 return buf; 110 } 111 @Converter 112 public static ByteBuffer toByteBuffer(Float value) { 113 ByteBuffer buf = ByteBuffer.allocate(4); 114 buf.putFloat(value); 115 return buf; 116 } 117 @Converter 118 public static ByteBuffer toByteBuffer(Double value) { 119 ByteBuffer buf = ByteBuffer.allocate(8); 120 buf.putDouble(value); 121 return buf; 122 } 123 124 @Converter 125 public static InputStream toInputStream(ByteBuffer bufferbuffer) { 126 return IOConverter.toInputStream(toByteArray(bufferbuffer)); 127 } 128 }