-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFStream.cs
177 lines (155 loc) · 5.97 KB
/
CFStream.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using BinaryTrees;
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is OpenMCDF - Compound Document Format library.
The Initial Developer of the Original Code is Federico Blaseotto.
*/
namespace OpenMcdf
{
/// <summary>
/// OLE structured storage <see cref="T:OpenMcdf.CFStream">stream</see> Object
/// It is contained inside a Storage object in a file-directory
/// relationship and indexed by its name.
/// </summary>
public class CFStream : CFItem
{
internal CFStream(CompoundFile sectorManager)
: base(sectorManager)
{
this.DirEntry = new DirectoryEntry(StgType.StgStream);
this.DirEntry.StgColor = StgColor.Black;
sectorManager.InsertNewDirectoryEntry(this.DirEntry);
}
internal CFStream(CompoundFile sectorManager, IDirectoryEntry dirEntry)
: base(sectorManager)
{
if (dirEntry == null || dirEntry.SID < 0)
throw new CFException("Attempting to add a CFStream using an unitialized directory");
this.DirEntry = dirEntry;
}
/// <summary>
/// Set the data associated with the stream object.
/// </summary>
/// <example>
/// <code>
/// byte[] b = new byte[]{0x0,0x1,0x2,0x3};
/// CompoundFile cf = new CompoundFile();
/// CFStream myStream = cf.RootStorage.AddStream("MyStream");
/// myStream.SetData(b);
/// </code>
/// </example>
/// <param name="data">Data bytes to write to this stream</param>
public void SetData(Byte[] data)
{
CheckDisposed();
this.CompoundFile.SetData(this, data);
}
/// <summary>
/// Append the provided data to stream data.
/// </summary>
/// <example>
/// <code>
/// byte[] b = new byte[]{0x0,0x1,0x2,0x3};
/// byte[] b2 = new byte[]{0x4,0x5,0x6,0x7};
/// CompoundFile cf = new CompoundFile();
/// CFStream myStream = cf.RootStorage.AddStream("MyStream");
/// myStream.SetData(b); // here we could also have invoked .AppendData
/// myStream.AppendData(b2);
/// cf.Save("MyLargeStreamsFile.cfs);
/// cf.Close();
/// </code>
/// </example>
/// <param name="data">Data bytes to append to this stream</param>
/// <remarks>
/// This method allows user to create stream with more than 2GB of data,
/// appending data to the end of existing ones.
/// Large streams (>2GB) are only supported by CFS version 4.
/// Append data can also be invoked on streams with no data in order
/// to simplify its use inside loops.
/// </remarks>
public void AppendData(Byte[] data)
{
CheckDisposed();
if (this.Size > 0)
{
this.CompoundFile.AppendData(this, data);
}
else
{
this.CompoundFile.SetData(this, data);
}
}
/// <summary>
/// Get the data associated with the stream object.
/// </summary>
/// <example>
/// <code>
/// CompoundFile cf2 = new CompoundFile("AFileName.cfs");
/// CFStream st = cf2.RootStorage.GetStream("MyStream");
/// byte[] buffer = st.GetData();
/// </code>
/// </example>
/// <returns>Array of byte containing stream data</returns>
/// <exception cref="T:OpenMcdf.CFDisposedException">
/// Raised when the owner compound file has been closed.
/// </exception>
public Byte[] GetData()
{
CheckDisposed();
return this.CompoundFile.GetData(this);
}
/// <summary>
/// Get <paramref name="count"/> bytes associated with the stream object, starting from
/// a provided <paramref name="offset"/>. When method returns, count will contain the
/// effective count of bytes read.
/// </summary>
/// <example>
/// <code>
/// CompoundFile cf = new CompoundFile("AFileName.cfs");
/// CFStream st = cf.RootStorage.GetStream("MyStream");
/// int count = 8;
/// // The stream is supposed to have a length greater than offset + count
/// byte[] data = st.GetData(20, ref count);
/// cf.Close();
/// </code>
/// </example>
/// <returns>Array of byte containing stream data</returns>
/// <exception cref="T:OpenMcdf.CFDisposedException">
/// Raised when the owner compound file has been closed.
/// </exception>
public Byte[] GetData(long offset, ref int count)
{
CheckDisposed();
return this.CompoundFile.GetData(this, offset, ref count);
}
/// <summary>
/// Copy data from an existing stream.
/// </summary>
/// <param name="input">A stream to read from</param>
/// <remarks>
/// Input stream is NOT closed after method invocation.
/// </remarks>
public void CopyFrom(Stream input)
{
CheckDisposed();
byte[] buffer = new byte[input.Length];
if (input.CanSeek)
{
input.Seek(0, SeekOrigin.Begin);
}
input.Read(buffer, 0, (int)input.Length);
this.SetData(buffer);
}
}
}