Exemplo n.º 1
0
        // Converts the file content passed as any supported object to a binary stream;
        Stream GetContent(object value, NewFileParameters parameters)
        {
            if (value == null)
            {
                throw new ApplicationException("Content passed as value cannot be empty.");
            }
            value = value.GetBaseObject();
            var file = value as System.IO.FileInfo;

            if (file != null)
            {
                return(file.OpenRead());
            }
            if (value is byte)
            {
                return(new MemoryStream(new byte[] { (byte)value }));
            }
            var encoding = parameters.GetEncoding();
            var text     = value as string;

            if (text != null)
            {
                if (text.Length == 0)
                {
                    throw new ApplicationException("Content passed as value cannot be empty.");
                }
                return(new MemoryStream(encoding.GetBytes(text)));
            }
            var array = value as Array;

            if (array == null)
            {
                throw new ApplicationException("Content passed as value " +
                                               "must be a file, byte or string or an array of them.");
            }
            var content = ConvertToBytes.GetBytes(array, encoding);

            if (content.Length == 0)
            {
                throw new ApplicationException("Content passed as value cannot be empty.");
            }
            return(new MemoryStream(content));
        }
Exemplo n.º 2
0
 // Converts the file content passed as any supported object to a binary stream;
 Stream GetContent(object value, NewFileParameters parameters)
 {
     if (value == null)
         throw new ApplicationException("Content passed as value cannot be empty.");
     value = value.GetBaseObject();
     var file = value as System.IO.FileInfo;
     if (file != null)
         return file.OpenRead();
     if (value is byte)
         return new MemoryStream(new byte[] { (byte) value });
     var encoding = parameters.GetEncoding();
     var text = value as string;
     if (text != null) {
         if (text.Length == 0)
             throw new ApplicationException("Content passed as value cannot be empty.");
         return new MemoryStream(encoding.GetBytes(text));
     }
     var array = value as Array;
     if (array == null)
         throw new ApplicationException("Content passed as value " +
             "must be a file, byte or string or an array of them.");
     var content = ConvertToBytes.GetBytes(array, encoding);
     if (content.Length == 0)
         throw new ApplicationException("Content passed as value cannot be empty.");
     return new MemoryStream(content);
 }