/*
(non-Javadoc)

@see org.mobicents.protocols.asn.External#decode(org.mobicents.protocols.asn.AsnInputStream)
777340
public void decode(AsnInputStream ais) throws ParseException {

        try {
            ext.decode(ais);
        } catch (AsnException e) {
            throw new ParseException(PAbortCause.BadlyStructuredDialoguePortion, "AsnException while decoding UserInformationElement: " + e.getMessage(), e);
        }
    }
441034
public static String decode(final String encoded, final String charset) 
        throws UnsupportedEncodingException {
        try {
            return s_codec.decode(encoded, charset);
        } catch (DecoderException e) {
            throw new IllegalStateException(Bundle.getErrorString("URLCodec_decodeException", new String[] {e.getMessage()}), e);
        }
    }
167355
private String decode(byte[] data, String encoding) {

        try {
            return new String(data, encoding);
        } catch (UnsupportedEncodingException e) {
            LOG.warn(e.getLocalizedMessage(), e);
            return new String(data);
        }
    }
231904
@Override
    public int decode ( byte[] buffer, int bufferIndex, int len ) throws SMBProtocolDecodingException {
        int start = bufferIndex;
        int nciphers = SMBUtil.readInt2(buffer, bufferIndex);
        bufferIndex += 2;

        this.ciphers = new int[nciphers];
        for ( int i = 0; i < nciphers; i++ ) {
            this.ciphers[ i ] = SMBUtil.readInt2(buffer, bufferIndex);
            bufferIndex += 2;
        }

        return bufferIndex - start;
    }
167154
@Override
    public int decode ( byte[] buffer, int bufferIndex, int len ) throws SMBProtocolDecodingException {
        int start = bufferIndex;
        this.replaceIfExists = buffer[ bufferIndex ] != 0;
        bufferIndex += 8;
        bufferIndex += 8;

        int nameLen = SMBUtil.readInt4(buffer, bufferIndex);
        bufferIndex += 4;
        byte[] nameBytes = new byte[nameLen];
        System.arraycopy(buffer, bufferIndex, nameBytes, 0, nameBytes.length);
        bufferIndex += nameLen;
        this.fileName = new String(nameBytes, StandardCharsets.UTF_16LE);
        return bufferIndex - start;
    }
677103
public <T> T decode(String source, Class<? extends T> cls) {

        return net.arnx.jsonic.JSON.decode(source, cls);
    }
84161
@Override
    public int decode ( byte[] buffer, int bufferIndex, int len ) throws SMBProtocolDecodingException {
        int start = bufferIndex;
        this.nextEntryOffset = SMBUtil.readInt4(buffer, bufferIndex);
        bufferIndex += 4;
        this.fileIndex = SMBUtil.readInt4(buffer, bufferIndex);
        bufferIndex += 4;
        this.creationTime = SMBUtil.readTime(buffer, bufferIndex);
        bufferIndex += 8;
        this.lastAccessTime = SMBUtil.readTime(buffer, bufferIndex);
        bufferIndex += 8;
        this.lastWriteTime = SMBUtil.readTime(buffer, bufferIndex);
        bufferIndex += 8;
        this.changeTime = SMBUtil.readTime(buffer, bufferIndex);
        bufferIndex += 8;
        this.endOfFile = SMBUtil.readInt8(buffer, bufferIndex);
        bufferIndex += 8;
        this.allocationSize = SMBUtil.readInt8(buffer, bufferIndex);
        bufferIndex += 8;
        this.extFileAttributes = SMBUtil.readInt4(buffer, bufferIndex);
        bufferIndex += 4;
        int fileNameLength = SMBUtil.readInt4(buffer, bufferIndex);
        bufferIndex += 4;
        this.eaSize = SMBUtil.readInt4(buffer, bufferIndex);
        bufferIndex += 4;

        int shortNameLength = buffer[ bufferIndex ] & 0xFF;
        bufferIndex += 2;

        this.shortName = Strings.fromUNIBytes(buffer, bufferIndex, shortNameLength);
        bufferIndex += 24;

        String str;
        if ( this.unicode ) {
            if ( fileNameLength > 0 && buffer[ bufferIndex + fileNameLength - 1 ] == '\0' && buffer[ bufferIndex + fileNameLength - 2 ] == '\0' ) {
                fileNameLength -= 2;
            }
            str = Strings.fromUNIBytes(buffer, bufferIndex, fileNameLength);
        }
        else {
            if ( fileNameLength > 0 && buffer[ bufferIndex + fileNameLength - 1 ] == '\0' ) {
                fileNameLength -= 1;
            }
            str = Strings.fromOEMBytes(buffer, bufferIndex, fileNameLength, this.config);
        }
        this.filename = str;
        bufferIndex += fileNameLength;

        return start - bufferIndex;
    }
1200832
public void decode(AsnInputStream ais) throws ParseException {

        this.correlationId = null;
        this.errorCode = null;
        this.parameter = null;

        try {
            AsnInputStream localAis = ais.readSequenceStream();

            // correlationId
            byte[] buf = TcapFactory.readComponentId(localAis, 1, 1);
            this.setCorrelationId((long) buf[0]);

            // errorCode
            int tag = localAis.readTag();
            if ((tag != ErrorCode._TAG_NATIONAL && tag != ErrorCode._TAG_PRIVATE) || localAis.getTagClass() != Tag.CLASS_PRIVATE
                    || !localAis.isTagPrimitive()) {
                throw new ParseException(RejectProblem.generalIncorrectComponentPortion,
                        "ErrorCode in ReturnError has bad tag or tag class or is not primitive: tag=" + tag + ", tagClass=" + localAis.getTagClass());
            }
            this.errorCode = TcapFactory.createErrorCode(localAis);

            // Parameter
            this.parameter = TcapFactory.readParameter(localAis);

        } catch (IOException e) {
            throw new ParseException(RejectProblem.generalBadlyStructuredCompPortion, "IOException while decoding ReturnError: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new ParseException(RejectProblem.generalBadlyStructuredCompPortion, "AsnException while decoding ReturnError: " + e.getMessage(), e);
        } catch (ParseException e) {
            e.setInvokeId(this.correlationId);
            throw e;
        }
    }
544135
private static void decode(InputStream in, StringBuilder result) throws ApiDataException, ApiConnectionException {
        try {
            int len = readLen(in);
            if (len > 0) {
                byte buf[] = new byte[len];
                for (int i = 0; i < len; ++i) {
                    int c = in.read();
                    if (c < 0) {
                        throw new ApiDataException("Truncated data. Expected to read more bytes");
                    }
                    buf[i] = (byte) (c & 0xFF);
                }
                String res = new String(buf, Charset.forName("UTF-8"));
                if (result.length() > 0) {
                    result.append("\n");
                }
                result.append(res);
                decode(in, result);
            }
        } catch (IOException ex) {
            throw new ApiConnectionException(ex.getMessage(), ex);
        }
    }
794450
public static void decode(CharSequence in, Appendable out) throws IOException{
		decode(in, UTF_8, out);
	}
496992
public ServiceMessage decode(ByteBuf dataBuffer, ByteBuf headersBuffer)
      throws MessageCodecException {
    ServiceMessage.Builder builder = ServiceMessage.builder();

    if (dataBuffer.isReadable()) {
      builder.data(dataBuffer);
    }
    if (headersBuffer.isReadable()) {
      try (ByteBufInputStream stream = new ByteBufInputStream(headersBuffer, true)) {
        builder.headers(headersCodec.decode(stream));
      } catch (Throwable ex) {
        ReferenceCountUtil.safestRelease(dataBuffer); // release data buf as well
        throw new MessageCodecException("Failed to decode message headers", ex);
      }
    }

    return builder.build();
  }
1211129
public void decode(AsnInputStream ais) throws ParseException {
        this.destinationTransactionId = null;
        this.dp = null;
        this.pAbortCause = null;
        this.userAbortInformation = null;

        try {
            AsnInputStream localAis = ais.readSequenceStream();

            // transaction portion
            TransactionID tid = TcapFactory.readTransactionID(localAis);
            if (tid.getFirstElem() == null || tid.getSecondElem() != null) {
                throw new ParseException(PAbortCause.BadlyStructuredTransactionPortion,
                        "Error decoding TCAbortMessage: transactionId must contain one and only one transactionId");
            }
            this.destinationTransactionId = tid.getFirstElem();

            if (localAis.available() == 0) {
                throw new ParseException(PAbortCause.UnrecognizedDialoguePortionID,
                        "Error decoding TCAbortMessage: neither P-Abort-cause nor dialog portion or userInformation is found");
            }
            int tag = localAis.readTag();
            if (localAis.getTagClass() != Tag.CLASS_PRIVATE)
                throw new ParseException(PAbortCause.UnrecognizedDialoguePortionID,
                        "Error decoding TCAbortMessage: bad tagClass for P-Abort-cause, userInformation or abortCause, found tagClass=" + localAis.getTagClass());

            switch (tag) {
            case TCAbortMessage._TAG_P_ABORT_CAUSE:
                // P-Abort-cause
                if (!localAis.isTagPrimitive())
                    throw new ParseException(PAbortCause.IncorrectTransactionPortion, "Error decoding TCAbortMessage: P_ABORT_CAUSE is not primitive");
                int i1 = (int)localAis.readInteger();
                this.pAbortCause = PAbortCause.getFromInt(i1);
                break;

            case DialogPortion._TAG_DIALOG_PORTION:
            case TCAbortMessage._TAG_USER_ABORT_INFORMATION:
                // Dialog portion (opt) + UserAbortInformation
                if (tag == DialogPortion._TAG_DIALOG_PORTION) {
                    // Dialog portion
                    this.dp = TcapFactory.createDialogPortion(localAis);
                    if (localAis.available() == 0)
                        return;
                    tag = localAis.readTag();
                }

                // UserAbortInformation
                if (tag != TCAbortMessage._TAG_USER_ABORT_INFORMATION || localAis.getTagClass() != Tag.CLASS_PRIVATE || localAis.isTagPrimitive())
                    throw new ParseException(PAbortCause.IncorrectTransactionPortion,
                            "Error decoding TCAbortMessage: bad tag or tagClass or is primitive for userAbortInformation, found tagClass="
                                    + localAis.getTagClass() + ", tag" + tag);
                UserInformationElementImpl uai = new UserInformationElementImpl();
                AsnInputStream localAis2 = localAis.readSequenceStream();
                if (localAis2.available() > 0) {
                    tag = localAis2.readTag();
                    if (tag != Tag.EXTERNAL || localAis2.getTagClass() != Tag.CLASS_UNIVERSAL || localAis2.isTagPrimitive())
                        throw new ParseException(PAbortCause.IncorrectTransactionPortion,
                                "Error decoding TCAbortMessage: bad tag or tagClass or is primitive for userAbortInformation - External, found tagClass="
                                        + localAis2.getTagClass() + ", tag" + tag);
                    uai.decode(localAis2);
                    if (uai.isOid())
                        this.userAbortInformation = uai;
                }
                break;

            default:
                throw new ParseException(PAbortCause.IncorrectTransactionPortion,
                        "Error decoding TCAbortMessage: bad tag for P-Abort-cause, userInformation or abortCause, found tag=" + tag);
            }

        } catch (IOException e) {
            throw new ParseException(PAbortCause.BadlyStructuredDialoguePortion, "IOException while decoding TCAbortMessage: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new ParseException(PAbortCause.BadlyStructuredDialoguePortion, "AsnException while decoding TCAbortMessage: " + e.getMessage(), e);
        }
    }
478915
public void decode(AsnInputStream ais) throws ParseException {

        try {
            this.setLocalOriginated(false);

            AsnInputStream localAis = ais.readSequenceStream();

            int tag = localAis.readTag();
            if (localAis.getTagClass() != Tag.CLASS_UNIVERSAL) {
                throw new ParseException(null, GeneralProblemType.MistypedComponent,
                        "Error while decoding Reject: bad tag class for InvokeID or NULL: tagClass = " + localAis.getTagClass());
            }
            switch (tag) {
                case _TAG_IID:
                    this.invokeId = localAis.readInteger();
                    break;
                case Tag.NULL:
                    localAis.readNull();
                    break;
            }

            tag = localAis.readTag();
            if (localAis.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC) {
                throw new ParseException(null, GeneralProblemType.MistypedComponent,
                        "Error while decoding Reject: bad tag class for a problem: tagClass = " + localAis.getTagClass());
            }
            ProblemType pt = ProblemType.getFromInt(tag);
            if (pt == null) {
                throw new ParseException(null, GeneralProblemType.MistypedComponent,
                        "Error while decoding Reject: ProblemType not found");
            }
            this.problem = TcapFactory.createProblem(pt, localAis);

        } catch (IOException e) {
            throw new ParseException(null, GeneralProblemType.BadlyStructuredComponent, "IOException while decoding Reject: "
                    + e.getMessage(), e);
        } catch (AsnException e) {
            throw new ParseException(null, GeneralProblemType.BadlyStructuredComponent, "AsnException while decoding Reject: "
                    + e.getMessage(), e);
        }

    }
659530
function decode(s, tag) {
	if (!decodeTag(s, tag)) {
		throw new error.ProtocolError('NODE_RDP_ASN1_BER_INVALID_TAG');
	}
	var length = decodeLength(s);
	
	if (length === 0) {
		return new type.Stream(0);
	}
	return new type.BinaryString(null,{ readLength : new type.CallableValue(length) }).read(s);
}
868347
public void decode(AsnInputStream ais) throws ParseException {

        try {
            long val = ais.readInteger();
            if (ais.getTag() == ErrorCode._TAG_NATIONAL) {
                this.setNationalErrorCode(val);
            } else {
                this.setPrivateErrorCode(val);
            }
        } catch (IOException e) {
            throw new ParseException(RejectProblem.generalBadlyStructuredCompPortion, "IOException while decoding ErrorCode: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new ParseException(RejectProblem.generalBadlyStructuredCompPortion, "AsnException while decoding ErrorCode: " + e.getMessage(), e);
        }
    }
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;
        }
    }
552438
public void decode(AsnInputStream ais) throws ParseException {
        try {
            AsnInputStream localAis = ais.readSequenceStream();

            int tag = localAis.readTag();
            if (tag != AbortSource._TAG || localAis.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC)
                throw new ParseException(PAbortCauseType.IncorrectTxPortion, null,
                        "Error decoding DialogAbortAPDU.abort-source: bad tag or tagClass, found tag=" + tag + ", tagClass="
                                + localAis.getTagClass());
            this.abortSource = TcapFactory.createAbortSource(localAis);

            // optional sequence.
            if (localAis.available() == 0)
                return;

            tag = localAis.readTag();
            if (tag != UserInformation._TAG || localAis.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC)
                throw new ParseException(PAbortCauseType.IncorrectTxPortion, null,
                        "Error decoding DialogAbortAPDU.user-information: bad tag or tagClass, found tag=" + tag
                                + ", tagClass=" + localAis.getTagClass());
            this.userInformation = TcapFactory.createUserInformation(localAis);

        } catch (IOException e) {
            throw new ParseException(PAbortCauseType.BadlyFormattedTxPortion, null,
                    "IOException while decoding DialogAbortAPDU: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new ParseException(PAbortCauseType.BadlyFormattedTxPortion, null,
                    "AsnException while decoding DialogAbortAPDU: " + e.getMessage(), e);
        }

    }