/* ====================================================================
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
{
///
/// 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)
///
[Obsolete]
public class POIFSDocumentWriter:Stream
{
private int limit;
private Stream stream;
private int written;
///
/// Create a POIFSDocumentWriter
///
/// the OutputStream to which the data is actually
/// the maximum number of bytes that can be written
public POIFSDocumentWriter(Stream stream, int limit)
{
this.stream = stream;
this.limit = limit;
this.written = 0;
}
///
/// 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.
///
public override void Close()
{
this.stream.Close();
}
///
/// Flushes this output stream and forces any buffered output bytes
/// to be written out.
///
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);
}
///
/// Writes b.length bytes from the specified byte array
/// to this output stream.
///
/// the data.
public void Write(byte[] b)
{
this.Write(b, 0, b.Length);
}
///
/// 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.
///
/// the data.
/// the start offset in the data.
/// the number of bytes to write.
public override void Write(byte[] b, int off, int len)
{
this.LimitCheck(len);
this.stream.Write(b, off, len);
}
///
/// 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.
///
/// the byte.
public override void WriteByte(byte b)
{
this.LimitCheck(1);
this.stream.WriteByte(b);
}
///
/// write the rest of the document's data (fill in at the end)
///
/// the actual number of bytes the corresponding
/// document must fill
/// the byte to fill remaining space with
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);
}
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
///
///
/// true if the stream supports reading; otherwise, false.
///
public override bool CanRead
{
get
{
return false;
}
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
///
///
/// true if the stream supports seeking; otherwise, false.
///
public override bool CanSeek
{
get
{
return false;
}
}
///
/// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
///
///
/// true if the stream supports writing; otherwise, false.
///
public override bool CanWrite
{
get
{
return true;
}
}
///
/// When overridden in a derived class, gets the length in bytes of the stream.
///
///
///
/// A long value representing the length of the stream in bytes.
///
///
/// A class derived from Stream does not support seeking.
///
///
/// Methods were called after the stream was closed.
///
public override long Length
{
get
{
return this.stream.Length;
}
}
///
/// When overridden in a derived class, gets or sets the position within the current stream.
///
///
///
/// The current position within the stream.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support seeking.
///
///
/// Methods were called after the stream was closed.
///
public override long Position
{
get
{
return this.stream.Position;
}
set
{
this.stream.Position = value;
}
}
}
}