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.activemq.transport.util;
018    
019    import java.io.DataInput;
020    import java.io.DataInputStream;
021    import java.io.DataOutput;
022    import java.io.DataOutputStream;
023    import java.io.IOException;
024    import java.io.Reader;
025    import org.apache.activemq.util.ByteArrayInputStream;
026    import org.apache.activemq.util.ByteArrayOutputStream;
027    import org.apache.activemq.util.ByteSequence;
028    import org.apache.activemq.wireformat.WireFormat;
029    
030    /**
031     * Adds the extra methods available to text based wire format implementations
032     * 
033     * @version $Revision: 1.1 $
034     */
035    public abstract class TextWireFormat implements WireFormat {
036    
037        public abstract Object unmarshalText(String text);
038    
039        public abstract Object unmarshalText(Reader reader);
040    
041        public abstract String marshalText(Object command);
042    
043        public void marshal(Object command, DataOutput out) throws IOException {
044            out.writeUTF(marshalText(command));
045        }
046    
047        public Object unmarshal(DataInput in) throws IOException {
048            String text = in.readUTF();
049            return unmarshalText(text);
050        }
051    
052        public ByteSequence marshal(Object command) throws IOException {
053            ByteArrayOutputStream baos = new ByteArrayOutputStream();
054            DataOutputStream dos = new DataOutputStream(baos);
055            marshal(command, dos);
056            dos.close();
057            return baos.toByteSequence();
058        }
059    
060        public Object unmarshal(ByteSequence packet) throws IOException {
061            ByteArrayInputStream stream = new ByteArrayInputStream(packet);
062            DataInputStream dis = new DataInputStream(stream);
063            return unmarshal(dis);
064        }
065    
066            public boolean inReceive() {
067                    // TODO Implement for inactivity monitor
068                    return false;
069            }
070        
071    }