zhao
2021-07-09 0821715ebc11d3934d0594a1cc2c39686d808906
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (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.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
/* ================================================================
 * About NPOI
 * Author: Tony Qu 
 * Author's email: tonyqus (at) gmail.com 
 * Author's Blog: tonyqus.wordpress.com.cn (wp.tonyqus.cn)
 * HomePage: http://www.codeplex.com/npoi
 * Contributors:
 * 
 * ==============================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace HH.WMS.Utils.NPOI.POIFS.FileSystem
{
    /// <summary>
    /// This class provides a wrapper over an OutputStream so that Document
    /// writers can't accidently go over their size limits
    /// @author Marc Johnson (mjohnson at apache dot org)
    /// </summary>
    [Obsolete]
    public class POIFSDocumentWriter:Stream
    {
        private int limit;
        private Stream stream;
        private int written;
 
        /// <summary>
        /// Create a POIFSDocumentWriter
        /// </summary>
        /// <param name="stream">the OutputStream to which the data is actually</param>
        /// <param name="limit">the maximum number of bytes that can be written</param>
        public POIFSDocumentWriter(Stream stream, int limit)
        {
            this.stream = stream;
            this.limit = limit;
            this.written = 0;
        }
        /// <summary>
        /// Closes this output stream and releases any system resources
        /// associated with this stream. The general contract of close is
        /// that it closes the output stream. A closed stream cannot
        /// perform output operations and cannot be reopened.
        /// </summary>
        public override void Close()
        {
            this.stream.Close();
        }
        /// <summary>
        /// Flushes this output stream and forces any buffered output bytes
        /// to be written out.
        /// </summary>
        public override void Flush()
        {
            this.stream.Flush();
        }
 
        private void LimitCheck(int toBeWritten)
        {
            if ((this.written + toBeWritten) > this.limit)
            {
                throw new IOException("tried to write too much data");
            }
            this.written += toBeWritten;
        }
 
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
 
        public override long Seek(long offset, SeekOrigin origin)
        {
            return 0L;
        }
 
        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }
 
 
        public void Write(int b)
        {
            LimitCheck(1);
            stream.WriteByte((byte)b);
        }
 
        /// <summary>
        /// Writes b.length bytes from the specified byte array
        /// to this output stream.
        /// </summary>
        /// <param name="b">the data.</param>
        public void Write(byte[] b)
        {
            this.Write(b, 0, b.Length);
        }
        /// <summary>
        /// Writes len bytes from the specified byte array starting at
        /// offset off to this output stream.  The general contract for
        /// write(b, off, len) is that some of the bytes in the array b are
        /// written to the output stream in order; element b[off] is the
        /// first byte written and b[off+len-1] is the last byte written by
        /// this operation.
        /// If b is null, a NullPointerException is thrown.
        /// If off is negative, or len is negative, or off+len is greater
        /// than the length of the array b, then an
        /// IndexOutOfBoundsException is thrown.
        /// </summary>
        /// <param name="b">the data.</param>
        /// <param name="off">the start offset in the data.</param>
        /// <param name="len">the number of bytes to write.</param>
        public override void Write(byte[] b, int off, int len)
        {
            this.LimitCheck(len);
            this.stream.Write(b, off, len);
        }
 
        /// <summary>
        /// Writes the specified byte to this output stream. The general
        /// contract for write is that one byte is written to the output
        /// stream. The byte to be written is the eight low-order bits of
        /// the argument b. The 24 high-order bits of b are ignored.
        /// </summary>
        /// <param name="b">the byte.</param>
        public override void WriteByte(byte b)
        {
            this.LimitCheck(1);
            this.stream.WriteByte(b);
        }
        /// <summary>
        /// write the rest of the document's data (fill in at the end)
        /// </summary>
        /// <param name="totalLimit">the actual number of bytes the corresponding         
        /// document must fill</param>
        /// <param name="fill">the byte to fill remaining space with</param>
        public virtual void WriteFiller(int totalLimit, byte fill)
        {
            if (totalLimit > this.written)
            {
                byte[] buffer = new byte[totalLimit - this.written];
                for (int i = 0; i < buffer.Length; i++)
                {
                    buffer[i] = fill;
                }
                this.stream.Write(buffer, 0, buffer.Length);
            }
        }
 
        /// <summary>
        /// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
        /// </summary>
        /// <value></value>
        /// <returns>true if the stream supports reading; otherwise, false.
        /// </returns>
        public override bool CanRead
        {
            get
            {
                return false;
            }
        }
 
        /// <summary>
        /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
        /// </summary>
        /// <value></value>
        /// <returns>true if the stream supports seeking; otherwise, false.
        /// </returns>
        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }
 
        /// <summary>
        /// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
        /// </summary>
        /// <value></value>
        /// <returns>true if the stream supports writing; otherwise, false.
        /// </returns>
        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }
 
        /// <summary>
        /// When overridden in a derived class, gets the length in bytes of the stream.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// A long value representing the length of the stream in bytes.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">
        /// A class derived from Stream does not support seeking.
        /// </exception>
        /// <exception cref="T:System.ObjectDisposedException">
        /// Methods were called after the stream was closed.
        /// </exception>
        public override long Length
        {
            get
            {
                return this.stream.Length;
            }
        }
 
        /// <summary>
        /// When overridden in a derived class, gets or sets the position within the current stream.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The current position within the stream.
        /// </returns>
        /// <exception cref="T:System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="T:System.NotSupportedException">
        /// The stream does not support seeking.
        /// </exception>
        /// <exception cref="T:System.ObjectDisposedException">
        /// Methods were called after the stream was closed.
        /// </exception>
        public override long Position
        {
            get
            {
                return this.stream.Position;
            }
            set
            {
                this.stream.Position = value;
            }
        }
 
    }
}