FILE I/O Inheritance? Interface?

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Folks,

I have a need to create, I guess in .NET terms, a provider for a file
I/O interface.
For example, in VB.NET

' open local file to read
Dim sr As New BinaryReader(File.Open("c:\file.txt", FileMode.open))

' open server file to write
Dim sw As New BinaryWriter(File.Open("wc:\123.txt", fileMode.write))

where the source and target streams is prepared based on the prefix of
the file name.

I want to inherit or wrap the FILE class to handle proprietary I/O.
I have this done already in C/C++ and would like to do the same, if
possible, in VB.NET.

Should/Can I use BinaryReader, BinaryWriter, File or some other class
like Stream, BufferedStream?

In other tools framework, they provides a FILE I/O class where you
override the the basic methods:

Open
Read
Write
Seek
Lock
UnLock
EOF
Close

etc. How can this be done in .NET?

Thanks

--
 
Folks,

I have a need to create, I guess in .NET terms, a provider for a file
I/O interface.
For example, in VB.NET

' open local file to read
Dim sr As New BinaryReader(File.Open("c:\file.txt", FileMode.open))

' open server file to write
Dim sw As New BinaryWriter(File.Open("wc:\123.txt", fileMode.write))

where the source and target streams is prepared based on the prefix of
the file name.

I want to inherit or wrap the FILE class to handle proprietary I/O.
I have this done already in C/C++ and would like to do the same, if
possible, in VB.NET.

Should/Can I use BinaryReader, BinaryWriter, File or some other class
like Stream, BufferedStream?

In other tools framework, they provides a FILE I/O class where you
override the the basic methods:

Open
Read
Write
Seek
Lock
UnLock
EOF
Close

etc. How can this be done in .NET?

Thanks

Inherit from System.IO.Stream.
 
Tom said:
Inherit from System.IO.Stream.

Nak and Tom:

This is the approach I started, but I am still missing something,
namely the open file or file related overrides so I am checking that
out. Maybe that is what the CreateObjRef() is?

I can do it by directly using our WcCreateFile() function and passing
the handle to the Stream class. Then all the other read, write,
positioning functions apply.

In short, I have a fascimile of the WIN32 functions:

HANDLE WcCreateFile(const char * fn, ....);
BOOL WcCloseHandle(HANDLE h);
BOOL WcWriteFile(WCHANDLE h, LPCVOID buffer, ....);
BOOL WcReadFile(WCHANDLE h, LPVOID buffer, DWORD requested, ...);
BOOL WcSetEndOfFile(WCHANDLE h);
DWORD WcSetFilePointer(WCHANDLE h, LONG dist, DWORD movemethod);
BOOL WcSetFileTime(WCHANDLE h, FILETIME &ft);
DWORD WcGetFileSize(WCHANDLE h);
BOOL WcGetFileTime(WCHANDLE h, FILETIME &ft);

And the Read/Write, Pointer functions apply to the Stream class, but
the others need to be at higher level. That will allow for:

dim stm as new stream = WCFile.Open("wc:\123.txt", fileMode.write)
Dim fw As New BinaryWriter(stm)

--
 
Nak and Tom:

This is the approach I started, but I am still missing something,
namely the open file or file related overrides so I am checking that
out. Maybe that is what the CreateObjRef() is?

I can do it by directly using our WcCreateFile() function and passing
the handle to the Stream class. Then all the other read, write,
positioning functions apply.

In short, I have a fascimile of the WIN32 functions:

HANDLE WcCreateFile(const char * fn, ....);
BOOL WcCloseHandle(HANDLE h);
BOOL WcWriteFile(WCHANDLE h, LPCVOID buffer, ....);
BOOL WcReadFile(WCHANDLE h, LPVOID buffer, DWORD requested, ...);
BOOL WcSetEndOfFile(WCHANDLE h);
DWORD WcSetFilePointer(WCHANDLE h, LONG dist, DWORD movemethod);
BOOL WcSetFileTime(WCHANDLE h, FILETIME &ft);
DWORD WcGetFileSize(WCHANDLE h);
BOOL WcGetFileTime(WCHANDLE h, FILETIME &ft);

And the Read/Write, Pointer functions apply to the Stream class, but
the others need to be at higher level. That will allow for:

dim stm as new stream = WCFile.Open("wc:\123.txt", fileMode.write)
Dim fw As New BinaryWriter(stm)

--

I'm not sure I understand the problem:

A Stream in an abstract base class that represents a stream of bytes. Nothing
more. If wrapping it in a BinaryWriter makes sense, then do that. I would
say it would look something like:

Dim fw As New BinaryWriter(New MyCustomFileStream ("wc:\123.txt", FileMode.Write)

Your MyCustomFileStream class should handle all of the Seek, Open, Close, etc,
etc methods. BinaryWriter will just call your implementations anyway.

Maybe a little more information would give me a better perspective to give a
better answer :)
 
Tom said:
I'm not sure I understand the problem:

A Stream in an abstract base class that represents a stream of bytes. Nothing
more. If wrapping it in a BinaryWriter makes sense, then do that. I would
say it would look something like:

Dim fw As New BinaryWriter(New MyCustomFileStream ("wc:\123.txt", FileMode.Write)

Your MyCustomFileStream class should handle all of the Seek, Open, Close, etc,
etc methods. BinaryWriter will just call your implementations anyway.

Right. There are a few things here. It might up being as you suggest.

I got it to work adding the open to the stream class. I was looking to
see if I can encapsulate the FILE class, looking at SafeHandle as this
example:

http://msdn.microsoft.com/en-us/library/microsoft.win32.safehandles.safefilehandle.aspx

Streams use the safe handle provided by a derived stream class, i.e,
FileStream, TextStream, SocketStream.


Dim fs As New wioFileStream(filename,
GENERIC_READ, 0, OPEN_EXISTING)

Dim fs As New wioFileStream( _
new wioStream(filename), _ <-- returns safehandle
GENERIC_READ, 0, OPEN_EXISTING)
Maybe a little more information would give me a better perspective
to give a better answer :)

Ideally, I would like to just inherit the FILE class so as I have now
in C/C++ wraps:

HANDLE h = WcCreateFile()
HANDLE h = CreateFile()

The handle is the same, so that is all the streams need.

--
 
Back
Top