It is possible to open a file stream on an existing file descriptor by sending the on: message to the CfsFileStream class. The on: message answers an instance of the concrete stream class corresponding to the access mode of the file descriptor. An example of converting a file descriptor into a file stream is shown below:
| fd file | fd := CfsFileDescriptor open: 'new.fil' oflag: ORDWR | OCREAT | OEXCL. fd isCfsError ifTrue: [^self error: fd printString]. file := CfsFileStream on: fd. "Close the file stream - this will automatically close the file descriptor as well" file close.
The on: message is also useful if the file is to be opened with a special share mode, using the open:oflag:share: message, which was discussed in "File locking". An example follows:
| file fd | (fd := CfsFileDescriptor open: 'a.fil' oflag: ORDWR share: ODENYRDWR) isCfsError ifTrue: [^self error: fd message]. file := CfsFileStream on: fd. file close.