Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Contracts/3.2/MultiStream.cs @ 4042

Last change on this file since 4042 was 4042, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

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