<commit_before>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace WInterop.Com;

/// <summary>
///  Stream that wraps a COM <see cref="IStream"/>
/// </summary>
public class ComStream : System.IO.Stream
{
    private readonly bool _ownsStream;
    private Stream _stream;

    public unsafe ComStream(IStream* stream, bool ownsStream = true)
        : this(new Stream(stream), ownsStream) { }

    /// <summary>
    ///  Construct a Stream wrapper around a <see cref="WInterop.Com.Stream"/> object.
    /// </summary>
    /// <param name="stream">The COM stream to wrap.</param>
    /// <param name="ownsStream">If true (default), will release the <see cref="WInterop.Com.Stream"/> object when disposed.</param>
    public ComStream(Stream stream, bool ownsStream = true)
    {
        if (stream.IsNull)
            throw new ArgumentNullException(nameof(stream));

        _stream = stream;
        _ownsStream = ownsStream;
        using var stats = stream.Stat(StatFlag.NoName);
        StorageType = stats.StorageType;
        StorageMode = stats.Mode;
        ClassId = stats.ClassId;
        // new Guid("0000000C-0000-0000-C000-000000000046");
    }

    public StorageType StorageType { get; }

    public StorageMode StorageMode { get; }

    public Guid ClassId { get; }

    public Stream Stream
        => Stream.IsNull ? throw new ObjectDisposedException(nameof(ComStream)) : Stream;

    public override bool CanRead
        // Can read is the default, only can't if in Write only mode
        => (StorageMode & StorageMode.Write) == 0;

    public override bool CanSeek => true;

    public override bool CanWrite
        => (StorageMode & StorageMode.Write) != 0 || (StorageMode & StorageMode.ReadWrite) != 0;

    public override long Length
        => checked((long)Stream.Stat(StatFlag.NoName).Size);

    public override unsafe long Position
    {<commit_msg>Fix bug in ComStream<commit_after>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace WInterop.Com;

/// <summary>
///  Stream that wraps a COM <see cref="IStream"/>
/// </summary>
public class ComStream : System.IO.Stream
{
    private readonly bool _ownsStream;
    private Stream _stream;

    public unsafe ComStream(IStream* stream, bool ownsStream = true)
        : this(new Stream(stream), ownsStream) { }

    /// <summary>
    ///  Construct a Stream wrapper around a <see cref="WInterop.Com.Stream"/> object.
    /// </summary>
    /// <param name="stream">The COM stream to wrap.</param>
    /// <param name="ownsStream">If true (default), will release the <see cref="WInterop.Com.Stream"/> object when disposed.</param>
    public ComStream(Stream stream, bool ownsStream = true)
    {
        if (stream.IsNull)
            throw new ArgumentNullException(nameof(stream));

        _stream = stream;
        _ownsStream = ownsStream;
        using var stats = stream.Stat(StatFlag.NoName);
        StorageType = stats.StorageType;
        StorageMode = stats.Mode;
        ClassId = stats.ClassId;
        // new Guid("0000000C-0000-0000-C000-000000000046");
    }

    public StorageType StorageType { get; }

    public StorageMode StorageMode { get; }

    public Guid ClassId { get; }

    public Stream Stream
        => _stream.IsNull ? throw new ObjectDisposedException(nameof(ComStream)) : _stream;

    public override bool CanRead
        // Can read is the default, only can't if in Write only mode
        => (StorageMode & StorageMode.Write) == 0;

    public override bool CanSeek => true;

    public override bool CanWrite
        => (StorageMode & StorageMode.Write) != 0 || (StorageMode & StorageMode.ReadWrite) != 0;

    public override long Length
        => checked((long)Stream.Stat(StatFlag.NoName).Size);

    public override unsafe long Position
    {
{'fraction_non_alphanumeric': 0.11236518685728618, 'fraction_numerical': 0.01655379984951091, 'mean_word_length': 2.8568665377176017, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7233302', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1076, 'n_words': 353}