Skip to content

Commit

Permalink
ПотокВПамяти и начало рефакторинга потоков
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBeaver committed Dec 16, 2016
1 parent e8378e5 commit 1b2358c
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 556 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public FileStreamContext(string fileName, FileStream openedStream)
FileName = fileName;
_underlyingStream = openedStream;
}


public bool IsReadOnly => CanWrite;

/// <summary>
///
/// Признак доступности записи в поток.
Expand Down
41 changes: 12 additions & 29 deletions src/ScriptEngine.HostedScript/Library/Binary/GenericStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace ScriptEngine.HostedScript.Library.Binary
internal interface IStreamWrapper
{
Stream GetUnderlyingStream();

bool IsReadOnly { get; }
}

/// <summary>
Expand All @@ -18,13 +20,15 @@ internal interface IStreamWrapper
[ContextClass("Поток", "Stream")]
public class GenericStream : AutoContext<GenericStream>, IDisposable, IStreamWrapper
{

private bool _isReadOnly;
private readonly Stream _underlyingStream;
private readonly bool _isReadOnly;

private readonly GenericStreamImpl _commonImpl;

public GenericStream(Stream underlyingStream)
{
_underlyingStream = underlyingStream;
_commonImpl = new GenericStreamImpl(_underlyingStream);
_isReadOnly = false;
}

Expand All @@ -33,7 +37,9 @@ public GenericStream(Stream underlyingStream, bool readOnly)
_underlyingStream = underlyingStream;
_isReadOnly = readOnly;
}


public bool IsReadOnly => CanWrite;

/// <summary>
///
/// Признак доступности записи в поток.
Expand Down Expand Up @@ -90,8 +96,7 @@ public void Close()
[ContextMethod("Записать", "Write")]
public void Write(BinaryDataBuffer buffer, int positionInBuffer, int number)
{
buffer.ThrowIfReadonly();
_underlyingStream.Write(buffer.Bytes, positionInBuffer, number);
_commonImpl.Write(buffer, positionInBuffer, number);
}


Expand All @@ -109,15 +114,7 @@ public void Write(BinaryDataBuffer buffer, int positionInBuffer, int number)
[ContextMethod("КопироватьВ", "CopyTo")]
public void CopyTo(IValue targetStream, int bufferSize = 0)
{
IStreamWrapper sw = targetStream.GetRawValue() as IStreamWrapper;
if(sw == null)
throw RuntimeException.InvalidArgumentType("targetStream");

var stream = sw.GetUnderlyingStream();
if(bufferSize == 0)
_underlyingStream.CopyTo(stream);
else
_underlyingStream.CopyTo(stream, bufferSize);
_commonImpl.CopyTo(targetStream, bufferSize);
}

/// <summary>
Expand All @@ -135,21 +132,7 @@ public void CopyTo(IValue targetStream, int bufferSize = 0)
[ContextMethod("Перейти", "Seek")]
public long Seek(int offset, StreamPositionEnum initialPosition = StreamPositionEnum.Begin)
{
SeekOrigin origin;
switch (initialPosition)
{
case StreamPositionEnum.End:
origin = SeekOrigin.End;
break;
case StreamPositionEnum.Current:
origin = SeekOrigin.Current;
break;
default:
origin = SeekOrigin.Begin;
break;
}

return _underlyingStream.Seek(offset, origin);
return _commonImpl.Seek(offset, initialPosition);
}


Expand Down
60 changes: 60 additions & 0 deletions src/ScriptEngine.HostedScript/Library/Binary/GenericStreamImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using ScriptEngine.Machine;

namespace ScriptEngine.HostedScript.Library.Binary
{
class GenericStreamImpl
{
Stream _underlyingStream;

public GenericStreamImpl(Stream stream)
{
_underlyingStream = stream;
}

public void Write(BinaryDataBuffer buffer, int positionInBuffer, int number)
{
buffer.ThrowIfReadonly();
_underlyingStream.Write(buffer.Bytes, positionInBuffer, number);
}

public void CopyTo(IValue targetStream, int bufferSize = 0)
{
IStreamWrapper sw = targetStream.GetRawValue() as IStreamWrapper;
if (sw == null)
throw RuntimeException.InvalidArgumentType("targetStream");

var stream = sw.GetUnderlyingStream();
if (bufferSize == 0)
_underlyingStream.CopyTo(stream);
else
_underlyingStream.CopyTo(stream, bufferSize);
}

public long Seek(int offset, StreamPositionEnum initialPosition = StreamPositionEnum.Begin)
{
SeekOrigin origin;
switch (initialPosition)
{
case StreamPositionEnum.End:
origin = SeekOrigin.End;
break;
case StreamPositionEnum.Current:
origin = SeekOrigin.Current;
break;
default:
origin = SeekOrigin.Begin;
break;
}

return _underlyingStream.Seek(offset, origin);
}

}
}
Loading

0 comments on commit 1b2358c

Please sign in to comment.