示例#1
0
        public Segment Read(Stream stream, int segmentId)
        {
            byte[] buffer = new byte[configuration.SegmentSize];

            Segment segment = null;

            int readResult = stream.Read(buffer, 0, buffer.Length);

            if (readResult > 0)
            {
                // Adjust buffer size to the stream length in case if we almost reached its end
                if (readResult < configuration.SegmentSize)
                {
                    buffer = buffer.Take(readResult).ToArray();
                }

                segment = dataSegmenter.GetSegment(segmentId, buffer);
            }
            return(segment);
        }
        private void DoBlockByBlockWriting()
        {
            // Process while there is still remaining data in the stream
            while (true)
            {
                // Generate segment
                Segment segment = null;

                lock (source)
                {
                    byte[] buffer = new byte[configuration.SegmentSize];

                    int readResult = source.Read(buffer, 0, buffer.Length);

                    if (readResult > 0)
                    {
                        // Adjust buffer size to the stream length in case if we almost reached the end of it
                        if (readResult < configuration.SegmentSize)
                        {
                            buffer = buffer.Take(readResult).ToArray();
                        }

                        segment = dataSegmenter.GetSegment(++segmentId, buffer);
                    }
                    else
                    {
                        break;
                    }
                }

                // Compress Segment
                lock (segmentStreamWriter)
                {
                    segmentStreamWriter.Write(destination, segment);
                }
            }
        }