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 ByteBuffer buf = ByteBuffer.allocate(value.length()); 088 byte[] bytes = null; 089 if (exchange != null) { 090 String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class); 091 if (charsetName != null) { 092 try { 093 bytes = value.getBytes(charsetName); 094 } catch (UnsupportedEncodingException e) { 095 LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e); 096 } 097 } 098 } 099 if (bytes == null) { 100 bytes = value.getBytes(); 101 } 102 buf.put(bytes); 103 buf.flip(); 104 return buf; 105 } 106 107 @Converter 108 public static ByteBuffer toByteBuffer(Short value) { 109 ByteBuffer buf = ByteBuffer.allocate(2); 110 buf.putShort(value); 111 buf.flip(); 112 return buf; 113 } 114 115 @Converter 116 public static ByteBuffer toByteBuffer(Integer value) { 117 ByteBuffer buf = ByteBuffer.allocate(4); 118 buf.putInt(value); 119 buf.flip(); 120 return buf; 121 } 122 123 @Converter 124 public static ByteBuffer toByteBuffer(Long value) { 125 ByteBuffer buf = ByteBuffer.allocate(8); 126 buf.putLong(value); 127 buf.flip(); 128 return buf; 129 } 130 131 @Converter 132 public static ByteBuffer toByteBuffer(Float value) { 133 ByteBuffer buf = ByteBuffer.allocate(4); 134 buf.putFloat(value); 135 buf.flip(); 136 return buf; 137 } 138 139 @Converter 140 public static ByteBuffer toByteBuffer(Double value) { 141 ByteBuffer buf = ByteBuffer.allocate(8); 142 buf.putDouble(value); 143 buf.flip(); 144 return buf; 145 } 146 147 @Converter 148 public static InputStream toInputStream(ByteBuffer bufferbuffer) { 149 return IOConverter.toInputStream(toByteArray(bufferbuffer)); 150 } 151}