Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Contracts/3.2/MultiStream.cs @ 3068

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

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

File size: 2.6 KB
Line 
1/* taken from:
2 * http://www.clevercomponents.com/articles/article017/multistreamcs.asp
3 * */
4
5using System;
6using System.IO;
7using System.Collections;
8
9namespace HeuristicLab.Hive.Contracts {
10  public class MultiStream : Stream {
11    ArrayList streamList = new ArrayList();
12    long position = 0;
13    public override bool CanRead {
14      get { return true; }
15    }
16
17    public override bool CanSeek {
18      get { return true; }
19    }
20
21    public override bool CanWrite {
22      get { return false; }
23    }
24
25    public override long Length {
26      get {
27        long result = 0;
28        foreach (Stream stream in streamList) {
29          result += stream.Length;
30        }
31        return result;
32      }
33    }
34
35    public override long Position {
36      get { return position; }
37      set { Seek(value, SeekOrigin.Begin); }
38    }
39
40    public override void Flush() { }
41
42    public override long Seek(long offset, SeekOrigin origin) {
43      long len = Length;
44      switch (origin) {
45        case SeekOrigin.Begin:
46          position = offset;
47          break;
48        case SeekOrigin.Current:
49          position += offset;
50          break;
51        case SeekOrigin.End:
52          position = len - offset;
53          break;
54      }
55      if (position > len) {
56        position = len;
57      } else if (position < 0) {
58        position = 0;
59      }
60      return position;
61    }
62
63    public override void SetLength(long value) { }
64
65    public void AddStream(Stream stream) {
66      streamList.Add(stream);
67    }
68
69    public override int Read(byte[] buffer, int offset, int count) {
70      long len = 0;
71      int result = 0;
72      int buf_pos = offset;
73      int bytesRead;
74      foreach (Stream stream in streamList) {
75        if (position < (len + stream.Length)) {
76          stream.Position = position - len;
77          bytesRead = stream.Read(buffer, buf_pos, count);
78          result += bytesRead;
79          buf_pos += bytesRead;
80          position += bytesRead;
81          if (bytesRead < count) {
82            count -= bytesRead;
83          } else {
84            break;
85          }
86        }
87        len += stream.Length;
88      }
89      return result;
90    }
91
92    public override void Write(byte[] buffer, int offset, int count) {
93    }
94
95    public override void Close() {
96      foreach (Stream s in this.streamList) {
97        s.Close();
98      }
99     
100      base.Close();
101    }
102
103    protected override void Dispose(bool disposing) {
104      if (!disposing) {
105        foreach (Stream s in this.streamList) {
106          s.Dispose();
107        }
108      }
109     
110      base.Dispose(disposing);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.