Beispiel #1
0
        public void MemoryStream_CopyTo_Invalid()
        {
            ChunkedMemoryStream memoryStream;
            const string        BufferSize = "bufferSize";

            using (memoryStream = new ChunkedMemoryStream(this.allocator))
            {
                const string Destination = "destination";
                Assert.Throws <ArgumentNullException>(Destination, () => memoryStream.CopyTo(destination: null));

                // Validate the destination parameter first.
                Assert.Throws <ArgumentNullException>(Destination, () => memoryStream.CopyTo(destination: null, bufferSize: 0));
                Assert.Throws <ArgumentNullException>(Destination, () => memoryStream.CopyTo(destination: null, bufferSize: -1));

                // Then bufferSize.
                Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // 0-length buffer doesn't make sense.
                Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: -1));
            }

            // After the Stream is disposed, we should fail on all CopyTos.
            Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // Not before bufferSize is validated.
            Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: -1));

            ChunkedMemoryStream disposedStream = memoryStream;

            // We should throw first for the source being disposed...
            Assert.Throws <ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1));

            // Then for the destination being disposed.
            memoryStream = new ChunkedMemoryStream(this.allocator);
            Assert.Throws <ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1));
            memoryStream.Dispose();
        }
Beispiel #2
0
        public void MemoryStream_ReadTest_Negative()
        {
            var ms2 = new ChunkedMemoryStream(this.allocator);

            Assert.Throws <ArgumentNullException>(() => ms2.Read(null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => ms2.Read(new byte[] { 1 }, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => ms2.Read(new byte[] { 1 }, 0, -1));
            Assert.Throws <ArgumentException>(() => ms2.Read(new byte[] { 1 }, 2, 0));
            Assert.Throws <ArgumentException>(() => ms2.Read(new byte[] { 1 }, 0, 2));

            ms2.Dispose();

            Assert.Throws <ObjectDisposedException>(() => ms2.Read(new byte[] { 1 }, 0, 1));
        }
Beispiel #3
0
        private static bool ReCalculateFileMD5(string path)
        {
            if (!XLPack.IsFileExist(path))
            {
                return(false);
            }

            var       position = XLPack.FOpen(path, "r");
            long      fileSize = XLPack.FSize(position);
            const int bufSize  = 0x4000;

            byte[] buffer = new byte[bufSize];
            IntPtr bufPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
            // TODO: Do this without reading the entire file into memory to calculate the MD5
            //       Maybe try to use the XLPack.DLL's MD5Init, MD5Update and MD5 Finalize functions ?
            // Using ChunkedMemoryStream instead of MemoryStream to hopefully avoid outofmemory errors on large files
            ChunkedMemoryStream ms = new ChunkedMemoryStream();
            long readTotalSize     = 0;

            while (readTotalSize < fileSize)
            {
                long readSize = fileSize - readTotalSize;
                if (readSize > bufSize)
                {
                    readSize = bufSize;
                }
                XLPack.FRead(position, bufPtr, readSize);
                ms.Write(buffer, 0, (int)readSize); // readSize should never be out of int range, so it's safe to cast it
                readTotalSize += readSize;
            }
            XLPack.FClose(ref position);
            ms.Position = 0;
            MD5    md5Hash   = MD5.Create();
            string md5String = GetMd5Hash(md5Hash, ms).Replace("-", "").ToLower();

            ms.Dispose();
            var res = SetFileMD5(path, md5String);

            return(res);
        }
Beispiel #4
0
            public void InsertRecord(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
            {
                var pref = PostgresTuple.BuildSlashEscape(escaping.Length);

                if (mappings != null)
                {
                    foreach (var p in pref)
                    {
                        mappings(sw, p);
                    }
                }
                else
                {
                    sw.Write(pref);
                }
                sw.Write('x');
                Value.ToPostgresBytea(sw);
                if (Dispose)
                {
                    Value.Dispose();
                }
            }
        internal static Stream ExecuteCommands <TFormat>(
            IProcessingEngine engine,
            IWireSerialization serialization,
            IServerCommandDescription <TFormat>[] commands,
            IRequestContext request,
            IResponseContext response,
            string accept)
        {
            var result = Execute(engine, commands, request, response);

            if (result.Error != null)
            {
                return(result.Error);
            }
            if (result.Result == null)
            {
                response.ContentType = accept;
                return(null);
            }
            if (accept == "application/octet-stream")
            {
                response.ContentType = "application/octet-stream";
                if (result.Result is Stream)
                {
                    return(result.Result as Stream);
                }
                else if (result.Result is StreamReader)
                {
                    return((result.Result as StreamReader).BaseStream);
                }
                else if (result.Result is StringBuilder)
                {
                    var sb  = result.Result as StringBuilder;
                    var cms = ChunkedMemoryStream.Create();
                    var sw  = cms.GetWriter();
                    for (int i = 0; i < sb.Length;)
                    {
                        var min = Math.Min(sb.Length - i, cms.CharBuffer.Length);
                        sb.CopyTo(i, cms.CharBuffer, 0, min);
                        i += min;
                        sw.Write(cms.CharBuffer, 0, min);
                    }
                    sw.Flush();
                    cms.Position = 0;
                    return(cms);
                }
                else if (result.Result is byte[])
                {
                    return(new MemoryStream(result.Result as byte[]));
                }
                else if (result.Result is string)
                {
                    var cms = ChunkedMemoryStream.Create();
                    var sw  = cms.GetWriter();
                    sw.Write(result.Result as string);
                    sw.Flush();
                    cms.Position = 0;
                    return(cms);
                }
                else if (result.Result is char[])
                {
                    var cms = ChunkedMemoryStream.Create();
                    var sw  = cms.GetWriter();
                    sw.Write(result.Result as char[]);
                    sw.Flush();
                    cms.Position = 0;
                    return(cms);
                }
                return(response.ReturnError(
                           "Unexpected command result. Can't convert "
                           + result.Result.GetType().FullName + " to octet-stream. Use application/x-dotnet mime type for .NET binary serialization",
                           HttpStatusCode.UnsupportedMediaType));
            }
            if (accept == "application/base64")
            {
                response.ContentType = "application/base64";
                if (result.Result is Stream)
                {
                    var stream = result.Result as Stream;
                    try
                    {
                        var cms = stream as ChunkedMemoryStream;
                        if (cms != null)
                        {
                            return(cms.ToBase64Stream());
                        }
                        else
                        {
                            cms = new ChunkedMemoryStream(stream);
                            try { return(cms.ToBase64Stream()); }
                            finally { cms.Dispose(); }
                        }
                    }
                    finally { stream.Dispose(); }
                }
                else if (result.Result is StreamReader)
                {
                    var sr = result.Result as StreamReader;
                    try
                    {
                        var cms = sr.BaseStream as ChunkedMemoryStream;
                        if (cms != null)
                        {
                            return(cms.ToBase64Stream());
                        }
                        else
                        {
                            cms = new ChunkedMemoryStream(sr.BaseStream);
                            try { return(cms.ToBase64Stream()); }
                            finally { cms.Dispose(); }
                        }
                    }
                    finally { sr.Dispose(); }
                }
                else if (result.Result is StringBuilder)
                {
                    var sb = result.Result as StringBuilder;
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        var sw = cms.GetWriter();
                        for (int i = 0; i < sb.Length;)
                        {
                            var min = Math.Min(sb.Length - i, cms.CharBuffer.Length);
                            sb.CopyTo(i, cms.CharBuffer, 0, min);
                            i += min;
                            sw.Write(cms.CharBuffer, 0, min);
                        }
                        sw.Flush();
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                else if (result.Result is byte[])
                {
                    var bytes = result.Result as byte[];
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        cms.Write(bytes, 0, bytes.Length);
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                else if (result.Result is string)
                {
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        var sw = cms.GetWriter();
                        sw.Write(result.Result as string);
                        sw.Flush();
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                else if (result.Result is char[])
                {
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        var sw = cms.GetWriter();
                        sw.Write(result.Result as char[]);
                        sw.Flush();
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                return(response.ReturnError("Unexpected command result. Can't convert to base64.", HttpStatusCode.UnsupportedMediaType));
            }
            if (accept == "application/x-dotnet")
            {
                response.ContentType = "application/x-dotnet";
                var bf = new BinaryFormatter();
                bf.AssemblyFormat = FormatterAssemblyStyle.Simple;
                var cms = ChunkedMemoryStream.Create();
                bf.Serialize(cms, result.Result);
                cms.Position = 0;
                return(cms);
            }
            var ms = ChunkedMemoryStream.Create();

            response.ContentType = serialization.Serialize(result.Result, accept, ms);
            ms.Position          = 0;
            return(ms);
        }