Ejemplo n.º 1
0
        private static void WriteAllText(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool      success   = false;
            Exception exception = null;

            if (arguments.Length > 1)
            {
                string   path      = (arguments[0] as NSJSString)?.Value;
                string   contents  = (arguments[1] as NSJSString)?.Value;
                Encoding encodings = NSJSEncoding.DefaultEncoding;
                if (arguments.Length > 2)
                {
                    encodings = NSJSEncoding.GetEncoding(arguments[2] as NSJSObject);
                }
                try
                {
                    FILE.WriteAllText(path, contents, encodings);
                    success = true;
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }
            if (exception == null)
            {
                arguments.SetReturnValue(success);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }
Ejemplo n.º 2
0
        private static void ContentEncoding(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            HTTPResponse             response  = GetResponse(arguments.This);

            if (response == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                NSJSVirtualMachine machine = arguments.VirtualMachine;
                if (arguments.Length <= 0)
                {
                    arguments.SetReturnValue(NSJSEncoding.New(machine, response.ContentEncoding));
                }
                else
                {
                    var encoding = NSJSEncoding.GetEncoding(arguments[0] as NSJSObject);
                    if (encoding == null)
                    {
                        encoding = NSJSEncoding.DefaultEncoding;
                    }
                    response.ContentEncoding = encoding;
                    arguments.SetReturnValue(true);
                }
            }
        }
Ejemplo n.º 3
0
        private void ComputeHashValueOrString(IntPtr info, bool hashValue)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);

            byte[] buffer = null;
            if (arguments.Length > 0)
            {
                buffer = (arguments[0] as NSJSUInt8Array)?.Buffer;
                if (buffer == null)
                {
                    string s = (arguments[0] as NSJSString).Value;
                    if (s != null)
                    {
                        Encoding encoding = NSJSEncoding.DefaultEncoding;
                        if (arguments.Length > 1)
                        {
                            encoding = NSJSEncoding.GetEncoding(arguments[1] as NSJSObject);
                        }
                        buffer = encoding.GetBytes(s);
                    }
                }
            }
            if (buffer == null)
            {
                Throwable.ArgumentNullException(arguments.VirtualMachine);
            }
            else
            {
                try
                {
                    using (HASHAlgorithm hash = New())
                    {
                        buffer = hash.ComputeHash(buffer);
                        if (hashValue)
                        {
                            arguments.SetReturnValue(buffer);
                        }
                        else
                        {
                            string s = string.Empty;
                            for (int i = 0; i < buffer.Length; i++)
                            {
                                s += buffer[i].ToString("X2");
                            }
                            arguments.SetReturnValue(s);
                        }
                    }
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
            }
        }
Ejemplo n.º 4
0
        private static void ReadAllText(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            string    path      = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;
            string    contents  = null;
            Exception exception = null;

            if (FILE.Exists(path))
            {
                Encoding encodings = NSJSEncoding.DefaultEncoding;
                if (arguments.Length > 1)
                {
                    encodings = NSJSEncoding.GetEncoding(arguments[1] as NSJSObject);
                }
                try
                {
                    contents = FILE.ReadAllText(path, encodings);
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }
            if (contents != null)
            {
                arguments.SetReturnValue(contents);
            }
            else if (exception == null)
            {
                Throwable.FileNotFoundException(arguments.VirtualMachine);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }