com.mobius.software.telco.protocols.diameter.DiameterAnswerData Maven / Gradle / Ivy
package com.mobius.software.telco.protocols.diameter;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
/*
* Mobius Software LTD
* Copyright 2023, Mobius Software LTD and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see
*/
/**
*
* @author yulian oifa
*
*/
public class DiameterAnswerData implements Externalizable
{
private ByteBuf buffer;
private Long initialTimestamp;
public DiameterAnswerData()
{
}
public DiameterAnswerData(Long initialTimestamp)
{
this.initialTimestamp = initialTimestamp;
}
public ByteBuf getBuffer()
{
if(buffer==null)
return null;
return Unpooled.wrappedBuffer(buffer);
}
public void setBuffer(ByteBuf buffer)
{
this.buffer = buffer;
}
public Long getInitialTimestamp()
{
return initialTimestamp;
}
public void setInitialTimestamp(Long initialTimestamp)
{
this.initialTimestamp = initialTimestamp;
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
Boolean hasAnswer = in.readBoolean();
if(hasAnswer)
{
int length = in.readInt();
byte[] data = new byte[length];
in.read(data);
buffer = Unpooled.wrappedBuffer(data);
buffer.readerIndex(0);
}
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
if(buffer!=null)
{
out.writeBoolean(true);
out.writeInt(buffer.readableBytes());
buffer.markReaderIndex();
try
{
byte[] data = new byte[buffer.readableBytes()];
buffer.readBytes(data);
out.write(data);
}
finally
{
buffer.resetReaderIndex();
}
}
else
out.writeBoolean(false);
}
}