/// <summary> /// Loads a segment from the binary data beginning at the specified position. /// </summary> /// <param name="fileId">File record identifier.</param> /// <param name="position">Starting position of the segment.</param> /// <param name="count">Number of bytes to load.</param> /// <returns>Byte array containing the requested number of bytes (or less if there is not enough in the binary data).</returns> protected internal static byte[] LoadBinaryFragment(int fileId, long position, int count) { var ctx = GetBlobStorageContext(fileId); var provider = ctx.Provider; if (provider == BuiltInProvider) { return(BuiltInBlobProvider.ReadRandom(ctx, position, count)); } var realCount = Convert.ToInt32(Math.Min(ctx.Length - position, count)); var bytes = new byte[realCount]; using (var stream = provider.GetStreamForRead(ctx)) { stream.Seek(position, SeekOrigin.Begin); stream.Read(bytes, 0, realCount); } return(bytes); }
/// <summary> /// Loads a segment from the binary data beginning at the specified position. /// </summary> /// <param name="fileId">File record identifier.</param> /// <param name="position">Starting position of the segment.</param> /// <param name="count">Number of bytes to load.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A Task that represents the asynchronous operation containing /// a byte array containing the requested number of bytes (or less if there is not enough in the binary data).</returns> protected internal static async Task <byte[]> LoadBinaryFragmentAsync(int fileId, long position, int count, CancellationToken cancellationToken) { var ctx = await GetBlobStorageContextAsync(fileId, cancellationToken).ConfigureAwait(false); var provider = ctx.Provider; if (provider == BuiltInProvider) { return(BuiltInBlobProvider.ReadRandom(ctx, position, count)); } var realCount = Convert.ToInt32(Math.Min(ctx.Length - position, count)); var bytes = new byte[realCount]; using (var stream = provider.GetStreamForRead(ctx)) { stream.Seek(position, SeekOrigin.Begin); stream.Read(bytes, 0, realCount); } return(bytes); }