Exemple #1
0
        public string ReadString()
        {
            var length        = SerDe.Convert(stream.Reader.ReadInt32());
            var stringAsBytes = stream.Reader.ReadBytes(length);

            return(SerDe.ToString(stringAsBytes));
        }
Exemple #2
0
        private object ReadJvmObjectReferenceCollection(NetworkStream s)
        {
            object returnValue;
            var    listItemTypeAsChar = Convert.ToChar(s.ReadByte());

            switch (listItemTypeAsChar)
            {
            case 'j':
                var jvmObjectReferenceList = new List <JvmObjectReference>();
                var numOfItemsInList       = SerDe.ReadInt(s);
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    var itemIdentifier = SerDe.ReadString(s);
                    jvmObjectReferenceList.Add(new JvmObjectReference(itemIdentifier));
                }
                returnValue = jvmObjectReferenceList;
                break;

            default:
                // convert listItemTypeAsChar to UInt32 because the char may be non-printable
                throw new NotSupportedException(
                          string.Format("Identifier for list item type 0x{0:X} not supported",
                                        Convert.ToUInt32(listItemTypeAsChar)));
            }
            return(returnValue);
        }
Exemple #3
0
        internal static byte[] ToPayloadBytes(string value)
        {
            var inputAsBytes = SerDe.ToBytes(value);
            var byteRepresentationofInputLength = SerDe.ToBytes(inputAsBytes.Length);
            var sendPayloadBytes = new byte[byteRepresentationofInputLength.Length + inputAsBytes.Length];

            Array.Copy(byteRepresentationofInputLength, 0, sendPayloadBytes, 0, byteRepresentationofInputLength.Length);
            Array.Copy(inputAsBytes, 0, sendPayloadBytes, byteRepresentationofInputLength.Length, inputAsBytes.Length);

            return(sendPayloadBytes);
        }
Exemple #4
0
        private object CallJavaMethod(bool isStatic, object classNameOrJvmObjectReference, string methodName, params object[] parameters)
        {
            object returnValue = null;

            try
            {
                var overallPayload = PayloadHelper.BuildPayload(isStatic, classNameOrJvmObjectReference, methodName, parameters);

                Socket socket = GetConnection();
                using (NetworkStream s = new NetworkStream(socket))
                {
                    SerDe.Write(s, overallPayload);

                    var isMethodCallFailed = SerDe.ReadInt(s);
                    //TODO - add boolean instead of int in the backend
                    if (isMethodCallFailed != 0)
                    {
                        var    jvmFullStackTrace = SerDe.ReadString(s);
                        string errorMessage      = string.Format("JVM method execution failed: {0}", methodName);
                        logger.LogError(errorMessage);
                        logger.LogError(jvmFullStackTrace);
                        throw new Exception(errorMessage);
                    }

                    var typeAsChar = Convert.ToChar(s.ReadByte());
                    switch (typeAsChar) //TODO - add support for other types
                    {
                    case 'n':
                        break;

                    case 'j':
                        returnValue = SerDe.ReadString(s);
                        break;

                    case 'c':
                        returnValue = SerDe.ReadString(s);
                        break;

                    case 'i':
                        returnValue = SerDe.ReadInt(s);
                        break;

                    case 'd':
                        returnValue = SerDe.ReadDouble(s);
                        break;

                    case 'b':
                        returnValue = Convert.ToBoolean(s.ReadByte());
                        break;

                    case 'l':
                        returnValue = ReadJvmObjectReferenceCollection(s);

                        break;

                    default:
                        // convert typeAsChar to UInt32 because the char may be non-printable
                        throw new NotSupportedException(string.Format("Identifier for type 0x{0:X} not supported", Convert.ToUInt32(typeAsChar)));
                    }
                }
                sockets.Enqueue(socket);
            }
            catch (Exception e)
            {
                logger.LogException(e);
                throw;
            }

            return(returnValue);
        }
Exemple #5
0
        private object ReadCollection(NetworkStream s)
        {
            object returnValue;
            var    listItemTypeAsChar = Convert.ToChar(s.ReadByte());
            int    numOfItemsInList   = SerDe.ReadInt(s);

            switch (listItemTypeAsChar)
            {
            case 'c':
                var strList = new List <string>();
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    strList.Add(SerDe.ReadString(s));
                }
                returnValue = strList;
                break;

            case 'i':
                var intList = new List <int>();
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    intList.Add(SerDe.ReadInt(s));
                }
                returnValue = intList;
                break;

            case 'd':
                var doubleList = new List <double>();
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    doubleList.Add(SerDe.ReadDouble(s));
                }
                returnValue = doubleList;
                break;

            case 'b':
                var boolList = new List <bool>();
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    boolList.Add(Convert.ToBoolean(s.ReadByte()));
                }
                returnValue = boolList;
                break;

            case 'r':
                var byteArrayList = new List <byte[]>();
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    var byteArrayLen = SerDe.ReadInt(s);
                    byteArrayList.Add(SerDe.ReadBytes(s, byteArrayLen));
                }
                returnValue = byteArrayList;
                break;

            case 'j':
                var jvmObjectReferenceList = new List <JvmObjectReference>();
                for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
                {
                    var itemIdentifier = SerDe.ReadString(s);
                    jvmObjectReferenceList.Add(new JvmObjectReference(itemIdentifier));
                }
                returnValue = jvmObjectReferenceList;
                break;

            default:
                // convert listItemTypeAsChar to UInt32 because the char may be non-printable
                throw new NotSupportedException(
                          string.Format("Identifier for list item type 0x{0:X} not supported",
                                        Convert.ToUInt32(listItemTypeAsChar)));
            }
            return(returnValue);
        }
Exemple #6
0
        internal static byte[] ConvertParametersToBytes(object[] parameters, bool addTypeIdPrefix = true)
        {
            var paramtersBytes = new List <byte[]>();

            foreach (var parameter in parameters)
            {
                if (parameter != null)
                {
                    if (addTypeIdPrefix)
                    {
                        paramtersBytes.Add(GetTypeId(parameter.GetType()));
                    }

                    if (parameter is int)
                    {
                        paramtersBytes.Add(SerDe.ToBytes((int)parameter));
                    }
                    else if (parameter is long)
                    {
                        paramtersBytes.Add(SerDe.ToBytes((long)parameter));
                    }
                    else if (parameter is string)
                    {
                        paramtersBytes.Add(ToPayloadBytes(parameter.ToString()));
                    }
                    else if (parameter is bool)
                    {
                        paramtersBytes.Add(SerDe.ToBytes((bool)parameter));
                    }
                    else if (parameter is double)
                    {
                        paramtersBytes.Add(SerDe.ToBytes((double)parameter));
                    }
                    else if (parameter is byte[])
                    {
                        paramtersBytes.Add(SerDe.ToBytes(((byte[])parameter).Length));
                        paramtersBytes.Add((byte[])parameter);
                    }
                    else if (parameter is int[])
                    {
                        paramtersBytes.Add(GetTypeId(typeof(int)));
                        paramtersBytes.Add(SerDe.ToBytes(((int[])parameter).Length));
                        paramtersBytes.AddRange(((int[])parameter).Select(x => SerDe.ToBytes(x)));
                    }
                    else if (parameter is long[])
                    {
                        paramtersBytes.Add(GetTypeId(typeof(long)));
                        paramtersBytes.Add(SerDe.ToBytes(((long[])parameter).Length));
                        paramtersBytes.AddRange(((long[])parameter).Select(x => SerDe.ToBytes(x)));
                    }
                    else if (parameter is double[])
                    {
                        paramtersBytes.Add(GetTypeId(typeof(double)));
                        paramtersBytes.Add(SerDe.ToBytes(((double[])parameter).Length));
                        paramtersBytes.AddRange(((double[])parameter).Select(x => SerDe.ToBytes(x)));
                    }
                    else if (parameter is IEnumerable <byte[]> )
                    {
                        paramtersBytes.Add(GetTypeId(typeof(byte[])));
                        paramtersBytes.Add(SerDe.ToBytes(((IEnumerable <byte[]>)parameter).Count())); //TODO - Count() will traverse the collection - change interface?
                        foreach (var byteArray in (IEnumerable <byte[]>)parameter)
                        {
                            paramtersBytes.Add(SerDe.ToBytes(byteArray.Length));
                            paramtersBytes.Add(byteArray);
                        }
                    }
                    else if (parameter is IEnumerable <string> )
                    {
                        paramtersBytes.Add(GetTypeId(typeof(string)));
                        paramtersBytes.Add(SerDe.ToBytes(((IEnumerable <string>)parameter).Count())); //TODO - Count() will traverse the collection - change interface?
                        paramtersBytes.AddRange(from stringVal in (IEnumerable <string>) parameter select ToPayloadBytes(stringVal));
                    }
                    else if (parameter is IEnumerable <JvmObjectReference> )
                    {
                        paramtersBytes.Add(GetTypeId(typeof(JvmObjectReference)));
                        paramtersBytes.Add(SerDe.ToBytes(((IEnumerable <JvmObjectReference>)parameter).Count())); //TODO - Count() will traverse the collection - change interface?
                        paramtersBytes.AddRange(from jObj in (IEnumerable <JvmObjectReference>) parameter select ToPayloadBytes(jObj.Id));
                    }
                    else if (IsDictionary(parameter.GetType()))
                    {
                        // Generic Dictionary 'parameter' passed into this function is a object which lost its Generic Type T (namely Dictionary<T, T>).
                        // Cannot neither cast to Dictionary<T, T> nor Dictionary<dynamic, dynamic>, so we need to first cast to Non-Generic interface IDictionary and then
                        // rebuild a Dictionary<dynamic, dynamic>.
                        var nonGenericDict = (IDictionary)parameter;
                        var dict           = new Dictionary <dynamic, dynamic>();
                        foreach (var k in nonGenericDict.Keys)
                        {
                            dict[k] = nonGenericDict[k];
                        }

                        Type keyType = parameter.GetType().GetGenericArguments()[0];

                        // Below serialization is coressponding to deserialization method ReadMap() of SerDe.scala
                        paramtersBytes.Add(SerDe.ToBytes(dict.Count()));                                                          // dictionary's length
                        paramtersBytes.Add(GetTypeId(keyType));                                                                   // keys' data type
                        paramtersBytes.Add(SerDe.ToBytes(dict.Count()));                                                          // keys' length, same as dictionary's length
                        paramtersBytes.AddRange(from kv in dict select ConvertParametersToBytes(new object[] { kv.Key }, false)); // keys, do not need type prefix
                        paramtersBytes.Add(SerDe.ToBytes(dict.Count()));                                                          // values' length, same as dictionary's length
                        paramtersBytes.AddRange(from kv in dict select ConvertParametersToBytes(new object[] { kv.Value }));      // values, need type prefix
                    }
                    else if (parameter is JvmObjectReference)
                    {
                        paramtersBytes.Add(ToPayloadBytes((parameter as JvmObjectReference).Id));
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format("Type {0} is not supported", parameter.GetType()));
                    }
                }
                else
                {
                    paramtersBytes.Add(new [] { Convert.ToByte('n') });
                }
            }

            return(paramtersBytes.SelectMany(byteArray => byteArray).ToArray());
        }
Exemple #7
0
 public double ReadDouble()
 {
     return(SerDe.Convert(stream.Reader.ReadDouble()));
 }
Exemple #8
0
        public string ReadString(int length)
        {
            var stringAsBytes = stream.Reader.ReadBytes(length);

            return(SerDe.ToString(stringAsBytes));
        }
Exemple #9
0
 public int ReadInt()
 {
     return(SerDe.Convert(stream.Reader.ReadInt32()));
 }
Exemple #10
0
 public char ReadChar()
 {
     return(SerDe.ToChar(stream.Reader.ReadByte()));
 }
Exemple #11
0
 public void Write(string value)
 {
     byte[] buffer = SerDe.ToBytes(value);
     Write(buffer.Length);
     Write(buffer);
 }
Exemple #12
0
 public void Write(long value)
 {
     stream.Writer.Write(SerDe.Convert(value));
 }