9 people like it.

EnumerableStream

A lazy enumerable/stream of bytes.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics.Contracts
open System.IO
open System.Runtime.Serialization
open System.Runtime.Serialization.Formatters.Binary
open System.Text

/// Initializes a new instance of the SeqStream class.
/// <see href="http://extensia.codeplex.com"/>
type SeqStream(data:seq<byte>) =
  inherit Stream()
  do Contract.Requires(data <> null)
  let d = data.GetEnumerator()

  override this.CanRead = true
  override this.CanSeek = false
  override this.CanWrite = false
  override this.Flush() = ()
  override this.Length = data |> Seq.length |> int64
  override this.Position with get() = raise (NotSupportedException())
                         and set(v) = raise (NotSupportedException())
  override this.Seek(offset, origin) = raise (NotSupportedException())
  override this.SetLength(value) = raise (NotSupportedException())
  override this.Write(buffer, offset, count) = raise (NotSupportedException())
  override this.Dispose(disposing) = d.Dispose()
                                     base.Dispose(disposing)
  override this.Read(buffer, offset, count) =
    Contract.Requires(buffer <> null)
    Contract.Requires(offset >= 0)
    Contract.Requires(count > 0)
    Contract.Requires(offset + count <= buffer.Length)

    let rec loop bytesRead =
      if d.MoveNext() && bytesRead < count
        then
          buffer.[bytesRead + offset] <- d.Current
          loop (bytesRead + 1)
        else bytesRead
    loop 0
    
  /// Returns the SeqStream as a UTF8 encoded string.
  override this.ToString() = Encoding.UTF8.GetString(data |> Seq.toArray)

  /// An empty SeqStream.
  static member Empty = new SeqStream(Seq.empty<byte>)

  /// Converts a string into a SeqStream.
  static member FromString(s:string) = new SeqStream(Encoding.UTF8.GetBytes(s))

  /// Converts a stream into a SeqStream.
  static member FromStream(stream:Stream, ?bufferSize) =
    let bufferSize = defaultArg bufferSize 1024
    Contract.Requires(stream <> null)
    Contract.Requires(bufferSize > 0)

    let buffer = Array.zeroCreate bufferSize
    let count = ref 0
    count := stream.Read(buffer, 0, buffer.Length)
    let bytes = seq {
      while !count > 0 do
        for i in [0..(!count-1)] do yield buffer.[i]
        count := stream.Read(buffer, 0, buffer.Length) }
    new SeqStream(bytes)

  /// Converts a FileInfo into a SeqStream.
  static member FromFileInfo(file:FileInfo) =
    Contract.Requires(file <> null)
    use stream = file.OpenRead()
    SeqStream.FromStream(stream, int stream.Length)

  /// Converts an object to a SeqStream.
  static member FromObject(ob) =
    let formatter = BinaryFormatter()
    use stream = new MemoryStream()
    try
      formatter.Serialize(stream, ob)
      SeqStream.FromStream(stream)
    with :? SerializationException as e -> SeqStream.Empty
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Diagnostics
namespace System.Diagnostics.Contracts
namespace System.IO
namespace System.Runtime
namespace System.Runtime.Serialization
namespace System.Runtime.Serialization.Formatters
namespace System.Runtime.Serialization.Formatters.Binary
namespace System.Text
Multiple items
type SeqStream =
  inherit Stream
  new : data:seq<byte> -> SeqStream
  override Dispose : disposing:bool -> unit
  override Flush : unit -> unit
  override Read : buffer:byte [] * offset:int * count:int -> int
  override Seek : offset:int64 * origin:SeekOrigin -> int64
  override SetLength : value:int64 -> unit
  override ToString : unit -> string
  override Write : buffer:byte [] * offset:int * count:int -> unit
  override CanRead : bool
  ...

Full name: Script.SeqStream


 Initializes a new instance of the SeqStream class.
 <see href="http://extensia.codeplex.com"/>


--------------------
new : data:seq<byte> -> SeqStream
val data : seq<byte>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
Multiple items
type Stream =
  inherit MarshalByRefObject
  member BeginRead : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginWrite : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanTimeout : bool
  member CanWrite : bool
  member Close : unit -> unit
  member CopyTo : destination:Stream -> unit + 1 overload
  member Dispose : unit -> unit
  member EndRead : asyncResult:IAsyncResult -> int
  ...

Full name: System.IO.Stream

--------------------
Stream() : unit
type Contract =
  static member Assert : condition:bool -> unit + 1 overload
  static member Assume : condition:bool -> unit + 1 overload
  static member EndContractBlock : unit -> unit
  static member Ensures : condition:bool -> unit + 1 overload
  static member EnsuresOnThrow<'TException> : condition:bool -> unit + 1 overload
  static member Exists<'T> : collection:IEnumerable<'T> * predicate:Predicate<'T> -> bool + 1 overload
  static member ForAll<'T> : collection:IEnumerable<'T> * predicate:Predicate<'T> -> bool + 1 overload
  static member Invariant : condition:bool -> unit + 1 overload
  static member OldValue<'T> : value:'T -> 'T
  static member Requires : condition:bool -> unit + 3 overloads
  ...

Full name: System.Diagnostics.Contracts.Contract
Contract.Requires<'TException (requires 'TException :> exn)>(condition: bool) : unit
Contract.Requires(condition: bool) : unit
Contract.Requires<'TException (requires 'TException :> exn)>(condition: bool, userMessage: string) : unit
Contract.Requires(condition: bool, userMessage: string) : unit
val d : IEnumerator<byte>
IEnumerable.GetEnumerator() : IEnumerator<byte>
val this : SeqStream
override SeqStream.CanRead : bool

Full name: Script.SeqStream.CanRead
override SeqStream.CanSeek : bool

Full name: Script.SeqStream.CanSeek
override SeqStream.CanWrite : bool

Full name: Script.SeqStream.CanWrite
override SeqStream.Flush : unit -> unit

Full name: Script.SeqStream.Flush
override SeqStream.Length : int64

Full name: Script.SeqStream.Length
module Seq

from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int64

--------------------
type int64 = Int64

Full name: Microsoft.FSharp.Core.int64

--------------------
type int64<'Measure> = int64

Full name: Microsoft.FSharp.Core.int64<_>
override SeqStream.Position : int64 with set

Full name: Script.SeqStream.Position
val raise : exn:Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type NotSupportedException =
  inherit SystemException
  new : unit -> NotSupportedException + 2 overloads

Full name: System.NotSupportedException

--------------------
NotSupportedException() : unit
NotSupportedException(message: string) : unit
NotSupportedException(message: string, innerException: exn) : unit
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val v : int64
override SeqStream.Seek : offset:int64 * origin:SeekOrigin -> int64

Full name: Script.SeqStream.Seek
val offset : int64
val origin : SeekOrigin
override SeqStream.SetLength : value:int64 -> unit

Full name: Script.SeqStream.SetLength
val value : int64
override SeqStream.Write : buffer:byte [] * offset:int * count:int -> unit

Full name: Script.SeqStream.Write
val buffer : byte []
val offset : int
val count : int
override SeqStream.Dispose : disposing:bool -> unit

Full name: Script.SeqStream.Dispose
val disposing : bool
IDisposable.Dispose() : unit
override SeqStream.Read : buffer:byte [] * offset:int * count:int -> int

Full name: Script.SeqStream.Read
property Array.Length: int
val loop : (int -> int)
val bytesRead : int
IEnumerator.MoveNext() : bool
property IEnumerator.Current: byte
override SeqStream.ToString : unit -> string

Full name: Script.SeqStream.ToString


 Returns the SeqStream as a UTF8 encoded string.
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
property Encoding.UTF8: Encoding
Encoding.GetString(bytes: byte []) : string
Encoding.GetString(bytes: byte [], index: int, count: int) : string
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
static member SeqStream.Empty : SeqStream

Full name: Script.SeqStream.Empty


 An empty SeqStream.
val empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty
static member SeqStream.FromString : s:string -> SeqStream

Full name: Script.SeqStream.FromString


 Converts a string into a SeqStream.
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
Encoding.GetBytes(s: string) : byte []
Encoding.GetBytes(chars: char []) : byte []
Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
static member SeqStream.FromStream : stream:Stream * ?bufferSize:int -> SeqStream

Full name: Script.SeqStream.FromStream


 Converts a stream into a SeqStream.
val stream : Stream
val bufferSize : int option
val bufferSize : int
val defaultArg : arg:'T option -> defaultValue:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.defaultArg
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
val count : int ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
Stream.Read(buffer: byte [], offset: int, count: int) : int
val bytes : seq<byte>
val i : int
static member SeqStream.FromFileInfo : file:FileInfo -> SeqStream

Full name: Script.SeqStream.FromFileInfo


 Converts a FileInfo into a SeqStream.
val file : FileInfo
Multiple items
type FileInfo =
  inherit FileSystemInfo
  new : fileName:string -> FileInfo
  member AppendText : unit -> StreamWriter
  member CopyTo : destFileName:string -> FileInfo + 1 overload
  member Create : unit -> FileStream
  member CreateText : unit -> StreamWriter
  member Decrypt : unit -> unit
  member Delete : unit -> unit
  member Directory : DirectoryInfo
  member DirectoryName : string
  member Encrypt : unit -> unit
  ...

Full name: System.IO.FileInfo

--------------------
FileInfo(fileName: string) : unit
val stream : FileStream
FileInfo.OpenRead() : FileStream
static member SeqStream.FromStream : stream:Stream * ?bufferSize:int -> SeqStream


 Converts a stream into a SeqStream.
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
property FileStream.Length: int64
static member SeqStream.FromObject : ob:'a -> SeqStream

Full name: Script.SeqStream.FromObject


 Converts an object to a SeqStream.
val ob : 'a
val formatter : BinaryFormatter
Multiple items
type BinaryFormatter =
  new : unit -> BinaryFormatter + 1 overload
  member AssemblyFormat : FormatterAssemblyStyle with get, set
  member Binder : SerializationBinder with get, set
  member Context : StreamingContext with get, set
  member Deserialize : serializationStream:Stream -> obj + 1 overload
  member DeserializeMethodResponse : serializationStream:Stream * handler:HeaderHandler * methodCallMessage:IMethodCallMessage -> obj
  member FilterLevel : TypeFilterLevel with get, set
  member Serialize : serializationStream:Stream * graph:obj -> unit + 1 overload
  member SurrogateSelector : ISurrogateSelector with get, set
  member TypeFormat : FormatterTypeStyle with get, set
  ...

Full name: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

--------------------
BinaryFormatter() : unit
BinaryFormatter(selector: ISurrogateSelector, context: StreamingContext) : unit
val stream : MemoryStream
Multiple items
type MemoryStream =
  inherit Stream
  new : unit -> MemoryStream + 6 overloads
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member Capacity : int with get, set
  member Flush : unit -> unit
  member GetBuffer : unit -> byte[]
  member Length : int64
  member Position : int64 with get, set
  member Read : buffer:byte[] * offset:int * count:int -> int
  ...

Full name: System.IO.MemoryStream

--------------------
MemoryStream() : unit
MemoryStream(capacity: int) : unit
MemoryStream(buffer: byte []) : unit
MemoryStream(buffer: byte [], writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool, publiclyVisible: bool) : unit
BinaryFormatter.Serialize(serializationStream: Stream, graph: obj) : unit
BinaryFormatter.Serialize(serializationStream: Stream, graph: obj, headers: Runtime.Remoting.Messaging.Header []) : unit
Multiple items
type SerializationException =
  inherit SystemException
  new : unit -> SerializationException + 2 overloads

Full name: System.Runtime.Serialization.SerializationException

--------------------
SerializationException() : unit
SerializationException(message: string) : unit
SerializationException(message: string, innerException: exn) : unit
val e : SerializationException
property SeqStream.Empty: SeqStream


 An empty SeqStream.

More information

Link:http://fssnip.net/1s
Posted:13 years ago
Author:Ryan Riley
Tags: sequence , stream