/* ====================================================================
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:
*
* ==============================================================*/
namespace HH.WMS.Utils.NPOI.Util
{
using System;
using System.IO;
public class PushbackStream:Stream
{
private int buf = -1;
private Stream s;
public PushbackStream(
Stream s)
{
this.s = s;
}
protected override void Dispose(bool disposing)
{
this.s = null;
base.Dispose(disposing);
}
///
/// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
///
///
/// The unsigned byte cast to an Int32, or -1 if at the end of the stream.
///
///
/// The stream does not support reading.
///
///
/// Methods were called after the stream was closed.
///
public override int ReadByte()
{
if (buf != -1)
{
int tmp = buf;
buf = -1;
return tmp;
}
return s.ReadByte();
}
///
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
///
/// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source.
/// The zero-based byte offset in at which to begin storing the data read from the current stream.
/// The maximum number of bytes to be read from the current stream.
///
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
///
///
/// The sum of and is larger than the buffer length.
///
///
/// is null.
///
///
/// or is negative.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support reading.
///
///
/// Methods were called after the stream was closed.
///
public override int Read(byte[] buffer, int offset, int count)
{
if (buf != -1 && count > 0)
{
// TODO Can this case be made more efficient?
buffer[offset] = (byte) buf;
buf = -1;
return 1;
}
return s.Read(buffer, offset, count);
}
///
/// Unreads the specified b.
///
/// The b.
public virtual void Unread(int b)
{
if (buf != -1)
throw new InvalidOperationException("Can only push back one byte");
buf = b & 0xFF;
s.Position -= b;
}
///
/// 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 s.CanRead; }
}
///
/// 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 s.CanSeek; }
}
///
/// 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 s.CanWrite; }
}
///
/// 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 s.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 s.Position; }
set { s.Position = value; }
}
///
/// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.
///
public override void Close()
{
s.Close();
}
///
/// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
///
///
/// An I/O error occurs.
///
public override void Flush()
{
s.Flush();
}
///
/// When overridden in a derived class, sets the position within the current stream.
///
/// A byte offset relative to the parameter.
/// A value of type indicating the reference point used to obtain the new position.
///
/// The new position within the current stream.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
///
///
/// Methods were called after the stream was closed.
///
public override long Seek(long offset, SeekOrigin origin)
{
return s.Seek(offset, origin);
}
///
/// When overridden in a derived class, sets the length of the current stream.
///
/// The desired length of the current stream in bytes.
///
/// An I/O error occurs.
///
///
/// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
///
///
/// Methods were called after the stream was closed.
///
public override void SetLength(long value)
{
s.SetLength(value);
}
///
/// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
///
/// An array of bytes. This method copies bytes from to the current stream.
/// The zero-based byte offset in at which to begin copying bytes to the current stream.
/// The number of bytes to be written to the current stream.
///
/// The sum of and is greater than the buffer length.
///
///
/// is null.
///
///
/// or is negative.
///
///
/// An I/O error occurs.
///
///
/// The stream does not support writing.
///
///
/// Methods were called after the stream was closed.
///
public override void Write(byte[] buffer, int offset, int count)
{
s.Write(buffer, offset, count);
}
///
/// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
///
/// The byte to write to the stream.
///
/// An I/O error occurs.
///
///
/// The stream does not support writing, or the stream is already closed.
///
///
/// Methods were called after the stream was closed.
///
public override void WriteByte(byte value)
{
s.WriteByte(value);
}
}
}