Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Contracts/3.2/StreamedObject.cs @ 2471

Last change on this file since 2471 was 2117, checked in by svonolfe, 15 years ago

Streaming of Jobs and JobsResults directly from/to the DB (#680)

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using System.Runtime.Serialization.Formatters.Binary;
7using System.Runtime.Serialization;
8
9namespace HeuristicLab.Hive.Contracts {
10  [DataContract]
11  public class StreamedObject<T> : Stream {
12    private T encapsulatedObject;
13
14    private Stream stream =
15      new MemoryStream();
16
17    public StreamedObject(T encapsulatedObject) {
18      this.encapsulatedObject = encapsulatedObject;
19
20      BinaryFormatter formatter =
21        new BinaryFormatter();
22      formatter.Serialize(this.stream, this.encapsulatedObject);
23
24      this.stream.Seek(0, SeekOrigin.Begin);
25    }
26
27    public T EncapsulatedObject {
28      get {
29        return this.encapsulatedObject;
30      }
31    }
32
33    public override bool CanRead {
34      get { return this.stream.CanRead; }
35    }
36
37    public override bool CanSeek {
38      get { throw new NotImplementedException(); }
39    }
40
41    public override bool CanWrite {
42      get { throw new NotImplementedException(); }
43    }
44
45    public override void Flush() {
46      throw new NotImplementedException();
47    }
48
49    public override long Length {
50      get { return this.stream.Length; }
51    }
52
53    public override long Position {
54      get {
55        return this.stream.Position;
56      }
57      set {
58        if(this.stream.Position != value)
59          throw new NotImplementedException();
60      }
61    }
62
63    public override int Read(byte[] buffer, int offset, int count) {
64      int read = this.stream.Read(buffer, offset, count);
65
66      return read;
67    }
68
69    public override long Seek(long offset, SeekOrigin origin) {
70      throw new NotImplementedException();
71    }
72
73    public override void SetLength(long value) {
74      throw new NotImplementedException();
75    }
76
77    public override void Write(byte[] buffer, int offset, int count) {
78      throw new NotImplementedException();
79    }
80
81    public override void Close() {
82      this.stream.Close();
83      base.Close();
84    }
85    protected override void Dispose(bool disposing) {
86      this.stream.Dispose();
87      base.Dispose(disposing);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.