/*
(non-Javadoc)

@see org.restcomm.protocols.ss7.map.primitives.MAPAsnPrimitive#encodeData (org.mobicents.protocols.asn.AsnOutputStream)
494778
public void encodeData(AsnOutputStream asnOs) throws MAPException {

        int cnt = 0;
        if (this.forwardingInfo != null)
            cnt++;
        if (this.callBarringInfo != null)
            cnt++;
        if (this.cugInfo != null)
            cnt++;
        if (this.ssData != null)
            cnt++;
        if (this.emlppInfo != null)
            cnt++;

        if (cnt != 1)
            throw new MAPException("Error while encoding " + _PrimitiveName + ": one and only one choice is required.");

        if (this.forwardingInfo != null) {
            ((ExtForwInfoImpl) this.forwardingInfo).encodeData(asnOs);
            return;
        }

        if (this.callBarringInfo != null) {
            ((ExtCallBarInfoImpl) this.callBarringInfo).encodeData(asnOs);
            return;
        }

        if (this.cugInfo != null) {
            ((CUGInfoImpl) this.cugInfo).encodeData(asnOs);
            return;
        }

        if (this.ssData != null) {
            ((ExtSSDataImpl) this.ssData).encodeData(asnOs);
            return;
        }

        if (this.emlppInfo != null) {
            ((EMLPPInfoImpl) this.emlppInfo).encodeData(asnOs);
            return;
        }
    }
841447
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.responseTimeCategory == null) {
            throw new MAPException("Error while encoding " + _PrimitiveName
                    + " the mandatory parameter responseTimeCategory is not defined");
        }
        try {
            asnOs.writeInteger(Tag.CLASS_UNIVERSAL, Tag.ENUMERATED, this.responseTimeCategory.getCategory());
        } catch (IOException e) {
            throw new MAPException("IOException when encoding responseTimeCategory: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding responseTimeCategory: ", e);
        }
    }
95030
func (typedData *TypedData) EncodeData(primaryType string, data map[string]interface{}, depth int) (hexutil.Bytes, error) {
	if err := typedData.validate(); err != nil {
		return nil, err
	}

	buffer := bytes.Buffer{}

	// Verify extra data
	if len(typedData.Types[primaryType]) < len(data) {
		return nil, errors.New("there is extra data provided in the message")
	}

	// Add typehash
	buffer.Write(typedData.TypeHash(primaryType))

	// Add field contents. Structs and arrays have special handlers.
	for _, field := range typedData.Types[primaryType] {
		encType := field.Type
		encValue := data[field.Name]
		if encType[len(encType)-1:] == "]" {
			arrayValue, ok := encValue.([]interface{})
			if !ok {
				return nil, dataMismatchError(encType, encValue)
			}

			arrayBuffer := bytes.Buffer{}
			parsedType := strings.Split(encType, "[")[0]
			for _, item := range arrayValue {
				if typedData.Types[parsedType] != nil {
					mapValue, ok := item.(map[string]interface{})
					if !ok {
						return nil, dataMismatchError(parsedType, item)
					}
					encodedData, err := typedData.EncodeData(parsedType, mapValue, depth+1)
					if err != nil {
						return nil, err
					}
					arrayBuffer.Write(encodedData)
				} else {
					bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth)
					if err != nil {
						return nil, err
					}
					arrayBuffer.Write(bytesValue)
				}
			}

			buffer.Write(crypto.Keccak256(arrayBuffer.Bytes()))
		} else if typedData.Types[field.Type] != nil {
			mapValue, ok := encValue.(map[string]interface{})
			if !ok {
				return nil, dataMismatchError(encType, encValue)
			}
			encodedData, err := typedData.EncodeData(field.Type, mapValue, depth+1)
			if err != nil {
				return nil, err
			}
			buffer.Write(crypto.Keccak256(encodedData))
		} else {
			byteValue, err := typedData.EncodePrimitiveValue(encType, encValue, depth)
			if err != nil {
				return nil, err
			}
			buffer.Write(byteValue)
		}
	}
	return buffer.Bytes(), nil
}
293238
public static StringBuffer encode(byte[] data)
	{
		int rem = data.length % 3;
		int num = data.length / 3;
		StringBuffer result = new StringBuffer();
		int p = 0;
		// int c=0;

		for (int i = 0; i < num; i++)
		{
			int b1 = (data[p] & 0xfc) >> 2;
			int b2 = (data[p] & 0x03) << 4 | (data[p + 1] & 0xf0) >> 4;
			int b3 = (data[p + 1] & 0x0f) << 2 | (data[p + 2] & 0xc0) >> 6;
			int b4 = data[p + 2] & 0x3f;

			result.append(b64encode(b1));
			result.append(b64encode(b2));
			result.append(b64encode(b3));
			result.append(b64encode(b4));

			p += 3;
			// c += 4;
		}

		switch (rem)
		{
			case 0:
				break;

			case 1:
			{
				int b1 = (data[p] & 0xfc) >> 2;
				int b2 = (data[p] & 0x03) << 4;

				result.append(b64encode(b1));
				result.append(b64encode(b2));
				result.append('=');
				result.append('=');
				break;
			}

			case 2:
			{
				int b1 = (data[p] & 0xfc) >> 2;
				int b2 = (data[p] & 0x03) << 4 | (data[p + 1] & 0xf0) >> 4;
				int b3 = (data[p + 1] & 0x0f) << 2;

				result.append(b64encode(b1));
				result.append(b64encode(b2));
				result.append(b64encode(b3));
				result.append('=');
				break;
			}
		}

		return result;
	}
371103
func (enc *HTTPEncoder) EncodeData(data []byte) error {
	setContentType(enc.writer, ContentBinary)

	_, err := enc.writer.Write(data)
	if err != nil {
		http.Error(enc.writer, fmt.Sprintf("Unable to encode binary data: %s", err.Error()), http.StatusInternalServerError)
	}
	return err
}
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);
        }
    }
787854
func encodeData(o interface{}) (io.Reader, int, error) {
	if o == nil {
		return nil, 0, nil
	}
	buf, err := json.Marshal(&o)
	if err != nil {
		return nil, 0, err
	} else {
		return bytes.NewReader(buf), len(buf), nil
	}
}
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);
        }
    }
982383
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.deferredLocationEventType == null) {
            throw new MAPException("Encdoing of " + _PrimitiveName
                    + " failed. Missing mandatory parameter deferredLocationEventType DeferredLocationEventType");
        }

        ((DeferredLocationEventTypeImpl) this.deferredLocationEventType).encodeAll(asnOs);

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

        if (this.lcsLocationInfo != null) {
            ((LCSLocationInfoImpl) this.lcsLocationInfo).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_LCS_LOCATION_INFO);
        }
    }
297892
@Override
	public ByteArray encodeAsByteArray() {
		if(message.isEmpty()) return ByteArray.EMPTY_BYTE_ARRAY;

		return new ByteArray(message.getBytes(CHARSET));
	}
1363113
public static String encode(byte[] rawData) {
        StringBuilder buffer = new StringBuilder(4 * rawData.length / 3);

        int pos = 0;
        int iterations = rawData.length / 3;
        for (int i = 0; i < iterations; i++) {
            int value = ((rawData[pos++] & 0xFF) << 16) |
                    ((rawData[pos++] & 0xFF) << 8) | (rawData[pos++] & 0xFF);

            buffer.append(BASE64_ALPHABET[(value >>> 18) & 0x3F]);
            buffer.append(BASE64_ALPHABET[(value >>> 12) & 0x3F]);
            buffer.append(BASE64_ALPHABET[(value >>> 6) & 0x3F]);
            buffer.append(BASE64_ALPHABET[value & 0x3F]);
        }

        switch (rawData.length % 3) {
            case 1:
                buffer.append(BASE64_ALPHABET[(rawData[pos] >>> 2) & 0x3F]);
                buffer.append(BASE64_ALPHABET[(rawData[pos] << 4) & 0x3F]);
                buffer.append("==");
                break;
            case 2:
                int value = ((rawData[pos++] & 0xFF) << 8) | (rawData[pos] & 0xFF);
                buffer.append(BASE64_ALPHABET[(value >>> 10) & 0x3F]);
                buffer.append(BASE64_ALPHABET[(value >>> 4) & 0x3F]);
                buffer.append(BASE64_ALPHABET[(value << 2) & 0x3F]);
                buffer.append("=");
                break;
        }

        return buffer.toString();
    }
751040
public static Map<String, Object> parseData(String data) {
		Map<String, Object> ret = new HashMap<String, Object>();
		String[] split = data.split("&");
		for (String s : split) {
			int idx = s.indexOf('=');
			try {
				if (idx != -1) {
					ret.put(URLDecoder.decode(s.substring(0, idx), "UTF-8"), URLDecoder.decode(s.substring(idx + 1), "UTF-8"));
				} else {
					ret.put(URLDecoder.decode(s, "UTF-8"), "true");
				}
			} catch (UnsupportedEncodingException e) {
				// Why.
			}
		}
		return ret;
	}
269128
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        try {
            if (this.locationInformation) {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_locationInformation);
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter locationInformation: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter locationInformation: ", e);
        }

        try {
            if (this.subscriberState) {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_subscriberState);
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter subscriberState: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter subscriberState: ", e);
        }

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

        try {
            if (this.currentLocation) {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_currentLocation);
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter currentLocation: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter currentLocation: ", e);
        }

        try {
            if (this.requestedDomain != null) {
                asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _ID_requestedDomain, this.requestedDomain.getType());
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter requestedDomain: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter requestedDomain: ", e);
        }

        try {
            if (this.imei) {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_imei);
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter imei: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter imei: ", e);
        }

        try {
            if (this.msClassmark) {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_msclassmark);
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter msClassmark: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter msClassmark: ", e);
        }

        try {
            if (this.mnpRequestedInfo) {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _ID_mnpRequestedInfo);
            }
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter mnpRequestedInfo: ", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter mnpRequestedInfo: ", 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);
    }
587962
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.callSessionUnrelated == null) {
            throw new MAPException(
                    "Error while encoding LCSPrivacyCheck the mandatory parameter callSessionUnrelated is not defined");
        }

        try {
            asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_CALL_SESSION_UNRELATED, this.callSessionUnrelated.getAction());
        } catch (IOException e) {
            throw new MAPException("IOException when encoding parameter LCSPrivacyCheck.callSessionUnrelated", e);
        } catch (AsnException e) {
            throw new MAPException("AsnException when encoding parameter LCSPrivacyCheck.callSessionUnrelated", e);
        }

        if (this.callSessionRelated != null) {
            try {
                asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_CALL_SESSION_RELATED, this.callSessionRelated.getAction());
            } catch (IOException e) {
                throw new MAPException("IOException when encoding parameter LCSPrivacyCheck.callSessionRelated", e);
            } catch (AsnException e) {
                throw new MAPException("AsnException when encoding parameter LCSPrivacyCheck.callSessionRelated", e);
            }
        }
    }
794518
public void encodeData(AsnOutputStream asnOs) throws MAPException {
        if (this.horizontalAccuracy != null) {
            try {
                asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_HORIZONTAL_ACCURACY,
                        new byte[] { this.horizontalAccuracy.byteValue() });
            } catch (IOException e) {
                throw new MAPException("IOException when encoding parameter horizontalAccuracy: ", e);
            } catch (AsnException e) {
                throw new MAPException("AsnException when encoding parameter horizontalAccuracy: ", e);
            }
        }

        if (this.verticalCoordinateRequest) {
            try {
                asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_VERTICAL_COORDINATE_REQUEST);
            } catch (IOException e) {
                throw new MAPException("IOException when encoding parameter verticalCoordinateRequest: ", e);
            } catch (AsnException e) {
                throw new MAPException("IOException when encoding parameter verticalCoordinateRequest: ", e);
            }
        }

        if (this.verticalAccuracy != null) {
            try {
                asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_VERTICAL_ACCURACY,
                        new byte[] { this.verticalAccuracy.byteValue() });
            } catch (IOException e) {
                throw new MAPException("IOException when encoding parameter verticalAccuracy: ", e);
            } catch (AsnException e) {
                throw new MAPException("AsnException when encoding parameter verticalAccuracy: ", e);
            }
        }

        if (this.responseTime != null) {
            ((ResponseTimeImpl) this.responseTime).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_RESPONSE_TIME);
        }

        if (this.extensionContainer != null) {
            ((MAPExtensionContainerImpl) this.extensionContainer).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC,
                    _TAG_EXTENSION_CONTAINER);
        }
    }
953268
@Override
    public String encode(final String data, final Optional<RegisteredService> service) {
        try {
            if (service.isPresent()) {
                val registeredService = service.get();
                val publicKey = createRegisteredServicePublicKey(registeredService);
                val result = encodeInternal(data, publicKey, registeredService);
                if (result != null) {
                    return EncodingUtils.encodeBase64(result);
                }
            }
        } catch (final Exception e) {
            LOGGER.warn(e.getMessage(), e);
        }
        return null;
    }