Merge pull request #796 from alesliehughes/master
[mono.git] / mcs / class / corlib / System.IO / BinaryWriter.cs
index 5ee202b59704479553198b39f5906374b74ea438..be47858d4800486f5ea80d6a12e7f960196f0190 100644 (file)
@@ -1,12 +1,14 @@
 //
 // System.IO.BinaryWriter
 //
-// Author:
+// Authors:
 //   Matt Kimball (matt@kimball.net)
+//   Marek Safar (marek.safar@gmail.com)
 //
 
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright 2011 Xamarin Inc.
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -45,15 +47,33 @@ namespace System.IO {
                protected Stream OutStream;
                private Encoding m_encoding;
                private byte [] buffer;
-               private bool disposed = false;
+               byte [] stringBuffer;
+               int maxCharsPerRound;
+               bool disposed;
 
-               protected BinaryWriter() : this (Stream.Null, Encoding.UTF8UnmarkedUnsafe) {
+               protected BinaryWriter() : this (Stream.Null, Encoding.UTF8UnmarkedUnsafe)
+               {
                }
 
-               public BinaryWriter(Stream output) : this(output, Encoding.UTF8UnmarkedUnsafe) {
+               public BinaryWriter(Stream output) : this(output, Encoding.UTF8UnmarkedUnsafe)
+               {
                }
-
-               public BinaryWriter(Stream output, Encoding encoding) {
+               
+#if NET_4_5
+               readonly bool leave_open;
+               
+               public BinaryWriter(Stream output, Encoding encoding)
+                       : this (output, encoding, false)
+               {
+               }
+               
+               public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
+#else
+               const bool leave_open = false;
+               
+               public BinaryWriter(Stream output, Encoding encoding)
+#endif
+               {
                        if (output == null) 
                                throw new ArgumentNullException("output");
                        if (encoding == null) 
@@ -61,6 +81,9 @@ namespace System.IO {
                        if (!output.CanWrite)
                                throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
 
+#if NET_4_5
+                       leave_open = leaveOpen;
+#endif
                        OutStream = output;
                        m_encoding = encoding;
                        buffer = new byte [16];
@@ -68,6 +91,7 @@ namespace System.IO {
 
                public virtual Stream BaseStream {
                        get {
+                               Flush ();
                                return OutStream;
                        }
                }
@@ -87,7 +111,7 @@ namespace System.IO {
 
                protected virtual void Dispose (bool disposing)
                {
-                       if (disposing && OutStream != null)
+                       if (disposing && OutStream != null && !leave_open)
                                OutStream.Close();
                        
                        buffer = null;
@@ -270,9 +294,6 @@ namespace System.IO {
 
                        OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
                }
-
-               byte [] stringBuffer;
-               int maxCharsPerRound;
                
                public virtual void Write(string value) {