Package org.codehaus.activemq.message

Source Code of org.codehaus.activemq.message.AbstractPacketWriter

/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.message;

import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Set;
import org.codehaus.activemq.util.BitArray;

/**
* Allows instances implementing Packet interface to be serailized/deserailized
*/
public abstract class AbstractPacketWriter implements PacketWriter {
    /**
     * simple helper method to ensure null strings are catered for
     *
     * @param str
     * @param dataOut
     * @throws IOException
     */
    protected void writeUTF(String str, DataOutput dataOut) throws IOException {
        if (str == null) {
            str = "";
        }
        dataOut.writeUTF(str);
    }

    /**
     * @param packet
     * @return true if this PacketWriter can write this type of Packet
     */
    public boolean canWrite(Packet packet) {
        return packet.getPacketType() == this.getPacketType();
    }

    /**
     * Simple (but inefficent) utility method to write an object on to a stream
     *
     * @param object
     * @param dataOut
     * @throws IOException
     */
    protected void writeObject(Object object, DataOutput dataOut) throws IOException {
        if (object != null) {
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
            ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
            objOut.writeObject(object);
            objOut.flush();
            byte[] data = bytesOut.toByteArray();
            dataOut.writeInt(data.length);
            dataOut.write(data);
        }
        else {
            dataOut.writeInt(0);
        }
    }

    /**
     * Serializes a Packet int a byte array
     *
     * @param packet
     * @return the byte[]
     * @throws IOException
     */
    public byte[] writePacketToByteArray(Packet packet) throws IOException {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(bytesOut);
        writePacket(packet, dataOut);
        dataOut.flush();
        return bytesOut.toByteArray();
    }

    /**
     * Write a Packet instance to data output stream
     *
     * @param p  the instance to be seralized
     * @param dataOut the output stream
     * @throws IOException thrown if an error occurs
     */
    public void writePacket(Packet p, DataOutput dataOut) throws IOException {
        AbstractPacket packet = (AbstractPacket)p;
        writeUTF(packet.getId(), dataOut);
        BitArray ba = packet.getBitArray();
        ba.set(AbstractPacket.RECEIPT_REQUIRED_INDEX, packet.isReceiptRequired());
        Object[] visited = packet.getBrokersVisited();
        boolean writeVisited = visited != null && visited.length > 0;
        ba.set(AbstractPacket.BROKERS_VISITED_INDEX,writeVisited);
        ba.writeToStream(dataOut);
        if (writeVisited){
            dataOut.writeShort(visited.length);
            for(int i =0; i < visited.length; i++){
                dataOut.writeUTF(visited[i].toString());
            }
        }
    }
}
TOP

Related Classes of org.codehaus.activemq.message.AbstractPacketWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.