Пример #1
0
        public void TestReadAllBytes()
        {
            var fromBytes = Enumerable.Repeat<byte>(1, 1024 * 1024)
                .Concat(Enumerable.Repeat<byte>(2, 1024 * 1024))
                .Concat(Enumerable.Repeat<byte>(3, 1024 * 1024)).ToArray();

            var stream = new MemoryStream(fromBytes);

            var toBytes = stream.ReadAllBytes();

            Assert.IsTrue(fromBytes.SequenceEqual(toBytes));
        }
Пример #2
0
        public void Enqueue(EnvelopeToken envelope)
        {
            using (var stream = new MemoryStream())
            {
                _formatter.Serialize(stream, envelope);

                stream.Position = 0;
                var bytes = stream.ReadAllBytes();

                _queue.Add(bytes);
            }
        }
Пример #3
0
        public void read_all_bytes()
        {
            var helloWorld = "Hello world.";
            var bytes = Encoding.Unicode.GetBytes(helloWorld);

            var stream = new MemoryStream();
            stream.Write(bytes, 0, bytes.Length);

            stream.Position = 0;

            var actual = stream.ReadAllBytes();

            actual.ShouldBe(bytes);
        }
        public void Extract_PackageContainsFiles_ContentOfEachFileIsWrittenToDisc()
        {
            // Arrange
            string targetFolder = Environment.CurrentDirectory;

            string file1Path = "tools\\file1.txt";
            string file1Content = Guid.NewGuid().ToString();
            Stream file1Stream = TestUtilities.GetStreamReaderForText(file1Content).BaseStream;
            var file1 = new Mock<IPackageFile>();
            file1.Setup(f => f.Path).Returns(file1Path);
            file1.Setup(f => f.GetStream()).Returns(file1Stream);

            var files = new List<IPackageFile> { file1.Object };

            string packageId = "Package.A";
            var version = new SemanticVersion(1, 0, 0, 0);
            var packageDirectory = Path.Combine(targetFolder, string.Format("{0}.{1}", packageId, version));

            var package = new Mock<IPackage>();
            package.Setup(p => p.Id).Returns(packageId);
            package.Setup(p => p.Version).Returns(version);
            package.Setup(p => p.GetFiles()).Returns(files);

            var filesystemAccessor = new Mock<IFilesystemAccessor>();

            filesystemAccessor.Setup(f => f.DirectoryExists(packageDirectory)).Returns(true);
            filesystemAccessor.Setup(f => f.DeleteDirectory(packageDirectory)).Returns(true);

            var writeStream = new MemoryStream();
            var streamWriter = new StreamWriter(writeStream);
            filesystemAccessor.Setup(f => f.GetWriteStream(It.Is<string>(s => s.EndsWith(file1Path)))).Returns(streamWriter.BaseStream);

            var nugetPackageExtractor = new NugetPackageExtractor(filesystemAccessor.Object);

            // Act
            var result = nugetPackageExtractor.Extract(package.Object, targetFolder);

            // Assert
            var bytes = writeStream.ReadAllBytes();
            string newContentOfOldFile = Encoding.UTF8.GetString(bytes);

            Assert.IsNotNull(result);
            Assert.AreEqual(file1Content, newContentOfOldFile);

            file1.Verify(f => f.GetStream(), Times.Once());
            filesystemAccessor.Verify(f => f.GetWriteStream(It.Is<string>(s => s.StartsWith(packageDirectory))), Times.Once());
        }
        public void Serialize(Envelope envelope)
        {
            if (envelope.Message == null) throw new InvalidOperationException("No message on this envelope to serialize");

            if (envelope.ContentType.IsEmpty())
            {
                envelope.ContentType = _graph.DefaultContentType;
            }

            var serializer = selectSerializer(envelope);
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(envelope.Message, stream);
                stream.Position = 0;

                envelope.Data = stream.ReadAllBytes();
            }
        }
Пример #6
0
 public void StreamTest()
 {
     MemoryStream stream = new MemoryStream();
     stream.WriteAllBytes(CommonHelper.StringToByte("Hello"));
     stream.Position = 0;
     var bytes = stream.ReadAllBytes();
     Assert.AreEqual(CommonHelper.ByteToString(bytes), "Hello");
 }
            public static object Encode(SQLInfo.ColDef col, object data)
            {
                if (data == null) return null;

                //Logger.ReportVerbose("Encoding " + col.ColName + " as " + col.ColType.Name.ToLower());
                switch (col.ColType.Name.ToLower())
                {
                    case "guid":
                        return data.ToString();
                    case "string":
                        return data;

                    case "datetime":
                        return ((DateTime)data).Ticks;

                    case "mediatype":
                        return ((MediaType)data).ToString();

                    case "videoformat":
                        return ((VideoFormat)data).ToString();

                    case "downloadpolicy":
                        return ((MediaBrowser.Library.Network.DownloadPolicy)data).ToString();

                    case "int":
                    case "int16":
                    case "int32":
                    case "int64":
                    case "long":
                    case "double":
                    case "nullable`1":
                        return data;
                    default:
                        var ms = new MemoryStream();
                        Serializer.Serialize<object>(ms,data);
                        ms.Seek(0,0);
                        return ms.ReadAllBytes();

                }
            }
Пример #8
0
        private void PublishPackage(PublishState state)
        {
            var url = new Uri(String.Format(CultureInfo.InvariantCulture, "{0}/{1}", _baseGalleryServerUrl, PublishPackageService));

            using (Stream requestStream = new MemoryStream()) {
                var data = new PublishData {
                    Key = state.PublishKey,
                    Id = state.PackageMetadata.Id,
                    Version = state.PackageMetadata.Version.ToString()
                };

                var jsonSerializer = new DataContractJsonSerializer(typeof(PublishData));
                jsonSerializer.WriteObject(requestStream, data);
                requestStream.Seek(0, SeekOrigin.Begin);

                WebClient client = new WebClient();
                client.Proxy = _cachedProxy;
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                client.Headers[HttpRequestHeader.UserAgent] = _userAgent;
                client.UploadProgressChanged += OnUploadProgressChanged;
                client.UploadDataCompleted += OnPublishPackageCompleted;
                client.UploadDataAsync(url, "POST", requestStream.ReadAllBytes(), state);
            }
        }
        public void SelfUpdate_PackageIsFound_VersionIsNewerThanTheCurrentVersion_PackageContainsExeWhichNameMatchesTheApplication_PackageContentIsWrittenToExecutable()
        {
            // Arrange
            string exePath = "NuDeploy.exe";
            var version = new Version(1, 0, 0, 0);

            var userInterface = new Mock<IUserInterface>();
            var applicationInformation = new ApplicationInformation { NameOfExecutable = exePath };
            var packageRepositoryBrowser = new Mock<IPackageRepositoryBrowser>();
            var filesystemAccessor = new Mock<IFilesystemAccessor>();

            var packageContent = "Content of the update package";
            var packageContentStream = TestUtilities.GetStreamReaderForText(packageContent).BaseStream;

            var packageFileMock = new Mock<IPackageFile>();
            packageFileMock.Setup(f => f.Path).Returns("tools\\" + exePath);
            packageFileMock.Setup(f => f.GetStream()).Returns(packageContentStream);

            var packageMock = new Mock<IPackage>();
            packageMock.Setup(p => p.Version).Returns(new SemanticVersion(2, 0, 0, 0));
            packageMock.Setup(p => p.GetFiles()).Returns(new List<IPackageFile> { packageFileMock.Object });

            var targetStream = new MemoryStream();
            filesystemAccessor.Setup(f => f.GetWriteStream(exePath)).Returns(targetStream);

            packageRepositoryBrowser.Setup(p => p.FindPackage(It.IsAny<string>())).Returns(packageMock.Object);

            var selfUpdateService = new SelfUpdateService(
                userInterface.Object, applicationInformation, packageRepositoryBrowser.Object, filesystemAccessor.Object);

            // Act
            bool result = selfUpdateService.SelfUpdate(exePath, version);

            // Assert
            var bytes = targetStream.ReadAllBytes();
            string newContentOfOldFile = Encoding.UTF8.GetString(bytes);
            filesystemAccessor.Verify(f => f.GetWriteStream(exePath), Times.Once());
            Assert.AreEqual(packageContent, newContentOfOldFile);
            Assert.IsTrue(result);
        }
Пример #10
0
 private Assembly ExtractAssembly(ZipEntry entry)
 {
     var stream = new MemoryStream();
     entry.Extract(stream);
     return Assembly.Load(stream.ReadAllBytes());
 }
        public void Should_Read_To_Array()
        {
            var bytes = new byte[] { 1, 2, 3 };
            var stream = new MemoryStream(bytes);

            Assert.AreEqual(bytes, stream.ReadAllBytes());
        }
Пример #12
-1
        public byte[] Serialize()
        {
            using (var stream = new MemoryStream())
            {
                new BinarySerializer().Serialize(this, stream);
                stream.Position = 0;
                return stream.ReadAllBytes();
            }

            
        }