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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
//
// System.IO.BufferedStream
//
// Author:
// Matt Kimball (matt@kimball.net)
// Ville Palo <vi64pa@kolumbus.fi>
//
// Copyright (C) 2004 Novell (http://www.novell.com)
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Globalization;
using System.Runtime.InteropServices;
namespace System.IO {
#if NET_2_0
[ComVisible (true)]
#endif
public sealed class BufferedStream : Stream {
Stream m_stream;
byte[] m_buffer;
int m_buffer_pos;
int m_buffer_read_ahead;
bool m_buffer_reading;
private bool disposed = false;
public BufferedStream (Stream stream) : this (stream, 4096)
{
}
public BufferedStream (Stream stream, int bufferSize)
{
if (stream == null)
throw new ArgumentNullException ("stream");
// LAMESPEC: documented as < 0
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException ("bufferSize", "<= 0");
if (!stream.CanRead && !stream.CanWrite) {
throw new ObjectDisposedException (
Locale.GetText ("Cannot access a closed Stream."));
}
m_stream = stream;
m_buffer = new byte [bufferSize];
}
public override bool CanRead {
get {
return m_stream.CanRead;
}
}
public override bool CanWrite {
get {
return m_stream.CanWrite;
}
}
public override bool CanSeek {
get {
return m_stream.CanSeek;
}
}
public override long Length {
get {
Flush ();
return m_stream.Length;
}
}
public override long Position {
get {
CheckObjectDisposedException ();
return m_stream.Position - m_buffer_read_ahead + m_buffer_pos;
}
set {
if (value < Position && (Position - value <= m_buffer_pos) && m_buffer_reading) {
m_buffer_pos -= (int) (Position - value);
}
else if (value > Position && (value - Position < m_buffer_read_ahead - m_buffer_pos) && m_buffer_reading) {
m_buffer_pos += (int) (value - Position);
}
else {
Flush();
m_stream.Position = value;
}
}
}
#if NET_2_0
protected override void Dispose (bool disposing)
{
if (disposed)
return;
#else
public override void Close ()
{
#endif
if (m_buffer != null)
Flush();
m_stream.Close();
m_buffer = null;
disposed = true;
}
public override void Flush ()
{
CheckObjectDisposedException ();
if (m_buffer_reading) {
if (CanSeek)
m_stream.Position = Position;
} else if (m_buffer_pos > 0) {
m_stream.Write(m_buffer, 0, m_buffer_pos);
}
m_buffer_read_ahead = 0;
m_buffer_pos = 0;
}
public override long Seek (long offset, SeekOrigin origin)
{
CheckObjectDisposedException ();
if (!CanSeek) {
throw new NotSupportedException (
Locale.GetText ("Non seekable stream."));
}
Flush ();
return m_stream.Seek (offset, origin);
}
public override void SetLength (long value)
{
CheckObjectDisposedException ();
if (value < 0)
throw new ArgumentOutOfRangeException ("value must be positive");
if (!m_stream.CanWrite && !m_stream.CanSeek)
throw new NotSupportedException ("the stream cannot seek nor write.");
if ((m_stream == null) || (!m_stream.CanRead && !m_stream.CanWrite))
throw new IOException ("the stream is not open");
m_stream.SetLength(value);
if (Position > value)
Position = value;
}
public override int ReadByte ()
{
CheckObjectDisposedException ();
byte[] b = new byte[1];
if (Read(b, 0, 1) == 1) {
return b[0];
} else {
return -1;
}
}
public override void WriteByte (byte value)
{
CheckObjectDisposedException ();
byte[] b = new byte[1];
b[0] = value;
Write(b, 0, 1);
}
public override int Read ([In,Out] byte[] array, int offset, int count)
{
if (array == null)
throw new ArgumentNullException ("array");
CheckObjectDisposedException ();
if (!m_stream.CanRead) {
throw new NotSupportedException (
Locale.GetText ("Cannot read from stream"));
}
if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "< 0");
if (count < 0)
throw new ArgumentOutOfRangeException ("count", "< 0");
// re-ordered to avoid possible integer overflow
if (array.Length - offset < count)
throw new ArgumentException ("array.Length - offset < count");
if (!m_buffer_reading) {
Flush();
m_buffer_reading = true;
}
if (count <= m_buffer_read_ahead - m_buffer_pos) {
Buffer.BlockCopyInternal (m_buffer, m_buffer_pos, array, offset, count);
m_buffer_pos += count;
if (m_buffer_pos == m_buffer_read_ahead) {
m_buffer_pos = 0;
m_buffer_read_ahead = 0;
}
return count;
}
int ret = m_buffer_read_ahead - m_buffer_pos;
Buffer.BlockCopyInternal (m_buffer, m_buffer_pos, array, offset, ret);
m_buffer_pos = 0;
m_buffer_read_ahead = 0;
offset += ret;
count -= ret;
if (count >= m_buffer.Length) {
ret += m_stream.Read(array, offset, count);
} else {
m_buffer_read_ahead = m_stream.Read(m_buffer, 0, m_buffer.Length);
if (count < m_buffer_read_ahead) {
Buffer.BlockCopyInternal (m_buffer, 0, array, offset, count);
m_buffer_pos = count;
ret += count;
} else {
Buffer.BlockCopyInternal (m_buffer, 0, array, offset, m_buffer_read_ahead);
ret += m_buffer_read_ahead;
m_buffer_read_ahead = 0;
}
}
return ret;
}
public override void Write (byte[] array, int offset, int count)
{
if (array == null)
throw new ArgumentNullException ("array");
CheckObjectDisposedException ();
if (!m_stream.CanWrite) {
throw new NotSupportedException (
Locale.GetText ("Cannot write to stream"));
}
if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "< 0");
if (count < 0)
throw new ArgumentOutOfRangeException ("count", "< 0");
// avoid possible integer overflow
if (array.Length - offset < count)
throw new ArgumentException ("array.Length - offset < count");
if (m_buffer_reading) {
Flush();
m_buffer_reading = false;
}
// reordered to avoid possible integer overflow
if (m_buffer_pos >= m_buffer.Length - count) {
Flush ();
m_stream.Write (array, offset, count);
}
else {
Buffer.BlockCopyInternal (array, offset, m_buffer, m_buffer_pos, count);
m_buffer_pos += count;
}
}
private void CheckObjectDisposedException ()
{
if (disposed) {
throw new ObjectDisposedException ("BufferedStream",
Locale.GetText ("Stream is closed"));
}
}
}
}
|