public object Deserialize (Stream stream)
{
if (stream == null)
throw new ArgumentNullException ("stream");
#if NET_4_0
using (StreamReader sr = new StreamReader (stream)) {
return Deserialize (sr.ReadToEnd ());
}
#else
long streamLength = -1;
if (stream.CanSeek)
streamLength = stream.Length;
MemoryStream ms = null;
if (streamLength != -1 && (stream is MemoryStream) && stream.Position == 0) {
// We save allocating a new stream and reading in this case.
ms = (MemoryStream) stream;
} else {
byte [] bytes = new byte [streamLength >= 0 ? streamLength : 2048];
ms = new MemoryStream ();
int n;
while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
ms.Write (bytes, 0, n);
streamLength = ms.Length;
}
string b64 = Encoding.ASCII.GetString (ms.GetBuffer (),
0, (int) streamLength);
return Deserialize (b64);
#endif
}