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.BufferedInputStream;
020import java.io.BufferedReader;
021import java.io.ByteArrayOutputStream;
022import java.io.File;
023import java.io.InputStream;
024import java.io.ObjectInput;
025import java.io.ObjectOutput;
026import java.io.OutputStream;
027import java.io.Reader;
028import java.io.Writer;
029import java.net.URL;
030import java.util.Properties;
031
032import org.apache.camel.Exchange;
033import org.apache.camel.StreamCache;
034
035/**
036 * Optimised {@link IOConverter}
037 */
038public final class IOConverterOptimised {
039
040    private IOConverterOptimised() {
041    }
042
043    // CHECKSTYLE:OFF
044    public static Object convertTo(final Class<?> type, final Exchange exchange, final Object value) throws Exception {
045        Class fromType = value.getClass();
046
047        // if the value is StreamCache then ensure its readable before doing conversions
048        // by resetting it (this is also what StreamCachingAdvice does)
049        if (value instanceof StreamCache) {
050            ((StreamCache) value).reset();
051        }
052
053        if (type == InputStream.class) {
054            if (fromType == String.class) {
055                return IOConverter.toInputStream((String) value, exchange);
056            } else if (fromType == URL.class) {
057                return IOConverter.toInputStream((URL) value);
058            } else if (fromType == File.class) {
059                return IOConverter.toInputStream((File) value);
060            } else if (fromType == byte[].class) {
061                return IOConverter.toInputStream((byte[]) value);
062            } else if (fromType == ByteArrayOutputStream.class) {
063                return IOConverter.toInputStream((ByteArrayOutputStream) value);
064            } else if (fromType == BufferedReader.class) {
065                return IOConverter.toInputStream((BufferedReader) value, exchange);
066            } else if (fromType == StringBuilder.class) {
067                return IOConverter.toInputStream((StringBuilder) value, exchange);
068            }
069            return null;
070        }
071
072        if (type == Reader.class) {
073            if (fromType == File.class) {
074                return IOConverter.toReader((File) value, exchange);
075            } else if (fromType == String.class) {
076                return IOConverter.toReader((String) value);
077            } else if (InputStream.class.isAssignableFrom(fromType)) {
078                return IOConverter.toReader((InputStream) value, exchange);
079            }
080            return null;
081        }
082
083        if (type == File.class) {
084            if (fromType == String.class) {
085                return IOConverter.toFile((String) value);
086            }
087            return null;
088        }
089
090        if (type == OutputStream.class) {
091            if (fromType == File.class) {
092                return IOConverter.toOutputStream((File) value);
093            }
094            return null;
095        }
096
097        if (type == Writer.class) {
098            if (fromType == File.class) {
099                return IOConverter.toWriter((File) value, exchange);
100            } else if (OutputStream.class.isAssignableFrom(fromType)) {
101                return IOConverter.toWriter((OutputStream) value, exchange);
102            }
103            return null;
104        }
105
106        if (type == String.class) {
107            if (fromType == byte[].class) {
108                return IOConverter.toString((byte[]) value, exchange);
109            } else if (fromType == File.class) {
110                return IOConverter.toString((File) value, exchange);
111            } else if (fromType == URL.class) {
112                return IOConverter.toString((URL) value, exchange);
113            } else if (fromType == BufferedReader.class) {
114                return IOConverter.toString((BufferedReader) value);
115            } else if (Reader.class.isAssignableFrom(fromType)) {
116                return IOConverter.toString((Reader) value);
117            } else if (InputStream.class.isAssignableFrom(fromType)) {
118                return IOConverter.toString((InputStream) value, exchange);
119            } else if (fromType == ByteArrayOutputStream.class) {
120                return IOConverter.toString((ByteArrayOutputStream) value, exchange);
121            }
122            return null;
123        }
124
125        if (type == byte[].class) {
126            if (fromType == BufferedReader.class) {
127                return IOConverter.toByteArray((BufferedReader) value, exchange);
128            } else if (Reader.class.isAssignableFrom(fromType)) {
129                return IOConverter.toByteArray((Reader) value, exchange);
130            } else if (fromType == File.class) {
131                return IOConverter.toByteArray((File) value);
132            } else if (fromType == String.class) {
133                return IOConverter.toByteArray((String) value, exchange);
134            } else if (fromType == ByteArrayOutputStream.class) {
135                return IOConverter.toByteArray((ByteArrayOutputStream) value);
136            } else if (InputStream.class.isAssignableFrom(fromType)) {
137                return IOConverter.toBytes((InputStream) value);
138            }
139            return null;
140        }
141
142        if (type == ObjectInput.class) {
143            if (fromType == InputStream.class || fromType == BufferedInputStream.class) {
144                return IOConverter.toObjectInput((InputStream) value, exchange);
145            }
146            return null;
147        }
148
149        if (type == ObjectOutput.class) {
150            if (fromType == OutputStream.class) {
151                return IOConverter.toObjectOutput((OutputStream) value);
152            }
153            return null;
154        }
155
156        if (type == Properties.class) {
157            if (fromType == File.class) {
158                return IOConverter.toProperties((File) value);
159            } else if (fromType == InputStream.class) {
160                return IOConverter.toProperties((InputStream) value);
161            } else if (fromType == Reader.class) {
162                return IOConverter.toProperties((Reader) value);
163            }
164            return null;
165        }
166
167        // no optimised type converter found
168        return null;
169    }
170    // CHECKSTYLE:ON
171
172}