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
018 package org.apache.camel.component.netty;
019
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.ObjectInput;
023 import java.io.ObjectInputStream;
024
025 import org.apache.camel.Converter;
026 import org.apache.camel.Exchange;
027 import org.jboss.netty.buffer.ChannelBuffer;
028 import org.jboss.netty.buffer.ChannelBufferInputStream;
029 import org.jboss.netty.buffer.DynamicChannelBuffer;
030
031 /**
032 * A set of converter methods for working with Netty types
033 *
034 * @version
035 */
036 @Converter
037 public final class NettyConverter {
038
039 private NettyConverter() {
040 //Utility Class
041 }
042
043 @Converter
044 public static byte[] toByteArray(ChannelBuffer buffer) {
045 return buffer.array();
046 }
047
048 @Converter
049 public static String toString(ChannelBuffer buffer, Exchange exchange) {
050 byte[] bytes = toByteArray(buffer);
051 // use type converter as it can handle encoding set on the Exchange
052 return exchange.getContext().getTypeConverter().convertTo(String.class, exchange, bytes);
053 }
054
055 @Converter
056 public static InputStream toInputStream(ChannelBuffer buffer) {
057 return new ChannelBufferInputStream(buffer);
058 }
059
060 @Converter
061 public static ObjectInput toObjectInput(ChannelBuffer buffer) throws IOException {
062 InputStream is = toInputStream(buffer);
063 return new ObjectInputStream(is);
064 }
065
066 @Converter
067 public static ChannelBuffer toByteBuffer(byte[] bytes) {
068 ChannelBuffer buf = new DynamicChannelBuffer(bytes.length);
069
070 buf.writeBytes(bytes);
071 return buf;
072 }
073 }