2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.IO / BufferedStream.cs
index d70ad125374d8e875aa5279c474bf3ec635a0eb2..ab8bcd831d814ed138cc164b81e238a5eb238a02 100644 (file)
@@ -8,6 +8,29 @@
 // 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;
 
@@ -124,6 +147,16 @@ namespace System.IO {
                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;
@@ -174,7 +207,7 @@ namespace System.IO {
                        }
 
                        if (count <= m_buffer_read_ahead - m_buffer_pos) {
-                               Array.Copy(m_buffer, m_buffer_pos, array, offset, count);
+                               Buffer.BlockCopyInternal (m_buffer, m_buffer_pos, array, offset, count);
 
                                m_buffer_pos += count;
                                if (m_buffer_pos == m_buffer_read_ahead) {
@@ -186,7 +219,7 @@ namespace System.IO {
                        }
 
                        int ret = m_buffer_read_ahead - m_buffer_pos;
-                       Array.Copy(m_buffer, m_buffer_pos, array, offset, ret);
+                       Buffer.BlockCopyInternal (m_buffer, m_buffer_pos, array, offset, ret);
                        m_buffer_pos = 0;
                        m_buffer_read_ahead = 0;
                        offset += ret;
@@ -198,11 +231,11 @@ namespace System.IO {
                                m_buffer_read_ahead = m_stream.Read(m_buffer, 0, m_buffer.Length);
                                
                                if (count < m_buffer_read_ahead) {
-                                       Array.Copy(m_buffer, 0, array, offset, count);
+                                       Buffer.BlockCopyInternal (m_buffer, 0, array, offset, count);
                                        m_buffer_pos = count;
                                        ret += count;
                                } else {
-                                       Array.Copy(m_buffer, 0, array, offset, m_buffer_read_ahead);
+                                       Buffer.BlockCopyInternal (m_buffer, 0, array, offset, m_buffer_read_ahead);
                                        ret += m_buffer_read_ahead;
                                        m_buffer_read_ahead = 0;
                                }
@@ -239,7 +272,7 @@ namespace System.IO {
                                m_stream.Write (array, offset, count);
                        } 
                        else {
-                               Array.Copy (array, offset, m_buffer, m_buffer_pos, count);
+                               Buffer.BlockCopyInternal  (array, offset, m_buffer, m_buffer_pos, count);
                                m_buffer_pos += count;
                        }
                }
@@ -250,6 +283,6 @@ namespace System.IO {
                                throw new ObjectDisposedException ("BufferedStream", 
                                        Locale.GetText ("Stream is closed"));
                        }
-               }                       
+               }
        }
 }