001    /**
002     * Copyright (C) 2010, Skype Limited
003     *
004     * All intellectual property rights, including but not limited to copyrights,
005     * trademarks and patents, as well as know how and trade secrets contained in,
006     * relating to, or arising from the internet telephony software of
007     * Skype Limited (including its affiliates, "Skype"), including without
008     * limitation this source code, Skype API and related material of such
009     * software proprietary to Skype and/or its licensors ("IP Rights") are and
010     * shall remain the exclusive property of Skype and/or its licensors.
011     * The recipient hereby acknowledges and agrees that any unauthorized use of
012     * the IP Rights is a violation of intellectual property laws.
013     *
014     * Skype reserves all rights and may take legal action against infringers of
015     * IP Rights.
016     *
017     * The recipient agrees not to remove, obscure, make illegible or alter any
018     * notices or indications of the IP Rights and/or Skype's rights and
019     * ownership thereof.
020     */
021    
022    package com.skype.ipc;
023    
024    import java.io.IOException;
025    
026    import com.skype.util.Log;
027    
028    public class PropertyChange extends AbstractDecoder
029    {
030        private static final String TAG = "PropertyChange";
031        public int                  moid;
032        public int                  oid;
033        public int                  propid;
034        public int                  kind;
035        private Object              val;
036        private boolean             invalid;
037    
038        PropertyChange(Transport in) throws IOException
039        {
040            super(in);
041            // <modid><oid><typetag or N><propid><propvalueaccordingtotype or
042            // nothing if N>]]]z
043            invalid = false;
044            val = null;
045            moid = decodeVaruint();
046            oid = decodeVaruint();
047            kind = ioTransport.read();
048            propid = decodeVaruint();
049    
050            if (kind != 'N') {
051                val = decodeOneOfKind(kind);
052            }
053            
054            // check for known future protocol change
055            if (ioTransport.peek() == ',') {
056                invalid = true;
057                throw new IOException("Multiple property changes in single message are not supported (yet)!");
058            }
059            
060            if (ioTransport.read() != ']' ||
061                    ioTransport.read() != ']' ||
062                    ioTransport.read() != ']' ||
063                    ioTransport.read() != 'z')
064            {
065                Log.e(TAG, "Propertychange with unregognized extra bytes.");
066                invalid = true;
067            }
068            else {
069                return;
070            }
071        }
072    
073        private void CheckValidity() throws IOException
074        {
075            if (invalid) {
076                throw new IOException("Attempt to access malformed PropertyChange.");
077            }
078            
079            if (val == null) {
080                Log.e(TAG, "Attempt to read PropertyChange which has no value. Please add check for hasValue().");
081            }
082        }
083    
084        public boolean hasValue()
085        {
086            return (!invalid && val != null);
087        }
088    
089        public boolean isValid()
090        {
091            return (!invalid);
092        }
093    
094        public int GetKind()
095        {
096            return kind;
097        }
098    
099        public int GetOid()
100        {
101            return oid;
102        }
103    
104        public int GetPropid()
105        {
106            return propid;
107        }
108    
109        public int GetAsInt() throws IOException
110        {
111            CheckValidity();
112            return ((Number) val).intValue();
113        }
114    
115        public boolean GetAsBoolean() throws IOException
116        {
117            CheckValidity();
118            return (((Number) val).intValue() != 0);
119        }
120    
121        public String GetAsString() throws IOException
122        {
123            CheckValidity();
124            return (String) val;
125        }
126    
127        public byte[] GetAsBinary(int tag) throws IOException
128        {
129            CheckValidity();
130            return (byte[]) val;
131        }
132    
133    }