/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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.activemq.transport.openwire;
import org.activemq.io.AbstractWireFormat;
import org.activemq.io.WireFormat;
import org.activemq.io.impl.AbstractDefaultWireFormat;
import org.activemq.io.impl.AbstractPacketMarshaller;
import org.activemq.message.Packet;
import org.activemq.transport.openwire.io.OpenWireFormat;
import javax.jms.JMSException;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* Base class for the code generated {@link OpenWireFormat}
*
* @version $Revision: 1.1 $
*/
public abstract class OpenWireFormatSupport extends AbstractWireFormat {
public Packet readPacket(DataInput dataIn) throws IOException {
int type = -1;
while ((type = dataIn.readByte()) == 0);
if (type == -1){
throw new IOException("InputStream now closed");
}
return readPacket(type, dataIn);
}
public boolean canProcessWireFormatVersion(int version) {
return version == getCurrentWireFormatVersion();
}
public WireFormat copy() {
return new OpenWireFormat();
}
public int getCurrentWireFormatVersion() {
return AbstractDefaultWireFormat.WIRE_FORMAT_VERSION;
}
public Packet writePacket(Packet packet, DataOutput out) throws IOException, JMSException {
AbstractPacketMarshaller marshaller = getMarshaller(packet);
if (marshaller == null) {
throw new JMSException("No marshaller for packet type: " + packet.getPacketType() + " value: " + packet);
}
out.writeByte(marshaller.getPacketType());
marshaller.writePacket(packet, out);
return packet;
}
protected Packet readPacket(DataInput dataIn, AbstractPacketMarshaller marshaler) throws IOException {
Packet packet = marshaler.createPacket();
marshaler.buildPacket(packet, dataIn);
return packet;
}
protected abstract AbstractPacketMarshaller getMarshaller(Packet packet);
}