public override int Read(byte[] array, int offset, int count) { if (_CacheBufferIndex != null) { int index = (int)(this.Position / CachedFileBufferManager.BufferUnitSize); int offsetInBuffer = (int)(this.Position % CachedFileBufferManager.BufferUnitSize); if (_CacheBufferIndex[index] != null) { CachedFileBufferManager.Buffer buffer = _CacheBufferIndex[index]; byte[] data = _CacheBufferIndex[index].Data; if (data != null) { if (data.Length - offsetInBuffer - buffer.HeadPosition >= count) { //Data inside one block. Array.Copy(data, offsetInBuffer + buffer.HeadPosition, array, offset, count); Position += count; return(count); } else { if (buffer.HeadPosition > 0 || index == _CacheBufferIndex.Length - 1 || count < 10 * 1024) { //Data outside one block. but the index is not the beginning of the block //Or it is the last block //Or small count less than 10K //Return part of the data int len = data.Length - offsetInBuffer - buffer.HeadPosition; Array.Copy(data, offsetInBuffer + buffer.HeadPosition, array, offset, len); Position += len; return(len); } } } } if ((_Type == CachedType.Dynamic || _Type == CachedType.Full) && CacheAllFinished) { OffsetLength ol = _LastOLWithBuffer[index]; if (offsetInBuffer >= ol.Offset && offsetInBuffer <= ol.Offset + ol.Length) { ol.Length += count - (ol.Offset + ol.Length - offsetInBuffer); } else { ol.Offset = offsetInBuffer; ol.Length = count; } _LastOLWithBuffer[index] = ol; if (ol.Length >= MinCacheLength) { //Cached if (LoadToBuffer(count, Position)) { return(Read(array, offset, count)); } } } //return data from file base.Seek(Position, SeekOrigin.Begin); int retLen = base.Read(array, offset, count); Position += retLen; return(retLen); } return(base.Read(array, offset, count)); }
/// <summary> /// Read and output to array /// </summary> /// <param name="array">array</param> /// <param name="offsetInArray">offset in array</param> /// <param name="offset">offset in file</param> /// <param name="count">count want to read</param> /// <returns>actual read count</returns> public int Read(out byte[] array, out int offsetInArray, int count) { if (_CacheBufferIndex != null) { int index = (int)(this.Position / CachedFileBufferManager.BufferUnitSize); int offsetInBuffer = (int)(this.Position % CachedFileBufferManager.BufferUnitSize); if (_CacheBufferIndex[index] != null) { CachedFileBufferManager.Buffer buffer = _CacheBufferIndex[index]; byte[] data = _CacheBufferIndex[index].Data; if (data != null) { if (data.Length - offsetInBuffer - buffer.HeadPosition >= count) { array = data; offsetInArray = offsetInBuffer + buffer.HeadPosition; Position += count; return(count); } else { if (buffer.HeadPosition > 0 || index == _CacheBufferIndex.Length - 1 || count < 10 * 1024) { //Data outside one block. but the index is not the beginning of the block //Or it is the last block //Or small count less than 10K //Return part of the data array = new byte[count]; Stream.ReadToBuf(this, array, 0, ref count); offsetInArray = 0; return(count); } } } } if ((_Type == CachedType.Dynamic || _Type == CachedType.Full) && CacheAllFinished) { OffsetLength ol = _LastOLWithBuffer[index]; if (offsetInBuffer >= ol.Offset && offsetInBuffer <= ol.Offset + ol.Length) { ol.Length += count - (ol.Offset + ol.Length - offsetInBuffer); } else { ol.Offset = offsetInBuffer; ol.Length = count; } _LastOLWithBuffer[index] = ol; if (ol.Length >= MinCacheLength) { //Cached if (LoadToBuffer(count, Position)) { return(Read(out array, out offsetInArray, count)); } } } //return data from file array = new byte[count]; Stream.ReadToBuf(this, array, 0, ref count); offsetInArray = 0; return(count); } array = new byte[count]; offsetInArray = 0; Stream.ReadToBuf(this, array, 0, ref count); return(count); }