One of the design goals here is to prevent the buffer from getting in the way and slowing down underlying stream accesses when it is not needed. If you always read & write for sizes greater than the internal buffer size, then this class may not even allocate the internal buffer. See a large comment in Write for the details of the write buffer heuristic. This class buffers reads & writes in a shared buffer. (If you maintained two buffers separately, one operation would always trash the other buffer anyways, so we might as well use one buffer.) The assumption here is you will almost always be doing a series of reads or writes, but rarely alternate between the two of them on the same stream. Class Invariants: The class has one buffer, shared for reading & writing. It can only be used for one or the other at any point in time - not both. The following should be true: 0 implies the read buffer is valid, but we're at the end of the buffer. * _readPos == _readLen == 0 means the read buffer contains garbage. * Either _writePos can be greater than 0, or _readLen & _readPos can be greater than zero, but neither can be greater than zero at the same time. ]]> This class will never cache more bytes than the max specified buffer size. However, it may use a temporary buffer of up to twice the size in order to combine several IO operations on the underlying stream into a single operation. This is because we assume that memory copies are significantly faster than IO operations on the underlying stream (if this was not true, using buffering is never appropriate). The max size of this "shadow" buffer is limited as to not allocate it on the LOH. Shadowing is always transient. Even when using this technique, this class still guarantees that the number of bytes cached (not yet written to the target stream or not yet consumed by the user) is never larger than the actual specified buffer size.
Inheritance: Stream
コード例 #1
0
ファイル: MappedResults.cs プロジェクト: Trebornide/ravendb
		public void PutReducedResult(string view, string reduceKey, int level, int sourceBucket, int bucket, RavenJObject data)
		{
			Guid etag = uuidGenerator.CreateSequentialUuid();

			using (var update = new Update(session, ReducedResults, JET_prep.Insert))
			{
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["view"], view, Encoding.Unicode);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["level"], level);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["reduce_key"], reduceKey, Encoding.Unicode);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["hashed_reduce_key"], HashReduceKey(reduceKey));
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["bucket"], bucket);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["source_bucket"], sourceBucket);

				using (Stream stream = new BufferedStream(new ColumnStream(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["data"])))
				{
					using (var dataStream = documentCodecs.Aggregate(stream, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds)))
					{
						data.WriteTo(dataStream);
						dataStream.Flush();
					}
				}

				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["etag"], etag.TransformToValueForEsentSorting());
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["timestamp"], SystemTime.UtcNow);

				update.Save();
			}
		}
コード例 #2
0
ファイル: UidSpaceMapper.cs プロジェクト: pszmyd/SHS
 internal UidSpaceMapper(long strideLength, long newBaseUid, long oldBaseUid, long numUrls, string fileName)
 {
     this.oldBaseUid = oldBaseUid;
       this.newBaseUid = newBaseUid;
       this.numUrls = numUrls;
       this.strideLength = strideLength;
       using (var stream = new BufferedStream(new FileStream(fileName, FileMode.Open, FileAccess.Read))) {
     this.bytes = new CachedStream(stream, (ulong)stream.Length);
     this.deco = new VarNybbleIntStreamDecompressor(this.bytes);
       }
       // Construct the index by parsing this.bytes
       long numIdxItems = (numUrls / strideLength) + 1;
       this.idxPosition = new ulong[numIdxItems];
       this.idxGapSum = new ulong[numIdxItems];
       ulong gapSum = 0;
       int p = 0;
       for (long i = 0; i <= numUrls; i++) {
     if (i % strideLength == 0) {
       this.idxPosition[p] = deco.GetPosition();
       this.idxGapSum[p] = gapSum;
       p++;
     }
     if (i < numUrls) gapSum += deco.GetUInt64();
       }
       Contract.Assert(p == numIdxItems);
       Contract.Assert(deco.AtEnd());
 }
        public void BadXmlTests()
        {
            try {
            EncodeHelper.Deserialize("<garbage />", typeof(System.Object));
            Assert.Fail("You should not have been able to deserialize this preceding xml");
              }
              catch {
              }

              MemoryStream ms = new MemoryStream();
              using (StreamWriter sw = new StreamWriter(ms)) {
            sw.Write("<garbage />");
            sw.Flush();
            object test = EncodeHelper.Deserialize(ms);
            Assert.AreEqual(null, test);
              }

              try {
            //Test the Deserialize method that builds an error message.
            ms = new MemoryStream();
            using (BufferedStream sw = new BufferedStream(ms)) {
              sw.Write(Encoding.UTF8.GetPreamble(), 0, 3);
              byte[] msg = System.Text.Encoding.UTF8.GetBytes("<garbage>");
              sw.Write(msg, 0, msg.Length);
              sw.Flush();
              ms.Position = 0;
              object test = EncodeHelper.Deserialize(ms);
              Assert.AreEqual(null, test);
            }
              }
              catch (Exception ex) {
            if (ex.Message.IndexOf("Couldn't parse XML") == -1)
              Assert.Fail("We should have failed for a bad xml file.");
              }
        }
コード例 #4
0
ファイル: MappedResults.cs プロジェクト: KyleGobel/ravendb
		public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data)
		{
			Etag etag = uuidGenerator.CreateSequentialUuid(UuidType.MappedResults);
			using (var update = new Update(session, MappedResults, JET_prep.Insert))
			{
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["view"], view, Encoding.Unicode);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["document_key"], docId, Encoding.Unicode);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["reduce_key"], reduceKey, Encoding.Unicode);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["hashed_reduce_key"], HashReduceKey(reduceKey));
				var mapBucket = IndexingUtil.MapBucket(docId);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["bucket"], mapBucket);

				using (Stream stream = new BufferedStream(new ColumnStream(session, MappedResults, tableColumnsCache.MappedResultsColumns["data"])))
				{
					using (var dataStream = documentCodecs.Aggregate(stream, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds)))
					{
						data.WriteTo(dataStream);
						dataStream.Flush();
					}
				}

				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["etag"], etag.TransformToValueForEsentSorting());
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["timestamp"], SystemTime.UtcNow.ToBinary());

				update.Save();
			}
		}
コード例 #5
0
ファイル: EbcdicTests.cs プロジェクト: pkubryk/SummerBatch
        public void EbcdicTestsTestReader()
        {
            FileFormat fileFormat = CopybookLoader.LoadCopybook(Copybook + "/Test.fileformat");
            using (BufferedStream inputStream = new BufferedStream(new MemoryStream(Bytes)))
            {
                EbcdicReader reader = new EbcdicReader(inputStream, fileFormat, false);
                List<object> record = reader.NextRecord();
                Assert.AreEqual(Objects.Length, record.ToArray().Length);
                Assert.AreEqual(Objects.GetType(), record.ToArray().GetType());

                //NOTE : CollectionAssert does not support nested collection handling ...
                int index = 0;
                foreach (var rec in record.ToArray())
                {
                    var array = rec as Array;
                    if (array != null)
                    {
                        CollectionAssert.AreEqual((Array)Objects[index], array);
                    }
                    else
                    {
                        Assert.AreEqual(Objects[index], rec);
                    }
                    index++;
                }
            }
        }
コード例 #6
0
		public static TransportMessage ToTransportMessage(this SqsTransportMessage sqsTransportMessage, IAmazonS3 amazonS3, SqsConnectionConfiguration connectionConfiguration)
        {
            var messageId = sqsTransportMessage.Headers[Headers.MessageId];

			var result = new TransportMessage(messageId, sqsTransportMessage.Headers);

            if (!string.IsNullOrEmpty(sqsTransportMessage.S3BodyKey))
            {
                var s3GetResponse = amazonS3.GetObject(connectionConfiguration.S3BucketForLargeMessages, sqsTransportMessage.S3BodyKey);
                result.Body = new byte[s3GetResponse.ResponseStream.Length];
                using (BufferedStream bufferedStream = new BufferedStream(s3GetResponse.ResponseStream))
                {
                    int count;
                    int transferred = 0;
                    while ((count = bufferedStream.Read(result.Body, transferred, 8192)) > 0)
                    {
                        transferred += count;
                    }
                }
            }
            else
			{
				result.Body = Convert.FromBase64String(sqsTransportMessage.Body);
			}

            result.TimeToBeReceived = sqsTransportMessage.TimeToBeReceived;

			if (sqsTransportMessage.ReplyToAddress != null)
			{
				result.Headers[Headers.ReplyToAddress] = sqsTransportMessage.ReplyToAddress.ToString();
			}

            return result;
        }
コード例 #7
0
ファイル: ReaderFactory.cs プロジェクト: carlhuth/GenXSource
        /// <summary>
        /// Detects the format of the Reader input, and if known, it will return
        /// a CDK Reader to read the format, or null when the reader is not
        /// implemented.
        /// </summary>
        /// <returns> null if CDK does not contain a reader for the detected format.
        ///
        /// </returns>
        /// <seealso cref="createReader(Reader)">
        /// </seealso>
        public virtual IChemObjectReader createReader(System.IO.Stream input)
        {
            System.IO.BufferedStream bistream      = new System.IO.BufferedStream(input, 8192);
            System.IO.Stream         istreamToRead = bistream; // if gzip test fails, then take default
            SupportClass.BufferedStreamManager.manager.MarkPosition(5, bistream);
            int countRead = 0;

            try
            {
                sbyte[] abMagic = new sbyte[4];
                countRead         = SupportClass.ReadInput(bistream, abMagic, 0, 4);
                bistream.Position = SupportClass.BufferedStreamManager.manager.ResetMark(bistream);
                if (countRead == 4)
                {
                    if (abMagic[0] == (sbyte)0x1F && abMagic[1] == (sbyte)SupportClass.Identity(0x8B))
                    {
                        //UPGRADE_ISSUE: Constructor 'java.util.zip.GZIPInputStream.GZIPInputStream' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipGZIPInputStream'"
                        istreamToRead = null;// new GZIPInputStream(bistream);
                    }
                }
            }
            catch (System.IO.IOException exception)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                //logger.error(exception.Message);
                //logger.debug(exception);
            }
            return(createReader(new System.IO.StreamReader(istreamToRead, System.Text.Encoding.Default)));
        }
コード例 #8
0
ファイル: Processor.cs プロジェクト: shiallia/ZHttpServer
        public void process()
        {
            // we can't use a StreamReader for input, because it buffers up extra data on us inside it's
            // "processed" view of the world, and we want the data raw after the headers
            inputByteStream = new System.IO.BufferedStream(socket.GetStream());

            // we probably shouldn't be using a streamwriter for all output from handlers either
            outputStream     = new System.IO.StreamWriter(new System.IO.BufferedStream(socket.GetStream()));
            outputByteStream = new System.IO.BufferedStream(socket.GetStream());
            try
            {
                parseRequest();
                if (http_method.Equals("GET"))
                {
                    handleGETRequest();
                }
                else if (http_method.Equals("POST"))
                {
                    handlePOSTRequest();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
                writeFailure();
            }
            //outputStream.Flush();
            // bs.Flush(); // flush any remaining output
            inputByteStream = null; outputStream = null; // bs = null;
            socket.Close();
        }
コード例 #9
0
ファイル: ReaderFactory.cs プロジェクト: carlhuth/GenXSource
        public virtual IChemFormat guessFormat(System.IO.Stream input)
        {
            System.IO.BufferedStream bistream      = new System.IO.BufferedStream(input, 8192);
            System.IO.Stream         istreamToRead = bistream; // if gzip test fails, then take default
            SupportClass.BufferedStreamManager.manager.MarkPosition(5, bistream);
            int countRead = 0;

            try
            {
                sbyte[] abMagic = new sbyte[4];
                countRead         = SupportClass.ReadInput(bistream, abMagic, 0, 4);
                bistream.Position = SupportClass.BufferedStreamManager.manager.ResetMark(bistream);
                if (countRead == 4)
                {
                    if (abMagic[0] == (sbyte)0x1F && abMagic[1] == (sbyte)SupportClass.Identity(0x8B))
                    {
                        //UPGRADE_ISSUE: Constructor 'java.util.zip.GZIPInputStream.GZIPInputStream' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipGZIPInputStream'"
                        istreamToRead = null;// new GZIPInputStream(bistream);
                    }
                }
            }
            catch (System.IO.IOException exception)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                //logger.error(exception.Message);
                //logger.debug(exception);
            }
            //UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.io.BufferedReader.BufferedReader'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
            //UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'"
            return(guessFormat(new System.IO.StreamReader(new System.IO.StreamReader(istreamToRead, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(istreamToRead, System.Text.Encoding.Default).CurrentEncoding)));
        }
コード例 #10
0
        public PooledSocket(IPEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout)
        {
            this.isAlive = true;

            var socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            // TODO test if we're better off using nagle
            //PHP: OPT_TCP_NODELAY
            //socket.NoDelay = true;

            var timeout = connectionTimeout == TimeSpan.MaxValue
                            ? Timeout.Infinite
                            : (int)connectionTimeout.TotalMilliseconds;

            var rcv = receiveTimeout == TimeSpan.MaxValue
                ? Timeout.Infinite
                : (int)receiveTimeout.TotalMilliseconds;

            socket.ReceiveTimeout = rcv;
            socket.SendTimeout = rcv;
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

            ConnectWithTimeout(socket, endpoint, timeout);

            this.socket = socket;
            this.endpoint = endpoint;

            this.inputStream = new BufferedStream(new BasicNetworkStream(socket));
        }
コード例 #11
0
ファイル: CSocket.cs プロジェクト: livvyguo/Gaea
 public CSocket(IPEndPoint _endPoint, ScoketPool _pool, SocketPoolProfile config)
 {
     socketConfig = config;
     endpoint = _endPoint;
     pool = _pool;
     Socket _socket = new Socket(_endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
     _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, (int)config.SendTimeout.TotalMilliseconds);
     _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 24 * 3600 * 1000/*one day*/);//(int)config.ReceiveTimeout.TotalMilliseconds);
     _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, !config.Nagle);
     _socket.Connect(_endPoint);
     socket = _socket;
     this.inputStream = new BufferedStream(new NetworkStream(this.socket), config.BufferSize);
     Thread th = new Thread(delegate() {
         while (true)
         {
             try
             {
                 Receive();
             }
             catch (Exception ex)
             {
                 logger.Notice(ex.Message);
             }
         }
     });
     th.Start();
 }
コード例 #12
0
        public PooledSocket(IPEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout)
        {
            var socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // all operations are "atomic", we do not send small chunks of data
            socket.NoDelay = true;

            var mre = new ManualResetEvent(false);
            var timeout = connectionTimeout == TimeSpan.MaxValue
                            ? Timeout.Infinite
                            : (int)connectionTimeout.TotalMilliseconds;

            socket.ReceiveTimeout = (int)receiveTimeout.TotalMilliseconds;
            socket.SendTimeout = (int)receiveTimeout.TotalMilliseconds;

            socket.BeginConnect(endpoint, iar =>
            {
                try { using (iar.AsyncWaitHandle) socket.EndConnect(iar); }
                catch { }

                mre.Set();
            }, null);

            if (!mre.WaitOne(timeout) || !socket.Connected)
            {
                using (socket)
                    throw new TimeoutException("Could not connect to " + endpoint);
            }

            this.socket = socket;
            this.endpoint = endpoint;

            this.inputStream = new BufferedStream(new BasicNetworkStream(socket));
        }
コード例 #13
0
        public override void ShowUsage()
        {
            //BufferedStream类主要也是用来处理流数据的,但是该类主要的功能是用来封装其他流类。
            //为什么要封装其他流类,这么做的意义是什么?按照微软的话说主要是减少某些流直接操作存储设备的时间。
            //对于一些流来说直接向磁盘中存储数据这种做法的效率并不高,用BufferedStream包装过的流,先在内存中进行统一的处理再向磁盘中写入数据,也会提高写入的效率。

            Console.WriteLine("BufferedStream类主要也是用来处理流数据的,但是该类主要的功能是用来封装其他流类。");
            FileStream fileStream1 = File.Open(@"C:\NewText.txt", FileMode.OpenOrCreate, FileAccess.Read);  //读取文件流
            FileStream fileStream2 = File.Open(@"C:\Text2.txt", FileMode.OpenOrCreate, FileAccess.Write);   //写入文件流

            byte[] array4 = new byte[4096];

            BufferedStream bufferedInput = new BufferedStream(fileStream1);         //封装文件流
            BufferedStream bufferedOutput = new BufferedStream(fileStream2);        //封装文件流

            int byteRead = bufferedInput.Read(array4, 0, array4.Length);
            bufferedOutput.Write(array4, 0, array4.Length);

            //= bufferedInput.Read(array4, 0, 4096);
            while (byteRead > 0)                                                    //读取到了数据
            {
                bufferedOutput.Write(array4, 0, byteRead);
                Console.WriteLine(byteRead);
                break;
            };
            bufferedInput.Close();
            bufferedOutput.Close();
            fileStream1.Close();
            fileStream2.Close();
            Console.ReadKey();
        }
コード例 #14
0
ファイル: JsonContent.cs プロジェクト: heinnge/ravendb
        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            if (HasNoData())
                return new CompletedTask<bool>(true);

            using ( var undisposableStream = new UndisposableStream(stream) )
            using ( var bufferedStream = new BufferedStream(undisposableStream))
            {
                var writer = new StreamWriter(bufferedStream, DefaultEncoding);
                if (string.IsNullOrEmpty(Jsonp) == false)
                {
                    writer.Write(Jsonp);
                    writer.Write("(");
                }

                Data.WriteTo(new JsonTextWriter(writer)
                {
                    Formatting = IsOutputHumanReadable ? Formatting.Indented : Formatting.None,
                }, Default.Converters);

                if (string.IsNullOrEmpty(Jsonp) == false)
                    writer.Write(")");

                writer.Flush();
            }

            return new CompletedTask<bool>(true);
        }
コード例 #15
0
ファイル: Lists.cs プロジェクト: bbqchickenrobot/ravendb
		public void Set(string name, string key, RavenJObject data, UuidType uuidType)
		{
			Api.JetSetCurrentIndex(session, Lists, "by_name_and_key");
			Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
			Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None);

			var exists = Api.TrySeek(session, Lists, SeekGrbit.SeekEQ);


			using (var update = new Update(session, Lists, exists ? JET_prep.Replace : JET_prep.Insert))
			{
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["name"], name, Encoding.Unicode);
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["key"], key, Encoding.Unicode);
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], uuidGenerator.CreateSequentialUuid(uuidType).TransformToValueForEsentSorting());
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["created_at"], SystemTime.UtcNow);

				using (var columnStream = new ColumnStream(session, Lists, tableColumnsCache.ListsColumns["data"]))
				{
					if (exists)
						columnStream.SetLength(0);
					using (Stream stream = new BufferedStream(columnStream))
					{
						data.WriteTo(stream);
						stream.Flush();
					}
				}
				update.Save();
			}
		}
コード例 #16
0
 /// <summary>
 /// 分块下载
 /// </summary>
 /// <param name="bufferedStream"></param>
 /// <param name="startPos"></param>
 /// <param name="endPos"></param>
 /// <param name="localFilePath"></param>
 /// <param name="bucketName"></param>
 /// <param name="fileKey"></param>
 private static void Download(BufferedStream bufferedStream, long startPos, long endPos, String localFilePath, String bucketName, String fileKey)
 {
     Stream contentStream = null;
     try
     {
         var getObjectRequest = new GetObjectRequest(bucketName, fileKey);
         getObjectRequest.SetRange(startPos, endPos);
         var ossObject = _client.GetObject(getObjectRequest);
         byte[] buffer = new byte[1024 * 1024];
         var bytesRead = 0;
         bufferedStream.Seek(startPos, SeekOrigin.Begin);
         contentStream = ossObject.Content;
         while ((bytesRead = contentStream.Read(buffer, 0, buffer.Length)) > 0)
         {
             bufferedStream.Write(buffer, 0, bytesRead);
         }
     }
     finally
     {
         if (contentStream != null)
         {
             contentStream.Dispose();
         }
     }
 }
コード例 #17
0
ファイル: CompressedContent.cs プロジェクト: heinnge/ravendb
        protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            using (var uncloseableStream = new UndisposableStream(stream))
            using (var bufferedStream = new BufferedStream(uncloseableStream))
            {
                Stream compressedStream = null;

                if (encodingType == "gzip")
                {
                    compressedStream = new GZipStream(bufferedStream, CompressionMode.Compress, leaveOpen: true);
                }
                else if (encodingType == "deflate")
                {
                    compressedStream = new DeflateStream(bufferedStream, CompressionMode.Compress, leaveOpen: true);
                }
                else throw new InvalidOperationException("This shouldn't happen, ever.");

                await originalContent.CopyToAsync(compressedStream);

                if (compressedStream != null)
                {
                    compressedStream.Dispose();
                }
            }
        }
コード例 #18
0
            public Response(HttpWebResponse httpResponse)
            {
                this.response = httpResponse;
                StatusCode    = httpResponse.StatusCode;
                Message       = httpResponse.StatusDescription;
                ContentType   = httpResponse.ContentType;
                ContentLength = httpResponse.ContentLength == -1 ? null : (long?)httpResponse.ContentLength;
                string contentTransferEncoding = httpResponse.Headers["Content-Transfer-Encoding"];
                bool   isBase64 = contentTransferEncoding != null && contentTransferEncoding.Equals("base64", StringComparison.CurrentCultureIgnoreCase);

                if (httpResponse.StatusCode == HttpStatusCode.OK ||
                    httpResponse.StatusCode == HttpStatusCode.Created ||
                    httpResponse.StatusCode == HttpStatusCode.NonAuthoritativeInformation ||
                    httpResponse.StatusCode == HttpStatusCode.PartialContent)
                {
                    if (isBase64)
                    {
                        Stream = new System.IO.BufferedStream(new CryptoStream(httpResponse.GetResponseStream(), new FromBase64Transform(), CryptoStreamMode.Read), 64 * 1024);
                    }
                    else
                    {
                        Stream = new System.IO.BufferedStream(httpResponse.GetResponseStream(), 64 * 1024);
                    }
                }
                else
                {
                    try { httpResponse.Close(); }
                    catch (Exception) { }
                }
            }
コード例 #19
0
ファイル: IPod5GDbReader.cs プロジェクト: rcw5/iSavr
        /// <summary>
        /// Read the iPod5G database into the MediaDatabase.
        /// 
        /// </summary>
        public void readDatabase()
        {
            int buf;
            long position;
            //Create the handle to the iTunes database
            strDB = new BufferedStream(new FileStream(iTunesDBFile, FileMode.Open, FileAccess.Read));
            //read!
            while ((buf = this.strDB.ReadByte()) != -1)
            {
                if (buf == (int)'m') //Read until we find a 'm'
                {
                    position = strDB.Position;

                    byte[] buff = new byte[3];
                    this.strDB.Read(buff, 0, 3);
                    if (Encoding.Default.GetString(buff).Equals("hit")) //'mhit' describes a song in the database
                    {
                         parseMhit();

                    }
                    else
                    {
                        this.strDB.Seek(position, SeekOrigin.Begin); //go back to just after the m
                    }

                }
            }
        }
コード例 #20
0
 public static byte[] GetFileContent(String smbUrl)
 {
                 #if DEBUG_SAMBA
     Android.Util.Log.Debug("SmbClient", "Getting file content of " + smbUrl);
                 #endif
     byte[] buffer = null;
     try
     {
         var file = new Jcifs.Smb.SmbFile(smbUrl);
         using (var sis = new System.IO.BufferedStream(new SambaInputStream(file)))
         {
             var length = file.Length();
             buffer = new byte[length];
             sis.Read(buffer, 0, (int)length);                     // will internally make several tries
         }
     }
     catch (Jcifs.Smb.SmbException ex)
     {
         LogException(ex);
         throw new System.IO.IOException(ex.Message, ex);
     }
     catch (Java.IO.IOException ex)
     {
         LogException(ex);
         throw new System.IO.IOException(ex.Message, ex);
     }
     catch (System.IO.IOException ex)
     {
         LogException(ex);
         throw;
     }
     return(buffer);
 }
コード例 #21
0
		public Etag AddAttachment(string key, Etag etag, Stream data, RavenJObject headers)
		{
			Api.JetSetCurrentIndex(session, Files, "by_name");
			Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey);
			var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ);
			if (isUpdate)
			{
				var existingEtag = Etag.Parse(Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]));
				if (existingEtag != etag && etag != null)
				{
					throw new ConcurrencyException("PUT attempted on attachment '" + key +
						"' using a non current etag")
					{
						ActualETag = existingEtag,
						ExpectedETag = etag
					};
				}
			}
			else
			{
				if (data == null)
					throw new InvalidOperationException("When adding new attachment, the attachment data must be specified");

				if (Api.TryMoveFirst(session, Details))
					Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1);
			}

			Etag newETag = uuidGenerator.CreateSequentialUuid(UuidType.Attachments);
			using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert))
			{
				Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode);
				if (data != null)
				{
					long written;
					using (var columnStream = new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"]))
					{
						if (isUpdate)
							columnStream.SetLength(0);
						using (var stream = new BufferedStream(columnStream))
						{
							data.CopyTo(stream);
							written = stream.Position;
							stream.Flush();
						}
					}
					if (written == 0) // empty attachment
					{
						Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["data"], new byte[0]);
					}
				}

				Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting());
				Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode);

				update.Save();
			}
			logger.Debug("Adding attachment {0}", key);

			return newETag;
		}
コード例 #22
0
ファイル: ConfigLoader.cs プロジェクト: Team2122/SpudConf
        public static void Generate(Config config, Stream stream)
        {
            if (config == null)
                throw new ArgumentNullException("config");
            using (var bs = new BufferedStream(stream))
            using (var sw = new StreamWriter(bs, new System.Text.UTF8Encoding(false)))
            {
                sw.NewLine = "\n";

                //sw.WriteLine("# Generated by SpudConf {0}", Assembly.GetExecutingAssembly().GetName().Version);
                //sw.WriteLine(@"#! author = ""{0}""", Properties.Settings.Default.UserName);
                //sw.WriteLine(@"#! date = ""{0}""", DateTime.Now.ToFileTimeUtc());
                foreach (var c in config)
                {
                    foreach (var co in c.Value.Comments)
                    {
                        if (co.Equals("whitespace"))
                        {
                            sw.WriteLine();
                        }
                        else
                        {
                            sw.WriteLine("#{0}", co);
                        }
                    }
                    foreach (var m in c.Value.Metadata)
                    {
                        sw.WriteLine("#!{0} = \"{1}\"", m.Key, m.Value);
                    }
                    sw.WriteLine("{0} = \"{1}\"", c.Key, c.Value.Value);
                }
            }
        }
コード例 #23
0
        public static XDocument GetXmlFromStream(string url)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.ServicePoint.Expect100Continue = false;
            request.Proxy = null;

            var response = request.GetResponse();
            
            var xml = string.Empty;
            using (var stream = response.GetResponseStream())
            {
                using (var buffer = new BufferedStream(stream))
                {
                    using (var reader = new StreamReader(buffer, Encoding.UTF8))
                    {
                        xml = reader.ReadToEnd();
                    }
                }
            }

            //The Star-Wars-Beatles-PeerGynt-bug
            //Aleph XML-repsonses somtimes returns Unicode SOH instead of SP 
            //a few places in the document.
            //This causes the parsing to fail..
            const char soh ='\u0001';
            const char sp = '\u0020';
            var xmlEscaped = xml.Replace(soh, sp);

            return XDocument.Parse(xmlEscaped);
        }
コード例 #24
0
 public DSCTokenizer(Stream stream, bool isUnicode, bool isLittleEndian)
 {
     _stream = stream;
     _bufferedStream = new BufferedStream(_stream);
     _isUnicode = isUnicode;
     _isLittleEndian = isLittleEndian;
 }
コード例 #25
0
ファイル: Program.cs プロジェクト: demonix/KeClientTracer
        static void Main(string[] args)
        {
            FileStream fs1 = new FileStream("out.bin",FileMode.Create,FileAccess.Write,FileShare.Read);
            FileStream fs2 = new FileStream("out2.bin",FileMode.Create,FileAccess.Write,FileShare.Read);
            bf1 = new BufferedStream(fs1);
            bf2 = new BufferedStream(fs2);
            try
            {
                Server server = new Server(10, 4096* 100 * 2);
                server.Sequential = false;
                server.Start(new IPEndPoint(IPAddress.Any, 40004));
                server.MessageReceived += OnMessageReceived;

                server.ClientConnected += OnClientConnected;
                server.ClientDisconnected += OnClientDisconnected;
                Console.ReadKey();
            }
            finally
            {
                bf1.Flush();
                bf2.Flush();
                bf1.Close();
                bf2.Close();
                fs1.Close();
                fs2.Close();
            }
        }
コード例 #26
0
ファイル: CommHelper.cs プロジェクト: agilecomputing/nomads
        public bool init(TcpClient client)
        {
            if (client == null) {
                return false;
            }

            _tcpClient = client;

            try {
                Stream stream = _tcpClient.GetStream();

                // !! DO NOT instantiate the LineReaderInputStream with a BufferedStream
                // this can cause very odd behaviour
                _inputReader = new LineReaderInputStream(stream);

                BufferedStream buffStream = new BufferedStream(stream);
                _outputWriter = new StreamWriter(buffStream);

                return true;
            }
            catch (SocketException ex) {
                // close the socket if an exception occurs getting its  stream
                closeConnection();                        
                if (_debug) {
                    Console.WriteLine(ex.ToString());
                }
            }

            return false;  
        }
コード例 #27
0
ファイル: LoginFeiliu.cs プロジェクト: houguohua/Scut
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="base64code">传入加密数据</param>
        /// <returns>返回解密数据</returns>
        static public string Decrypt(string base64code)
        {

            var a = new FileInfo("E:/100115_SignKey.pub").OpenRead();
            var b = new BufferedStream(a);
            //string c = 
            try
            {
                UnicodeEncoding ByteConverter = new UnicodeEncoding();

                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSA.FromXmlString("");

                RSAParameters rsaParameters = new RSAParameters();

                rsaParameters.Exponent = Convert.FromBase64String("AQAB");
                rsaParameters.Modulus =
                    Convert.FromBase64String(
                        "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyq3xJ3jtuWSWk4nCCgysplqV3DyFGaF7iP7PO2vEUsgEq+vqKr+frlwji2n7A1TbpV7KhEGJIT9LW/9WCdBhlu6gnBdErtAA4Or43ol2K1BnY6VBcLWccloMd3YFHG8gOohCVIDbw863Wg0FNS27SM25U+XQfrNFaqBIa093WgAbwRIK06uzC01sW+soutvk+yAYBtbH7I7/1/dFixHKS2KN/7y3pvmXYBIRuBvn35IqwY3Gk0duEfbEr9F6wm2VKhS1zQG760FrHfhbXR+IN5nSTQBHBkw4QukLLvUqueKYfVdp2/2RCnY/At0bbOcA2tAPohDAfUDRdOZsFiTIMQID");

                byte[] encryptedData;
                byte[] decryptedData;

                encryptedData = Convert.FromBase64String(base64code);

                decryptedData = RSADeCrtypto(encryptedData, rsaParameters, true);
                return ByteConverter.GetString(decryptedData);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }
コード例 #28
0
ファイル: BitReader.cs プロジェクト: nazarovmikhail/sharp-lzw
        public BitReader(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            ms.Position = 0;

            s = new BufferedStream(ms);
        }
コード例 #29
0
ファイル: Nyse.cs プロジェクト: miguelqvd/QSTK.Net
        private static IList<DateTime> GetData()
        {
            IList<DateTime> dates = new List<DateTime>();
            var cultureInfo = new CultureInfo("en-US");

            using (var fs = File.Open(DataFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (var bs = new BufferedStream(fs, 262144))
            using (var sr = new StreamReader(bs))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null) break;
                    try
                    {
                        dates.Add(DateTime.Parse(line, cultureInfo));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

            return dates;
        }
コード例 #30
0
 public static void SetPositionToNegativeValue_Throws_ArgumentOutOfRangeException()
 {
     using (BufferedStream stream = new BufferedStream(new MemoryStream()))
     {
         Assert.Throws<ArgumentOutOfRangeException>(() => stream.Position = -1);
     }
 }
コード例 #31
0
        public static void GetObjectPartly()
        {
            const string localFilePath = "<your localFilePath>";
            const string bucketName = "<your bucketName>";
            const string fileKey = "<your fileKey>";
            const string accessId = "<your access id>";
            const string  accessKey = "<your access key>";

            _client = new OssClient(accessId, accessKey);

            using (var fileStream = new FileStream(localFilePath, FileMode.OpenOrCreate))
            {
                var bufferedStream = new BufferedStream(fileStream);
                var objectMetadata = _client.GetObjectMetadata(bucketName, fileKey);
                var fileLength = objectMetadata.ContentLength;
                const int partSize = 1024 * 1024 * 10;

                var partCount = CalPartCount(fileLength, partSize);

                for (var i = 0; i < partCount; i++)
                {
                    var startPos = partSize * i;
                    var endPos = partSize * i + (partSize < (fileLength - startPos) ? partSize : (fileLength - startPos)) - 1;
                    Download(bufferedStream, startPos, endPos, localFilePath, bucketName, fileKey);
                }
                bufferedStream.Flush();
            }
        }
コード例 #32
0
ファイル: Deduplicator.cs プロジェクト: marler8997/Protobuild
        public void AddFile(DeduplicatorState state, FileInfo sourceFile, string destinationPath)
        {
            if (state.DestinationToFileHash.ContainsKey(destinationPath))
            {
                // File has already been added.
                return;
            }

            // Read the source file.
            var memory = new MemoryStream();
            using (var stream = new BufferedStream(new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read, FileShare.None), 1200000))
            {
                stream.CopyTo(memory);
            }

            // Hash the memory stream.
            var sha1 = new SHA1Managed();
            memory.Seek(0, SeekOrigin.Begin);
            var hashBytes = sha1.ComputeHash(memory);
            var hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
            memory.Seek(0, SeekOrigin.Begin);

            // Add to the file hash -> source map if not already present.
            if (!state.FileHashToSource.ContainsKey(hashString))
            {
                state.FileHashToSource.Add(hashString, memory);
            }
            else
            {
                memory.Dispose();
            }

            state.DestinationToFileHash.Add(destinationPath, hashString);
        }
コード例 #33
0
ファイル: DroneProto.cs プロジェクト: jank3/MissionPlanner
        public bool connect()
        {
            client = new TcpClient(APIConstants.DEFAULT_SERVER,APIConstants.DEFAULT_TCP_PORT);

            client.NoDelay = true;

            start = DateTime.Now;

            st = new BufferedStream(client.GetStream(),8000);

            Envelope ping = new Envelope() { ping = new PingMsg() { nonce = new Random().Next(int.MaxValue) } , type = Envelope.MsgCode.PingMsgCode };

            DateTime deadline = DateTime.Now.AddMilliseconds(3000);
            while (DateTime.Now < deadline)
            {
                send(ping);

                System.Threading.Thread.Sleep(500);

                var pingresp = receive(0);

                if (pingresp != null && pingresp.pingResponse != null)
                    return true;
            }
            
            throw new IOException("No Responce");

        }
コード例 #34
0
ファイル: FileIndex3.cs プロジェクト: slieser/sandbox2
        public void Build(string filename) {
            positions = new List<long>();

            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
                using (BufferedStream bs = new BufferedStream(fs)) {
                    bool detectFirstLine = true;

                    int b = 0;
                    while ((b = bs.ReadByte()) != -1) {
                        if (b == '\n') {
                            positions.Add(bs.Position);
                            detectFirstLine = false;
                        }
                        if (detectFirstLine) {
                            if (bs.Position == 1 && b != 0xEF) {
                                positions.Add(0);
                                detectFirstLine = false;
                            }
                            if (bs.Position == 2 && b != 0xBB) {
                                positions.Add(0);
                                detectFirstLine = false;
                            }
                            if (bs.Position == 3 && b != 0xBF) {
                                positions.Add(0);
                                detectFirstLine = false;
                            }
                            if (bs.Position == 4 && detectFirstLine) {
                                positions.Add(3);
                                detectFirstLine = false;
                            }
                        }
                    }
                }
            }
        }
コード例 #35
0
        /* Unzips dirName + ".zip" --> dirName, removing dirName
         * first */
        public virtual void  Unzip(System.String zipName, System.String destDirName)
        {
#if SHARP_ZIP_LIB
            // get zip input stream
            ICSharpCode.SharpZipLib.Zip.ZipInputStream zipFile;
            zipFile = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(zipName + ".zip"));

            // get dest directory name
            System.String      dirName = FullDir(destDirName);
            System.IO.FileInfo fileDir = new System.IO.FileInfo(dirName);

            // clean up old directory (if there) and create new directory
            RmDir(fileDir.FullName);
            System.IO.Directory.CreateDirectory(fileDir.FullName);

            // copy file entries from zip stream to directory
            ICSharpCode.SharpZipLib.Zip.ZipEntry entry;
            while ((entry = zipFile.GetNextEntry()) != null)
            {
                System.IO.Stream streamout = new System.IO.BufferedStream(new System.IO.FileStream(new System.IO.FileInfo(System.IO.Path.Combine(fileDir.FullName, entry.Name)).FullName, System.IO.FileMode.Create));
                byte[]           buffer    = new byte[8192];
                int len;
                while ((len = zipFile.Read(buffer, 0, buffer.Length)) > 0)
                {
                    streamout.Write(buffer, 0, len);
                }

                streamout.Close();
            }

            zipFile.Close();
#else
            Assert.Fail("Needs integration with SharpZipLib");
#endif
        }
コード例 #36
0
        public GrammarBuilder GetWebsiteNamesGrammar()
        {
            try
            {
                Settings.CultureInfo = "en-GB";
                var webSiteNames = new List<string>();
                using (var fs = File.Open(VbwFileManager.FilePath() + "fnc_brwsr_websites" + VbwFileManager.FileExtension(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var bs = new BufferedStream(fs))
                using (var sr = new StreamReader(bs))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        webSiteNames.Add(line);
                    }
                }

                var dictationBuilder = new GrammarBuilder // creating a new grammar builder
                    {
                        Culture = new CultureInfo(Settings.CultureInfo)
                    };
                dictationBuilder.AppendDictation(); // append dictation to the created grammar builder

                var dictaphoneGb = new GrammarBuilder { Culture = new CultureInfo(Settings.CultureInfo) };
                dictaphoneGb.Append(dictationBuilder, 0 /* minimum repeat */, 10 /* maximum repeat*/ );
                dictaphoneGb.Append(new Choices(webSiteNames.ToArray()));
                dictaphoneGb.Append(dictationBuilder, 0 /* minimum repeat */, 10 /* maximum repeat*/ );
                return dictaphoneGb;
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex);
                throw;
            }
        }
コード例 #37
0
            public Connection(TcpClient connection)
            {
                _connection         = connection;
                _connection.NoDelay = true;

                _stream = new BufferedStream(_connection.GetStream());
                _reader = new BinaryReader(_stream);
                _writer = new BinaryWriter(_stream);
            }
コード例 #38
0
ファイル: AviWriter.cs プロジェクト: neolu/MissionPlanner
 public void avi_close()
 {
     if (fd != null)
     {
         avi_end(width, height, targetfps);
         writeindexs();
         fd.Close();
         fd.Dispose();
     }
     fd = null;
 }
コード例 #39
0
 private string CalculateHashFromStream(Stream stream)
 {
     using (var readerSource = new System.IO.BufferedStream(stream, 1200000))
     {
         using (var md51 = new System.Security.Cryptography.MD5CryptoServiceProvider())
         {
             md51.ComputeHash(readerSource);
             return(Convert.ToBase64String(md51.Hash));
         }
     }
 }
コード例 #40
0
ファイル: AviWriter.cs プロジェクト: zx901/ArduPlane
    /* start writing an AVI file */
    public void avi_start(string filename)
    {
        avi_close();

        fd = new BufferedStream(File.Open(filename, FileMode.Create));

        fd.Seek(2048, SeekOrigin.Begin);

        nframes   = 0;
        totalsize = 0;
    }
コード例 #41
0
        public ActionResult GetVideolist(string input)
        {
            lst.Clear();
            string lineOfText1 = "";

            input = "angular_videos";
            string folderName = "Upload";

            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath     = Path.Combine(webRootPath, folderName);

            // var filestream = new System.IO.FileStream("./app_data/" + input + ".txt",
            var filestream = new System.IO.FileStream(newPath + "/" + input + ".txt",

                                                      // var filestream = new System.IO.FileStream("./app_data/" + input + ".txt",
                                                      System.IO.FileMode.Open,
                                                      System.IO.FileAccess.Read,
                                                      System.IO.FileShare.ReadWrite);


            System.IO.BufferedStream bs = new System.IO.BufferedStream(filestream);

            var file = new System.IO.StreamReader(bs, System.Text.Encoding.UTF8, true, 128);

            while ((lineOfText1 = file.ReadLine()) != null)
            {
                try
                {
                    VdeoData obj = new VdeoData();
                    obj.name = Youtube.Youtube.GetVideoId(lineOfText1);
                    if (obj.name.Length > 4 && !lst.Select(x => x.name).ToList().Contains(obj.name))
                    {
                        lst.Add(obj);
                    }

                    // Example(lineOfText1);
                    if (lst.Count > 200)
                    {
                        break;
                    }
                } //return Json(lst.Take(5));
                catch (Exception ex)
                {
                }
            }
            var list = JsonConvert.SerializeObject(lst, Formatting.None,
                                                   new JsonSerializerSettings()
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            });

            return(Content(list, "application/json"));
        }
コード例 #42
0
        //private   Task ProcessPut (HttpClient hc, string putLocation, StreamContent sc)
        //{
        //    var response =   hc.Put (putLocation, sc);
        //    if (response.StatusCode == HttpStatusCode.TemporaryRedirect)
        //    {
        //        var uri = response.Content.Headers.ContentLocation;

        //    }
        //    else
        //    {
        //        response.EnsureSuccessStatusCode();
        //    }
        //}

        /// <summary>
        /// Opens an FSDataOutputStream at the indicated Path. Files are overwritten by default.
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="remotePath"></param>
        /// <returns></returns>
        public string CreateFile(string localFile, string remotePath)
        {
            WebClient hc  = this.CreateHTTPClient(true);
            var       uri = this.GetUriForOperation(remotePath) + "op=CREATE&overwrite=true";
            //            var resp =   hc.Put (uri, null);
            //            var putLocation = resp.Headers.Location;
            var            putLocation = uri;
            BufferedStream sc          = new System.IO.BufferedStream(System.IO.File.OpenRead(localFile));

            hc.UploadData(putLocation, "PUT", sc.ReadFully());
            return(hc.Headers[HttpResponseHeader.Location].ToString());
        }
コード例 #43
0
ファイル: AviWriter.cs プロジェクト: rajeper/ikarus-osd
    /* start writing an AVI file */
    public void avi_start(string filename)
    {
        avi_close();

        fd = new BufferedStream(File.Open(filename, FileMode.Create));

        fd.Seek(224, SeekOrigin.Begin);

        nframes         = 0;
        totalsize       = 0;
        max_buff_size   = 0;
        lista_tams      = new List <u32>();
        start_timestamp = DateTime.Now.Ticks;
    }
コード例 #44
0
ファイル: AviWriter.cs プロジェクト: OOP-03376400/DF_GCS_W
    /* start writing an AVI file */
    public void avi_start(string filename)
    {
        avi_close();

        fd = new BufferedStream(File.Open(filename, FileMode.Create));

        fd.Seek(8204, SeekOrigin.Begin);

        indexs.Clear();

        nframes   = 0;
        totalsize = 0;
        start     = DateTime.Now;
    }
コード例 #45
0
        /// <summary>
        /// Work around a 64Mb limit when writing streams to files.
        /// <seealso cref="http://groups.google.co.uk/group/microsoft.public.dotnet.framework/browse_thread/thread/ba3582b0a6e45517/03e8d3c8718a9c44"/>
        /// </summary>
        public static void WriteMemoryStreamToFile(string filename, MemoryStream memory)
        {
            //// old code:
            //using (Stream
            //    file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite),
            //    fileBuffer = new BufferedStream(file)
            //)
            //{
            //    byte[] memoryBuffer = memory.GetBuffer();
            //    int memoryLength = (int)memory.Length;
            //    fileBuffer.Write(memoryBuffer, 0, memoryLength); //##jpl: drawback: works only up to 2 gigabyte!
            //    fileBuffer.Close();
            //    file.Close();
            //}

            using (System.IO.Stream file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                using (Stream buffer = new System.IO.BufferedStream(file))
                {
                    byte[] memoryBuffer = memory.GetBuffer();
                    long   memoryLength = memory.Length;
                    int    index        = 0;
                    // writing everything at once fails for writing memory streams larger than 64 megabyte
                    // to a file stream on the network
                    //buffer.Write(memoryBuffer, 0, memoryLength);
                    // so we introduce a temporary buffer
                    const int copyBufferSize = 65536;
                    byte[]    copyBuffer     = new byte[copyBufferSize];
                    while (memoryLength > 0)
                    {
                        int actualLength;
                        if (memoryLength > copyBufferSize)
                        {
                            actualLength = copyBufferSize;
                        }
                        else
                        {
                            actualLength = (int)memoryLength; //jpl: this cast is valid, as now memoryLength <= copyBufferSize
                        }
                        Array.Copy(memoryBuffer, index, copyBuffer, 0, actualLength);
                        buffer.Write(copyBuffer, 0, actualLength);
                        memoryLength = memoryLength - actualLength;
                        index        = index + actualLength;
                    }
                    buffer.Flush();
                    buffer.Close();
                }
            }
        }
コード例 #46
0
        public static string GetHash256(string filename)
        {
            string hexString;

            using (var fs = new System.IO.FileStream(path: filename, mode: System.IO.FileMode.Open))
                using (var bs = new System.IO.BufferedStream(stream: fs))
                {
                    using (var sha256 = new System.Security.Cryptography.SHA256Managed())
                    {
                        byte[] hash = sha256.ComputeHash(inputStream: bs);
                        hexString = BitConverter.ToString(value: hash).Replace("-", String.Empty);
                    }
                }
            return(hexString);
        }
コード例 #47
0
 //-------------------------------------------------------------------------
 private static void copyStreamToFile(System.IO.Stream stream, string destination)
 {
     using (System.IO.BufferedStream bs = new System.IO.BufferedStream(stream))
     {
         using (System.IO.FileStream os = System.IO.File.OpenWrite(destination))
         {
             byte[] buffer = new byte[2 * 4096];
             int    nBytes;
             while ((nBytes = bs.Read(buffer, 0, buffer.Length)) > 0)
             {
                 os.Write(buffer, 0, nBytes);
             }
         }
     }
 }
コード例 #48
0
            public Connection(string hostname, int port)
            {
                try
                {
                    _connection         = new TcpClient(hostname, port);
                    _connection.NoDelay = true;

                    _stream = new BufferedStream(_connection.GetStream());
                    _reader = new BinaryReader(_stream);
                    _writer = new BinaryWriter(_stream);
                }
                catch (SocketException)
                {
                    _connection = null;
                    _stream     = null;

                    throw new ConnectionException("Failed to connect to server");
                }
            }
コード例 #49
0
        private string CalculateHashFromStream(Stream stream)
        {
            using (var readerSource = new System.IO.BufferedStream(stream, 1200000))
            {
                using (var md51 = new System.Security.Cryptography.MD5CryptoServiceProvider())
                {
                    md51.ComputeHash(readerSource);

                    //Dispose after using
                    readerSource.Dispose();
                    stream.Dispose();

                    //Because very big memory consuming, we need to force Garbage collection do its job immediately
                    GC.Collect();

                    //
                    return(Convert.ToBase64String(md51.Hash));
                }
            }
        }
コード例 #50
0
ファイル: program.cs プロジェクト: inwenis/stream_funs
    static void FileStreamAndBufferedStream()
    {
        System.IO.Stream         fileStream     = System.IO.File.Open("./file.txt", System.IO.FileMode.Open);
        System.IO.BufferedStream bufferedStream = new System.IO.BufferedStream(fileStream);
        System.Console.WriteLine("CanRead: " + bufferedStream.CanRead);
        System.Console.WriteLine("CanWrite: " + bufferedStream.CanWrite);
        System.Console.WriteLine("CanSeek: " + bufferedStream.CanSeek);
        System.Console.WriteLine("Length: " + bufferedStream.Length);
        System.Console.WriteLine("Position: " + bufferedStream.Position);
        int firstByte = bufferedStream.ReadByte();

        System.Console.WriteLine("Byte:" + firstByte);
        bufferedStream.Position = 0;
        System.Console.WriteLine("Position: " + bufferedStream.Position);
        bufferedStream.WriteByte((byte)(firstByte + 1));
        bufferedStream.Flush();
        //bufferedStream.Position = 0;
        //firstByte = bufferedStream.ReadByte();
        //bufferedStream.Dispose();
        System.Console.WriteLine("Byte:" + firstByte);
    }
コード例 #51
0
        public IEnumerable <UserData> GetData(string input, string start, string end)
        {
            input = "giffiles";
            string lineOfText1 = "";
            string folderName  = "Upload";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath     = Path.Combine(webRootPath, folderName);

            // var filestream = new System.IO.FileStream("./app_data/" + input + ".txt",
            var filestream = new System.IO.FileStream(newPath + input + ".txt",
                                                      System.IO.FileMode.Open,
                                                      System.IO.FileAccess.Read,
                                                      System.IO.FileShare.ReadWrite);

            System.IO.BufferedStream bs = new System.IO.BufferedStream(filestream);

            var             file = new System.IO.StreamReader(bs, System.Text.Encoding.UTF8, true, 128);
            List <UserData> lst  = new List <UserData>();

            while ((lineOfText1 = file.ReadLine()) != null)
            {
                try
                {
                    UserData obj = new UserData();
                    obj.name    = betweenStrings(lineOfText1, "1  ", "\"");
                    lineOfText1 = file.ReadLine();
                    obj.species = betweenStrings(lineOfText1, "1  ", "\"");
                    lineOfText1 = file.ReadLine();
                    obj.info1   = betweenStrings(lineOfText1, "1  ", "\"");
                    lineOfText1 = file.ReadLine();
                    obj.info2   = betweenStrings(lineOfText1, "1  ", "\"");
                    lst.Add(obj);
                }
                catch (Exception ex)
                {
                }
            }
            return(lst.Take(28));
        }
コード例 #52
0
        public static Stream Open()
        {
            var innerStream = File.Open(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileOptions.DeleteOnClose);

            try
            {
                var outerStream = new BufferedStream(innerStream, BufferSize);
                try
                {
                    return(new NotFlushingBufferOnDisposeStream(outerStream, innerStream));
                }
                catch
                {
                    outerStream.Dispose();
                    throw;
                }
            }
            catch
            {
                innerStream?.Dispose();
                throw;
            }
        }
コード例 #53
0
        /// <summary>
        /// Compare a source file with multiple destination files
        /// </summary>
        /// <param name="sourceFile">Source file path</param>
        /// <param name="comparisonFiles">List of destination files.</param>
        /// <returns>The result of the comparison</returns>
        public ComparisonResult CompareFiles(String sourceFile, List <String> comparisonFiles)
        {
            List <DestinationInfo> files = new List <DestinationInfo>();

            comparisonFiles.ForEach(file => files.Add(new DestinationInfo(file)));

            if (!File.Exists(sourceFile))
            {
                files.ForEach(file =>
                {
                    file.Done   = true;
                    file.Result = new ComparisonResult.Destination(file.Path, ComparisonResult.DifferenceType.SourceMissing);
                });
                return(new ComparisonResult(sourceFile, files.Select(file => file.Result).ToList(), true));
            }

            List <ComparisonResult.Destination> results = new List <ComparisonResult.Destination>();
            // get file length and make sure lengths are identical
            long length = new System.IO.FileInfo(sourceFile).Length;

            foreach (DestinationInfo file in files.Where(file => !file.Done))
            {
                if (sourceFile == file.Path)
                {
                    file.Done   = true;
                    file.Result = new ComparisonResult.Destination(file.Path, ComparisonResult.DifferenceType.Equal);
                }
                else if (!File.Exists(file.Path))
                {
                    file.Done   = true;
                    file.Result = new ComparisonResult.Destination(file.Path, ComparisonResult.DifferenceType.DestMissing);
                }
                else if (length != new System.IO.FileInfo(file.Path).Length)
                {
                    file.Done   = true;
                    file.Result = new ComparisonResult.Destination(file.Path, ComparisonResult.DifferenceType.NotEqual);
                }
                else if (useFastCompareForFile(file.Path))
                {
                    file.Done   = true;
                    file.Result = new ComparisonResult.Destination(file.Path, ComparisonResult.DifferenceType.Equal);
                }
                if (file.Done)
                {
                    results.Add(file.Result);
                }
            }

            if (!files.All(f => f.Done))
            {
                System.IO.BufferedStream sourceStream = null;
                try
                {
                    sourceStream = new System.IO.BufferedStream(System.IO.File.OpenRead(sourceFile));
                    foreach (DestinationInfo file in files.Where(f => !f.Done))
                    {
                        file.BufferStream = new System.IO.BufferedStream(System.IO.File.OpenRead(file.Path));
                    }
                    results.AddRange(CompareStreams(sourceStream, files.Where(f => !f.Done)));
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
                finally
                {
                    if (sourceStream != null)
                    {
                        sourceStream.Close();
                    }
                    foreach (DestinationInfo file in files.Where(f => f.Buffer != null))
                    {
                        file.BufferStream.Close();
                    }
                    foreach (DestinationInfo file in files.Where(f => !f.Done))
                    {
                        file.Done = true;
                        results.Add(new ComparisonResult.Destination(file.Path, ComparisonResult.DifferenceType.Equal));
                    }
                }
            }
            return(new ComparisonResult(sourceFile, results, true));
        }
コード例 #54
0
        public void Can_connect_to_Buffered_SslStream()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                SendTimeout    = -1,
                ReceiveTimeout = -1,
            };

            socket.Connect(Host, Port);

            if (!socket.Connected)
            {
                socket.Close();
                throw new Exception("Could not connect");
            }

            Stream networkStream = new NetworkStream(socket);

            SslStream sslStream;

            if (Env.IsMono)
            {
                //Mono doesn't support EncryptionPolicy
                sslStream = new SslStream(networkStream,
                                          leaveInnerStreamOpen: false,
                                          userCertificateValidationCallback: RedisConfig.CertificateValidationCallback,
                                          userCertificateSelectionCallback: RedisConfig.CertificateSelectionCallback);
            }
            else
            {
                var ctor = typeof(SslStream).GetConstructors()
                           .First(x => x.GetParameters().Length == 5);

                var policyType  = AssemblyUtils.FindType("System.Net.Security.EncryptionPolicy");
                var policyValue = Enum.Parse(policyType, "RequireEncryption");

                sslStream = (SslStream)ctor.Invoke(new[] {
                    networkStream,
                    false,
                    RedisConfig.CertificateValidationCallback,
                    RedisConfig.CertificateSelectionCallback,
                    policyValue,
                });

                //sslStream = new SslStream(networkStream,
                //    leaveInnerStreamOpen: false,
                //    userCertificateValidationCallback: null,
                //    userCertificateSelectionCallback: null,
                //    encryptionPolicy: EncryptionPolicy.RequireEncryption);
            }

#if NETCORE
            sslStream.AuthenticateAsClientAsync(Host).Wait();
#else
            sslStream.AuthenticateAsClient(Host);
#endif

            if (!sslStream.IsEncrypted)
            {
                throw new Exception("Could not establish an encrypted connection to " + Host);
            }

            var bstream = new System.IO.BufferedStream(sslStream, 16 * 1024);

            SendAuth(bstream);
        }
    public void connect_TCP()
    {
        try
        {
            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer
            // connected to the same address as specified by the server, port
            // combination.

            //Dim identity As String = "username:"******"Sent: {0}", "identity:" + identity)
            // Read the first batch of the TcpServer response bytes.
            System.IO.BufferedStream br           = new System.IO.BufferedStream(tcpStream);
            System.IO.StreamReader   streamReader = new System.IO.StreamReader(br);
            while ((streamReader.EndOfStream == false))
            {
                string currentLine = streamReader.ReadLine(); //return message from server
                //parse return message according to the message type
                //perform actions based on type.
                string response = handleResponse(currentLine);

                if (response != "Silence")
                {
                    Console.WriteLine(response);
                    Console.WriteLine("Received:" + currentLine);
                    Console.WriteLine("In busy waiting");
                }
                else
                {
                    silence++;
                    if (silence >= 15)
                    {
                        silence = 0;
                        Console.WriteLine("Received:" + currentLine);
                        Console.WriteLine("In busy waiting");
                    }
                }
                //this.mainForm.txtMain.Text += currentLine + Constants.vbNewLine;
            }
            //stream.Read(data, 0, 2)
            //Dim count As Integer
            //br.Read(data, 0, 2)
            //Dim bytes As Int32 = stream.ReadByte()
            Console.Write("TCP closed");
            tcpStream.Close();
            // Close everything.
            tcpClient.Close();
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
    }
コード例 #56
0
    public static void  Main(System.String[] args)
    {
        ChunkHeader     FormatChunkHeader = new ChunkHeader();
        ChunkHeader     DataChunkHeader   = new ChunkHeader();
        RiffChunkHeader myRiffChunkHeader = new RiffChunkHeader();
        WaveHeader      WaveHeader        = new WaveHeader();

        sbyte[] myRiffChunkHeaderAsByteArray   = new sbyte[12];
        sbyte[] myFormatChunkHeaderAsByteArray = new sbyte[8];
        sbyte[] myWaveHeaderAsByteArray        = new sbyte[16];
        sbyte[] myDataChunkHeaderAsByteArray   = new sbyte[8];

        long           total_unpacked_samples = 0, total_samples; // was uint32_t in C
        int            num_channels, bps;
        WavpackContext wpc = new WavpackContext();

        System.IO.FileStream   fistream;
        System.IO.FileStream   fostream;
        System.IO.BinaryReader in_Renamed;
        long start, end;

        System.String inputWVFile;

        if (args.Length == 0)
        {
            inputWVFile = "input.wv";
        }
        else
        {
            inputWVFile = args[0];
        }

        try
        {
            fistream = new System.IO.FileStream(inputWVFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BufferedStream bstream = new System.IO.BufferedStream(fistream, 16384);
            in_Renamed = new System.IO.BinaryReader(bstream);
            wpc        = WavPackUtils.WavpackOpenFileInput(in_Renamed);
        }
        catch (System.IO.FileNotFoundException)
        {
            System.Console.Error.WriteLine("Input file not found");
            System.Environment.Exit(1);
        }
        catch (System.IO.DirectoryNotFoundException)
        {
            System.Console.Error.WriteLine("Input file not found - invalid directory");
            System.Environment.Exit(1);
        }

        if (wpc.error)
        {
            System.Console.Error.WriteLine("Sorry an error has occured");
            System.Console.Error.WriteLine(wpc.error_message);
            System.Environment.Exit(1);
        }

        num_channels = WavPackUtils.WavpackGetReducedChannels(wpc);

        System.Console.Out.WriteLine("The WavPack file has " + num_channels + " channels");

        total_samples = WavPackUtils.WavpackGetNumSamples(wpc);

        System.Console.Out.WriteLine("The WavPack file has " + total_samples + " samples");

        bps = WavPackUtils.WavpackGetBytesPerSample(wpc);

        System.Console.Out.WriteLine("The WavPack file has " + bps + " bytes per sample");

        myRiffChunkHeader.ckID[0] = 'R';
        myRiffChunkHeader.ckID[1] = 'I';
        myRiffChunkHeader.ckID[2] = 'F';
        myRiffChunkHeader.ckID[3] = 'F';

        myRiffChunkHeader.ckSize      = total_samples * num_channels * bps + 8 * 2 + 16 + 4;
        myRiffChunkHeader.formType[0] = 'W';
        myRiffChunkHeader.formType[1] = 'A';
        myRiffChunkHeader.formType[2] = 'V';
        myRiffChunkHeader.formType[3] = 'E';

        FormatChunkHeader.ckID[0] = 'f';
        FormatChunkHeader.ckID[1] = 'm';
        FormatChunkHeader.ckID[2] = 't';
        FormatChunkHeader.ckID[3] = ' ';

        FormatChunkHeader.ckSize = 16;

        WaveHeader.FormatTag      = 1;
        WaveHeader.NumChannels    = num_channels;
        WaveHeader.SampleRate     = WavPackUtils.WavpackGetSampleRate(wpc);
        WaveHeader.BlockAlign     = num_channels * bps;
        WaveHeader.BytesPerSecond = WaveHeader.SampleRate * WaveHeader.BlockAlign;
        WaveHeader.BitsPerSample  = WavPackUtils.WavpackGetBitsPerSample(wpc);

        DataChunkHeader.ckID[0] = 'd';
        DataChunkHeader.ckID[1] = 'a';
        DataChunkHeader.ckID[2] = 't';
        DataChunkHeader.ckID[3] = 'a';
        DataChunkHeader.ckSize  = total_samples * num_channels * bps;

        myRiffChunkHeaderAsByteArray[0] = (sbyte)myRiffChunkHeader.ckID[0];
        myRiffChunkHeaderAsByteArray[1] = (sbyte)myRiffChunkHeader.ckID[1];
        myRiffChunkHeaderAsByteArray[2] = (sbyte)myRiffChunkHeader.ckID[2];
        myRiffChunkHeaderAsByteArray[3] = (sbyte)myRiffChunkHeader.ckID[3];

        // swap endians here

        myRiffChunkHeaderAsByteArray[7] = (sbyte)(SupportClass.URShift(myRiffChunkHeader.ckSize, 24));
        myRiffChunkHeaderAsByteArray[6] = (sbyte)(SupportClass.URShift(myRiffChunkHeader.ckSize, 16));
        myRiffChunkHeaderAsByteArray[5] = (sbyte)(SupportClass.URShift(myRiffChunkHeader.ckSize, 8));
        myRiffChunkHeaderAsByteArray[4] = (sbyte)(myRiffChunkHeader.ckSize);

        myRiffChunkHeaderAsByteArray[8]  = (sbyte)myRiffChunkHeader.formType[0];
        myRiffChunkHeaderAsByteArray[9]  = (sbyte)myRiffChunkHeader.formType[1];
        myRiffChunkHeaderAsByteArray[10] = (sbyte)myRiffChunkHeader.formType[2];
        myRiffChunkHeaderAsByteArray[11] = (sbyte)myRiffChunkHeader.formType[3];

        myFormatChunkHeaderAsByteArray[0] = (sbyte)FormatChunkHeader.ckID[0];
        myFormatChunkHeaderAsByteArray[1] = (sbyte)FormatChunkHeader.ckID[1];
        myFormatChunkHeaderAsByteArray[2] = (sbyte)FormatChunkHeader.ckID[2];
        myFormatChunkHeaderAsByteArray[3] = (sbyte)FormatChunkHeader.ckID[3];

        // swap endians here
        myFormatChunkHeaderAsByteArray[7] = (sbyte)(SupportClass.URShift(FormatChunkHeader.ckSize, 24));
        myFormatChunkHeaderAsByteArray[6] = (sbyte)(SupportClass.URShift(FormatChunkHeader.ckSize, 16));
        myFormatChunkHeaderAsByteArray[5] = (sbyte)(SupportClass.URShift(FormatChunkHeader.ckSize, 8));
        myFormatChunkHeaderAsByteArray[4] = (sbyte)(FormatChunkHeader.ckSize);

        // swap endians
        myWaveHeaderAsByteArray[1] = (sbyte)(SupportClass.URShift(WaveHeader.FormatTag, 8));
        myWaveHeaderAsByteArray[0] = (sbyte)(WaveHeader.FormatTag);

        // swap endians
        myWaveHeaderAsByteArray[3] = (sbyte)(SupportClass.URShift(WaveHeader.NumChannels, 8));
        myWaveHeaderAsByteArray[2] = (sbyte)WaveHeader.NumChannels;


        // swap endians
        myWaveHeaderAsByteArray[7] = (sbyte)(SupportClass.URShift(WaveHeader.SampleRate, 24));
        myWaveHeaderAsByteArray[6] = (sbyte)(SupportClass.URShift(WaveHeader.SampleRate, 16));
        myWaveHeaderAsByteArray[5] = (sbyte)(SupportClass.URShift(WaveHeader.SampleRate, 8));
        myWaveHeaderAsByteArray[4] = (sbyte)(WaveHeader.SampleRate);

        // swap endians

        myWaveHeaderAsByteArray[11] = (sbyte)(SupportClass.URShift(WaveHeader.BytesPerSecond, 24));
        myWaveHeaderAsByteArray[10] = (sbyte)(SupportClass.URShift(WaveHeader.BytesPerSecond, 16));
        myWaveHeaderAsByteArray[9]  = (sbyte)(SupportClass.URShift(WaveHeader.BytesPerSecond, 8));
        myWaveHeaderAsByteArray[8]  = (sbyte)(WaveHeader.BytesPerSecond);

        // swap endians
        myWaveHeaderAsByteArray[13] = (sbyte)(SupportClass.URShift(WaveHeader.BlockAlign, 8));
        myWaveHeaderAsByteArray[12] = (sbyte)WaveHeader.BlockAlign;

        // swap endians
        myWaveHeaderAsByteArray[15] = (sbyte)(SupportClass.URShift(WaveHeader.BitsPerSample, 8));
        myWaveHeaderAsByteArray[14] = (sbyte)WaveHeader.BitsPerSample;

        myDataChunkHeaderAsByteArray[0] = (sbyte)DataChunkHeader.ckID[0];
        myDataChunkHeaderAsByteArray[1] = (sbyte)DataChunkHeader.ckID[1];
        myDataChunkHeaderAsByteArray[2] = (sbyte)DataChunkHeader.ckID[2];
        myDataChunkHeaderAsByteArray[3] = (sbyte)DataChunkHeader.ckID[3];

        // swap endians

        myDataChunkHeaderAsByteArray[7] = (sbyte)(SupportClass.URShift(DataChunkHeader.ckSize, 24));
        myDataChunkHeaderAsByteArray[6] = (sbyte)(SupportClass.URShift(DataChunkHeader.ckSize, 16));
        myDataChunkHeaderAsByteArray[5] = (sbyte)(SupportClass.URShift(DataChunkHeader.ckSize, 8));
        myDataChunkHeaderAsByteArray[4] = (sbyte)DataChunkHeader.ckSize;

        try
        {
            fostream = new System.IO.FileStream("output.wav", System.IO.FileMode.Create);
            SupportClass.WriteOutput(fostream, myRiffChunkHeaderAsByteArray);
            SupportClass.WriteOutput(fostream, myFormatChunkHeaderAsByteArray);
            SupportClass.WriteOutput(fostream, myWaveHeaderAsByteArray);
            SupportClass.WriteOutput(fostream, myDataChunkHeaderAsByteArray);

            start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            while (true)
            {
                long samples_unpacked;                 // was uint32_t in C

                samples_unpacked = WavPackUtils.WavpackUnpackSamples(wpc, temp_buffer, Defines.SAMPLE_BUFFER_SIZE / num_channels);

                total_unpacked_samples += samples_unpacked;

                if (samples_unpacked > 0)
                {
                    samples_unpacked = samples_unpacked * num_channels;

                    pcm_buffer = format_samples(bps, temp_buffer, samples_unpacked);
                    fostream.Write(pcm_buffer, 0, (int)samples_unpacked * bps);
                }

                if (samples_unpacked == 0)
                {
                    break;
                }
            }             // end of while

            end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            System.Console.Out.WriteLine(end - start + " milli seconds to process WavPack file in main loop");
        }
        catch (System.Exception e)
        {
            System.Console.Error.WriteLine("Error when writing wav file, sorry: ");
            SupportClass.WriteStackTrace(e, Console.Error);
            System.Environment.Exit(1);
        }

        if ((WavPackUtils.WavpackGetNumSamples(wpc) != -1) && (total_unpacked_samples != WavPackUtils.WavpackGetNumSamples(wpc)))
        {
            System.Console.Error.WriteLine("Incorrect number of samples");
            System.Environment.Exit(1);
        }

        if (WavPackUtils.WavpackGetNumErrors(wpc) > 0)
        {
            System.Console.Error.WriteLine("CRC errors detected");
            System.Environment.Exit(1);
        }

        System.Environment.Exit(0);
    }
コード例 #57
0
ファイル: Report.cs プロジェクト: klot-git/scrum-factory
        /// <summary>
        /// Creates the report.
        /// </summary>
        public string CreateReportXAML(string serverUrl, ReportConfig config, bool toUseAsHtml = false)
        {
            if (config.StaticXAMLReport != null)
            {
                return(config.StaticXAMLReport);
            }

            // load the xslt template
            System.Xml.Xsl.XslCompiledTransform xslt = new System.Xml.Xsl.XslCompiledTransform();
            try {
                LoadTemplate(xslt, serverUrl, config.ReportGroup, config.ReportTemplate);
            }
            catch (System.Exception ex) {
                throw new ScrumFactory.Exceptions.ScrumFactoryException("Error_reading_report_template\n" + ex.Message);
            }

            // creates a buffer stream to write the report context in XML
            System.IO.BufferedStream     xmlBuffer      = new System.IO.BufferedStream(new System.IO.MemoryStream());
            System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
            writerSettings.CheckCharacters    = false;
            writerSettings.OmitXmlDeclaration = true;

            System.Xml.XmlWriter reportDataStream = System.Xml.XmlWriter.Create(xmlBuffer, writerSettings);

            // write XML start tag
            reportDataStream.WriteStartDocument();
            reportDataStream.WriteStartElement("ReportData");

            // create report context in XML
            CreateDefaultXMLContext(reportDataStream, writerSettings, serverUrl, config);

            // finish XML document
            reportDataStream.WriteEndDocument();
            reportDataStream.Flush();

            xmlBuffer.Seek(0, System.IO.SeekOrigin.Begin);
            // debug
            //System.IO.StreamReader s = new System.IO.StreamReader(xmlBuffer);
            //string ss = s.ReadToEnd();

            System.Xml.XmlReaderSettings readerSettings = new System.Xml.XmlReaderSettings();
            readerSettings.CheckCharacters = false;
            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(xmlBuffer, readerSettings);

            // creates a buffer stream to write the XAML flow document
            System.IO.StringWriter xamlBuffer = new System.IO.StringWriter();

            System.Xml.XmlWriter xamlWriter = System.Xml.XmlWriter.Create(xamlBuffer, writerSettings);

            // creates the flow document XMAL
            xslt.Transform(xmlReader, xamlWriter);

            // sets the flow document at the view
            var xaml = xamlBuffer.ToString();

            if (toUseAsHtml)
            {
                xaml = MDParser.ConvertToHTML(xaml);
            }
            else
            {
                xaml = MDParser.ConvertToXAML(xaml);
            }

            return(xaml);
        }
コード例 #58
0
        public IEnumerable <UserData> GetUserData(string input)
        {
            string lineOfText1 = "";
            string lineOfText2 = "";

            try {
                var filestream = new System.IO.FileStream("./app_data/" + input + ".txt",
                                                          System.IO.FileMode.Open,
                                                          System.IO.FileAccess.Read,
                                                          System.IO.FileShare.ReadWrite);


                System.IO.BufferedStream bs = new System.IO.BufferedStream(filestream);

                var             file = new System.IO.StreamReader(bs, System.Text.Encoding.UTF8, true, 128);
                List <UserData> lst  = new List <UserData>();
                while ((lineOfText1 = file.ReadLine()) != null)
                {
                    try
                    {
                        UserData obj = new UserData();
                        lineOfText2 = file.ReadLine();
                        Boolean found;
                        //Do something with the lineOfText(
                        if (!lineOfText2.Contains("profile") || deleteList.Contains(lineOfText2))
                        {
                            continue;
                        }
                        else
                        {
                            obj.name = lineOfText1.Replace("FileName", "");
                            obj.name = obj.name.Replace("Filename", "");
                            obj.name = obj.name.Replace("filename", "");
                            obj.name = obj.name.Replace(" ", "");
                            obj.name = obj.name.Replace(":", "");
                            var s = obj.name.Split("_");
                            obj.name = s[0];
                            obj.name = "https://www.facebook.com/" + obj.name;

                            string output = lineOfText2.Split(':', ',')[1];
                            obj.age = output.Replace("\"", "");
                            obj.age = "http://graph.facebook.com/" + obj.age + "/picture?type=large#/1039660954";

                            var lineOfText3 = file.ReadLine();
                            if (lineOfText3 != null)
                            {
                                obj.occupation = lineOfText1.Replace("FileName", "");
                                obj.occupation = obj.name.Replace("Filename", "");
                                obj.occupation = obj.occupation.Replace("filename", "");
                                obj.occupation = obj.occupation.Replace(" ", "");
                                obj.occupation = obj.occupation.Replace(":", "");
                                var s1 = obj.occupation.Split("_");
                                obj.occupation = s1[0];


                                obj.occupation = "https://www.facebook.com/" + obj.occupation;
                            }
                            var lineOfText4 = file.ReadLine();
                            if (lineOfText4 != null)
                            {
                                string output1 = lineOfText4.Split(':', ',')[1];
                                obj.species = output1.Replace("\"", "");
                                obj.species = "http://graph.facebook.com/" + obj.species + "/picture?type=large#/1039660954";
                            }

                            var lineOfText5 = file.ReadLine();
                            if (lineOfText5 != null)
                            {
                                obj.info1 = lineOfText5.Replace("FileName:", "");
                                obj.info1 = "https://www.facebook.com/" + obj.info1;
                            }
                            var lineOfText6 = file.ReadLine();
                            if (lineOfText6 != null)
                            {
                                string output2 = lineOfText6.Split(':', ',')[1];
                                obj.info2 = output2.Replace("\"", "");
                                obj.info2 = "http://graph.facebook.com/" + obj.info2 + "/picture?type=large#/1039660954";
                            }



                            //obj.species = obj.species.Replace("entity_id\":\"","");
                            lst.Add(obj);
                        }
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
                var rng = new Random();
                // lst =ShuffleList(lst);

                List <UserData> FINALDATA = new List <UserData>();
                Random          r         = new Random();
                for (int i = 0; i < 499; i++)
                {
                    FINALDATA.Insert(i, lst[r.Next(0, lst.Count)]);
                }
                // return lst.Take(1000);
                return(FINALDATA.Take(200));
            }
            catch (Exception ex)
            {
                return(null);
            }
            //return Enumerable.Range(1, 5).Select(index => new UserData
            //{
            //    name=  "http://facebook.com/e.melism",
            //     age= "http://graph.facebook.com/100001538625944/picture?type=normal#/100001538625944",
            //     species= "http://graph.facebook.com/1039660954/picture?type=large#/1039660954",
            //     occupation= "http://graph.facebook.com/1830700925/picture?type=normal#/1830700925"
            //});
        }