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.InputStream; 021import java.nio.ByteBuffer; 022 023import org.apache.camel.Exchange; 024 025/** 026 * Optimised {@link NIOConverter} 027 */ 028public final class NIOConverterOptimised { 029 030 private NIOConverterOptimised() { 031 } 032 033 public static Object convertTo(final Class<?> type, final Exchange exchange, final Object value) throws Exception { 034 Class fromType = value.getClass(); 035 036 if (type == String.class) { 037 if (fromType == ByteBuffer.class) { 038 return NIOConverter.toString((ByteBuffer) value, exchange); 039 } 040 return null; 041 } 042 043 if (type == byte[].class) { 044 if (fromType == ByteBuffer.class) { 045 return NIOConverter.toByteArray((ByteBuffer) value); 046 } 047 return null; 048 } 049 050 if (type == InputStream.class) { 051 if (fromType == ByteBuffer.class) { 052 return NIOConverter.toInputStream((ByteBuffer) value); 053 } 054 return null; 055 } 056 057 if (type == ByteBuffer.class) { 058 if (fromType == byte[].class) { 059 return NIOConverter.toByteBuffer((byte[]) value); 060 } else if (fromType == File.class) { 061 return NIOConverter.toByteBuffer((File) value); 062 } else if (fromType == String.class) { 063 return NIOConverter.toByteBuffer((String) value, exchange); 064 } else if (fromType == short.class || fromType == Short.class) { 065 return NIOConverter.toByteBuffer((Short) value); 066 } else if (fromType == int.class || fromType == Integer.class) { 067 return NIOConverter.toByteBuffer((Integer) value); 068 } else if (fromType == long.class || fromType == Long.class) { 069 return NIOConverter.toByteBuffer((Long) value); 070 } else if (fromType == float.class || fromType == Float.class) { 071 return NIOConverter.toByteBuffer((Float) value); 072 } else if (fromType == double.class || fromType == Double.class) { 073 return NIOConverter.toByteBuffer((Double) value); 074 } 075 return null; 076 } 077 078 // no optimised type converter found 079 return null; 080 } 081 082}