//
// Author:
// Zoltan Varga (vargaz@gmail.com)
+// Marek Safar (marek.safar@gmail.com)
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-#if NET_4_0
using System;
using System.Runtime.InteropServices;
namespace System.IO
{
+ [MonoTODO ("Offset is ignored")]
public class UnmanagedMemoryAccessor : IDisposable {
SafeBuffer buffer;
+#pragma warning disable 414
long offset;
+#pragma warning restore
long capacity;
bool canwrite, canread;
- public UnmanagedMemoryAccessor ()
+ protected UnmanagedMemoryAccessor ()
{
}
throw new ArgumentOutOfRangeException ("offset");
if (capacity < 0)
throw new ArgumentOutOfRangeException ("capacity");
- if (offset + capacity < 0)
- throw new InvalidOperationException ();
if (access == FileAccess.Read || access == FileAccess.ReadWrite)
canread = true;
this.capacity = capacity;
}
- public void Dispose () {
+ public void Dispose ()
+ {
Dispose (true);
GC.SuppressFinalize (this);
}
- public void Dispose (bool disposing) {
+ protected virtual void Dispose (bool disposing)
+ {
if (buffer != null){
if (disposing){
buffer.Dispose ();
buffer = null;
}
-#if false
- //
- // The code below can be enabled once this bug is fixed:
- // https://bugzilla.novell.com/show_bug.cgi?id=553650
- //
public byte ReadByte (long position)
{
if (!canread)
return buffer.Read<bool> ((ulong) position);
}
- public bool ReadChar (long position)
+ public char ReadChar (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<char> ((ulong) position);
}
- public bool ReadDecimal (long position)
+ public decimal ReadDecimal (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<decimal> ((ulong) position);
}
- public bool ReadDouble (long position)
+ public double ReadDouble (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<double> ((ulong) position);
}
- public bool ReadInt16 (long position)
+ public short ReadInt16 (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<short> ((ulong) position);
}
- public bool ReadInt32 (long position)
+ public int ReadInt32 (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<int> ((ulong) position);
}
- public bool ReadInt64 (long position)
+ public long ReadInt64 (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<long> ((ulong) position);
}
- public bool ReadSByte (long position)
+ [CLSCompliant (false)]
+ public sbyte ReadSByte (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<sbyte> ((ulong) position);
}
- public bool ReadSingle (long position)
+ public float ReadSingle (long position)
{
if (!canread)
throw new NotSupportedException ();
}
[CLSCompliant (false)]
- public bool ReadUInt16 (long position)
+ public ushort ReadUInt16 (long position)
{
if (!canread)
throw new NotSupportedException ();
}
[CLSCompliant (false)]
- public bool ReadUInt32 (long position)
+ public uint ReadUInt32 (long position)
{
if (!canread)
throw new NotSupportedException ();
}
[CLSCompliant (false)]
- public bool ReadUInt64 (long position)
+ public ulong ReadUInt64 (long position)
{
if (!canread)
throw new NotSupportedException ();
return buffer.Read<ulong> ((ulong) position);
}
- public void Read<T> (long position, out T structure)
+ public void Read<T> (long position, out T structure) where T : struct
{
if (!canread)
throw new NotSupportedException ();
structure = buffer.Read<T> ((ulong) position);
}
- public int ReadArray<T> (long position, T [] array, int offset, int count)
+ public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
{
if (position < 0)
throw new ArgumentOutOfRangeException ();
long left = capacity - position;
- int slots = (left / Marshal.SizeOf (typeof (T)));
-
- buffer.ReadArray ((ulong) position, array, index, slots);
+ var slots = Math.Min (count, (int)(left / Marshal.SizeOf (typeof (T))));
+
+ buffer.ReadArray ((ulong) position, array, offset, slots);
return slots;
}
buffer.Write ((ulong)position, value);
}
+
+ public void Write (long position, double value)
+ {
+ if (!canwrite)
+ throw new NotSupportedException ();
+ if (buffer == null)
+ throw new ObjectDisposedException ("buffer");
+ if (position < 0)
+ throw new ArgumentOutOfRangeException ();
+
+ buffer.Write ((ulong)position, value);
+ }
public void Write (long position, short value)
{
}
[CLSCompliant (false)]
- public void Write (long position, sbyte value)
+ public void Write (long position, sbyte value)
{
if (!canwrite)
throw new NotSupportedException ();
}
[CLSCompliant (false)]
- public void Write (long position, ushort value)
+ public void Write (long position, ushort value)
{
if (!canwrite)
throw new NotSupportedException ();
}
[CLSCompliant (false)]
- public void Write (long position, uint value)
+ public void Write (long position, uint value)
{
if (!canwrite)
throw new NotSupportedException ();
buffer.Write ((ulong)position, value);
}
- public void Write<T> (long position, ref T structure)
+ public void Write<T> (long position, ref T structure) where T : struct
{
if (!canwrite)
throw new NotSupportedException ();
if (position < 0)
throw new ArgumentOutOfRangeException ();
- buffer.Write<T> (position, structure);
+ buffer.Write<T> ((ulong)position, structure);
}
public void WriteArray<T> (long position, T [] array, int offset, int count) where T : struct
{
- buffer.WriteArray (position, array, offset, count);
+ buffer.WriteArray ((ulong)position, array, offset, count);
}
-#endif
public bool CanRead {
get { return canread; }
public long Capacity {
get { return capacity; }
}
-
+ protected bool IsOpen {
+ get { return buffer != null; }
+ }
}
}
-#endif
\ No newline at end of file