/*
(non-Javadoc)

@see org.restcomm.protocols.ss7.tcap.asn.Encodable#encode(org.mobicents.protocols .asn.AsnOutputStream)
178151
public void encode(AsnOutputStream aos) throws EncodeException {

        if (this.oid == null)
            throw new EncodeException("Error while decoding ApplicationContextName: No OID value set");

        try {
            aos.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, _TAG);
            int pos = aos.StartContentDefiniteLength();

            aos.writeObjectIdentifier(this.oid);

            aos.FinalizeContent(pos);

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding ApplicationContextName: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("IOException while encoding ApplicationContextName: " + e.getMessage(), e);
        }

    }
1327775
public final ByteArrayOutputStream encode() throws IOException
    {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        buffer.write(type);
        buffer.write(clientId);
        buffer.write(clientDestId);
        encode(buffer);

        return buffer;
    }
500699
public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)
        throws IOException {
        byte [] buf = getBytes(aBuffer);
        encodeBuffer(buf, aStream);
    }
482709
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.mlcNumber == null) {
            throw new MAPException("Encoding of " + _PrimitiveName
                    + " failed. Manadatory parameter mlcNumber [0] ISDN-AddressString is not set");
        }

        if (this.targetMS == null) {
            throw new MAPException("Encoding of " + _PrimitiveName
                    + " failed. Manadatory parameter targetMS [1] SubscriberIdentity is not set");
        }

        ((ISDNAddressStringImpl) this.mlcNumber).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_MLC_NUMBER);

        try {
            asnOs.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, _TAG_TARGET_MS);
            int pos = asnOs.StartContentDefiniteLength();
            ((SubscriberIdentityImpl) this.targetMS).encodeAll(asnOs);
            asnOs.FinalizeContent(pos);
        } catch (AsnException e) {
            throw new MAPException("AsnException while encoding parameter targetMS [1] SubscriberIdentity");
        }

        if (this.extensionContainer != null) {
            ((MAPExtensionContainerImpl) this.extensionContainer).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                    _TAG_EXTENSION_CONTAINER);
        }
    }
1343842
public void encodeData(AsnOutputStream asnOs) throws MAPException {

        if (this.ssCode == null)
            throw new MAPException("Error while encoding " + _PrimitiveName + ": ssCode required.");

        if (this.forwardingFeatureList == null)
            throw new MAPException("Error while encoding " + _PrimitiveName + ": forwardingFeatureList required.");

        if (this.forwardingFeatureList.size() < 1 || this.forwardingFeatureList.size() > 32) {
            throw new MAPException("Error while encoding " + _PrimitiveName
                    + ": Parameter forwardingFeatureList size must be from 1 to 32, found: "
                    + this.forwardingFeatureList.size());
        }

        try {

            ((SSCodeImpl) this.ssCode).encodeAll(asnOs);

            asnOs.writeTag(Tag.CLASS_UNIVERSAL, false, Tag.SEQUENCE);
            int pos = asnOs.StartContentDefiniteLength();
            for (ExtForwFeature featureItem : this.forwardingFeatureList) {
                ((ExtForwFeatureImpl) featureItem).encodeAll(asnOs);
            }
            asnOs.FinalizeContent(pos);

            if (this.extensionContainer != null)
                ((MAPExtensionContainerImpl) this.extensionContainer).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _TAG_extensionContainer);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
        }
    }
1107698
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.mSNetworkCapability == null)
            throw new MAPException("MSNetworkCapability cannot be null");

        ((MSNetworkCapabilityImpl) this.mSNetworkCapability).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                _ID_mSNetworkCapability);

        if (this.mSRadioAccessCapability != null)
            ((MSRadioAccessCapabilityImpl) this.mSRadioAccessCapability).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                    _ID_mSRadioAccessCapability);
    }
993235
public void encode(AsnOutputStream aos) throws EncodeException {

        if (acn == null)
            throw new EncodeException("Error encoding DialogResponseAPDU: Application Context Name must not be null");
        if (result == null)
            throw new EncodeException("Error encoding DialogResponseAPDU: Result must not be null");
        if (diagnostic == null)
            throw new EncodeException("Error encoding DialogResponseAPDU: Result-source-diagnostic must not be null");

        try {

            aos.writeTag(Tag.CLASS_APPLICATION, false, _TAG_RESPONSE);
            int pos = aos.StartContentDefiniteLength();

            if (!doNotSendProtocolVersion)
                this.protocolVersion.encode(aos);
            this.acn.encode(aos);
            this.result.encode(aos);
            this.diagnostic.encode(aos);

            if (ui != null)
                ui.encode(aos);

            aos.FinalizeContent(pos);

        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding DialogResponseAPDU: " + e.getMessage(), e);
        }

    }
318177
public void encode(AsnOutputStream aos) throws EncodeException {

        if (acn == null)
            throw new EncodeException("Error encoding DialogUniAPDU: Application Context Name must not be null");

        try {

            aos.writeTag(Tag.CLASS_APPLICATION, false, _TAG_UNIDIRECTIONAL);
            int pos = aos.StartContentDefiniteLength();

            // lets not omit protocol version, we check byte[] in tests, it
            // screws them :)
            if (!doNotSendProtocolVersion) {
                ProtocolVersion pv = TcapFactory.createProtocolVersion();
                pv.encode(aos);
            }
            this.acn.encode(aos);

            if (ui != null)
                ui.encode(aos);

            aos.FinalizeContent(pos);

        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding DialogUniAPDU: " + e.getMessage(), e);
        }
    }
1075059
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.routeingNumber != null)
            ((RouteingNumberImpl) this.routeingNumber).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_routeingNumber);

        if (this.imsi != null)
            ((IMSIImpl) this.imsi).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_imsi);

        if (this.msisdn != null)
            ((ISDNAddressStringImpl) this.msisdn).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_msisdn);

        if (this.numberPortabilityStatus != null) {
            try {
                asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _ID_numberPortabilityStatus,
                        this.numberPortabilityStatus.getType());
            } catch (IOException e) {
                throw new MAPException("IOException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
            } catch (AsnException e) {
                throw new MAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
            }
        }

        if (this.extensionContainer != null)
            ((MAPExtensionContainerImpl) this.extensionContainer).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                    _ID_extensionContainer);
    }
1048581
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.extensionContainer != null) {
            // extensionContainer ExtensionContainer OPTIONAL
            ((MAPExtensionContainerImpl) this.extensionContainer).encodeAll(asnOs);
        }

        if (this.naEsrk != null) {
            // na-ESRK [0] ISDN-AddressString OPTIONAL
            ((ISDNAddressStringImpl) this.naEsrk).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_NA_ESRK);
        }

        if (this.naEsrd != null) {
            // na-ESRD [1] ISDN-AddressString OPTIONAL ,
            ((ISDNAddressStringImpl) this.naEsrd).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_NA_ESRD);
        }
    }
269653
public void encodeAll(AsnOutputStream asnOs) throws MAPException {
        this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_BIT);
    }
158550
public void encode(AsnOutputStream aos) throws EncodeException {

        if (this.localOperationCode == null && this.globalOperationCode == null)
            throw new EncodeException("Operation code: No Operation code set!");

        try {
            if (this.type == OperationCodeType.Local) {
                aos.writeInteger(this.localOperationCode);
            } else if (this.type == OperationCodeType.Global) {
                aos.writeObjectIdentifier(this.globalOperationCode);
            } else {
                throw new EncodeException();
            }

        } catch (IOException e) {
            throw new EncodeException(e);
        } catch (AsnException e) {
            throw new EncodeException(e);
        }
    }
1367878
public void encode(AsnOutputStream aos) throws EncodeException {
        if (this.operationCode == null)
            throw new EncodeException("Error encoding Invoke: operationCode is mandatory but is not set");

        try {
            // tag
            if (this.notLast)
                aos.writeTag(Tag.CLASS_PRIVATE, false, Invoke._TAG_INVOKE_NOT_LAST);
            else
                aos.writeTag(Tag.CLASS_PRIVATE, false, Invoke._TAG_INVOKE_LAST);
            int pos = aos.StartContentDefiniteLength();

            // invokeId and correlationId
            byte[] buf;
            if (this.invokeId != null) {
                if (this.correlationId != null) {
                    buf = new byte[2];
                    buf[0] = (byte) (long) this.invokeId;
                    buf[1] = (byte) (long) this.correlationId;
                } else {
                    buf = new byte[1];
                    buf[0] = (byte) (long) this.invokeId;
                }
            } else {
                buf = new byte[0];
            }
            aos.writeOctetString(Tag.CLASS_PRIVATE, Component._TAG_INVOKE_ID, buf);

            // operationCode
            this.operationCode.encode(aos);

            // parameters
            if (this.parameter != null)
                this.parameter.encode(aos);
            else
                ParameterImpl.encodeEmptyParameter(aos);

            aos.FinalizeContent(pos);

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding Invoke: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding Invoke: " + e.getMessage(), e);
        }
    }
328653
public void encodeData(AsnOutputStream asnOs) throws MAPException {

        if (this.pdpType == null || this.qosSubscribed == null || this.apn == null) {
            throw new MAPException("pdpType, qosSubscribed and apn parameters must not be null");
        }

        try {
            asnOs.writeInteger((int) this.pdpContextId);

            ((PDPTypeImpl) this.pdpType).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_pdp_Type);

            if (this.pdpAddress != null)
                ((PDPAddressImpl) this.pdpAddress).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_pdp_Address);

            ((QoSSubscribedImpl) this.qosSubscribed).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_QoS_Subscribed);

            if (this.vplmnAddressAllowed)
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_vplmnAddressAllowed);

            ((APNImpl) this.apn).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_apn);

            if (this.extensionContainer != null)
                ((MAPExtensionContainerImpl) this.extensionContainer).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_extensionContainer);

            if (this.extQoSSubscribed != null)
                ((ExtQoSSubscribedImpl) this.extQoSSubscribed).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_ext_QoS_Subscribed);

            if (this.chargingCharacteristics != null)
                ((ChargingCharacteristicsImpl) this.chargingCharacteristics).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_pdp_ChargingCharacteristics);

            if (this.ext2QoSSubscribed != null)
                ((Ext2QoSSubscribedImpl) this.ext2QoSSubscribed).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_ext2_QoS_Subscribed);

            if (this.ext3QoSSubscribed != null)
                ((Ext3QoSSubscribedImpl) this.ext3QoSSubscribed).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_ext3_QoS_Subscribed);

            if (this.ext4QoSSubscribed != null)
                ((Ext4QoSSubscribedImpl) this.ext4QoSSubscribed).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_ext4_QoS_Subscribed);

            if (this.apnoiReplacement != null)
                ((APNOIReplacementImpl) this.apnoiReplacement).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                        _ID_apn_oi_Replacement);

            if (this.extpdpType != null)
                ((ExtPDPTypeImpl) this.extpdpType).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_ext_pdp_Type);

            if (this.extpdpAddress != null)
                ((PDPAddressImpl) this.extpdpAddress).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _ID_ext_pdp_Address);

            if (this.sipToPermission != null)
                asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _ID_sipto_Permission, this.sipToPermission.getCode());

            if (this.lipaPermission != null)
                asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _ID_lipa_Permission, this.lipaPermission.getCode());

        } catch (IOException e) {
            throw new MAPException("IOException when encoding PDPContext : " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding PDPContext : " + e.getMessage(), e);
        }
    }
1066868
public static byte[] encode(InputStream in) {
        Base64OutputStream out = null;
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            out = new Base64OutputStream(bytes);
            byte[] buf = new byte[4096];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.flush();
            return bytes.toByteArray();
        } catch (IOException e) {
            return null;
        } finally {
            try {
                in.close();
                if (out != null) out.close();
            } catch (IOException e) {
                throw new FaultException(e);
            }
        }
    }
758710
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.mSCNumber == null && this.sGSNNumber == null)
            throw new MAPException("Error when encoding " + _PrimitiveName + ": both mscNumber and sgsnNumber must not be null");
        if (this.mSCNumber != null && this.sGSNNumber != null)
            throw new MAPException("Error when encoding " + _PrimitiveName
                    + ": both mscNumber and sgsnNumber must not be not null");

        if (this.mSCNumber != null) {
            ((ISDNAddressStringImpl) this.mSCNumber).encodeData(asnOs);
        } else {
            ((ISDNAddressStringImpl) this.sGSNNumber).encodeData(asnOs);
        }
    }
1212507
public void encode(OutputStream out) throws IOException {
       DerOutputStream  tmp = new DerOutputStream();
        encode(out, PKIXExtensions.CRLNumber_Id, true);
    }