Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE NoImplicitPrelude #-}{-# OPTIONS_GHC -funbox-strict-fields #-}------------------------------------------------------------------------------- |-- Module : GHC.IO.BufferedIO-- Copyright : (c) The University of Glasgow 2008-- License : see libraries/base/LICENSE---- Maintainer : cvs-ghc@haskell.org-- Stability : internal-- Portability : non-portable (GHC Extensions)---- Class of buffered IO devices-------------------------------------------------------------------------------moduleGHC.IO.BufferedIO(BufferedIO(..),readBuf,readBufNonBlocking,writeBuf,writeBufNonBlocking)whereimportGHC.BaseimportGHC.PtrimportData.WordimportGHC.NumimportGHC.IO.DeviceasIODeviceimportGHC.IO.DeviceasRawIOimportGHC.IO.Buffer-- | The purpose of 'BufferedIO' is to provide a common interface for I/O-- devices that can read and write data through a buffer. Devices that-- implement 'BufferedIO' include ordinary files, memory-mapped files,-- and bytestrings. The underlying device implementing a 'Handle' must-- provide 'BufferedIO'.--classBufferedIOdevwhere-- | allocate a new buffer. The size of the buffer is at the-- discretion of the device; e.g. for a memory-mapped file the-- buffer will probably cover the entire file.newBuffer::dev->BufferState->IO(BufferWord8)-- | reads bytes into the buffer, blocking if there are no bytes-- available. Returns the number of bytes read (zero indicates-- end-of-file), and the new buffer.fillReadBuffer::dev->BufferWord8->IO(Int,BufferWord8)-- | reads bytes into the buffer without blocking. Returns the-- number of bytes read (Nothing indicates end-of-file), and the new-- buffer.fillReadBuffer0::dev->BufferWord8->IO(MaybeInt,BufferWord8)-- | Prepares an empty write buffer. This lets the device decide-- how to set up a write buffer: the buffer may need to point to a-- specific location in memory, for example. This is typically used-- by the client when switching from reading to writing on a-- buffered read/write device.---- There is no corresponding operation for read buffers, because before-- reading the client will always call 'fillReadBuffer'.emptyWriteBuffer::dev->BufferWord8->IO(BufferWord8)emptyWriteBuffer_devbuf=returnbuf{bufL=0,bufR=0,bufState=WriteBuffer}-- | Flush all the data from the supplied write buffer out to the device.-- The returned buffer should be empty, and ready for writing.flushWriteBuffer::dev->BufferWord8->IO(BufferWord8)-- | Flush data from the supplied write buffer out to the device-- without blocking. Returns the number of bytes written and the-- remaining buffer.flushWriteBuffer0::dev->BufferWord8->IO(Int,BufferWord8)-- for an I/O device, these operations will perform reading/writing-- to/from the device.-- for a memory-mapped file, the buffer will be the whole file in-- memory. fillReadBuffer sets the pointers to encompass the whole-- file, and flushWriteBuffer needs to do no I/O. A memory-mapped-- file has to maintain its own file pointer.-- for a bytestring, again the buffer should match the bytestring in-- memory.-- ----------------------------------------------------------------------------- Low-level read/write to/from buffers-- These operations make it easy to implement an instance of 'BufferedIO'-- for an object that supports 'RawIO'.readBuf::RawIOdev=>dev->BufferWord8->IO(Int,BufferWord8)readBufdevbbuf=doletbytes=bufferAvailablebbufres<-withBufferbbuf$\ptr->RawIO.readdev(ptr`plusPtr`bufRbbuf)bytesreturn(res,bbuf{bufR=bufRbbuf+res})-- zero indicates end of filereadBufNonBlocking::RawIOdev=>dev->BufferWord8->IO(MaybeInt,-- Nothing ==> end of file-- Just n ==> n bytes were read (n>=0)BufferWord8)readBufNonBlockingdevbbuf=doletbytes=bufferAvailablebbufres<-withBufferbbuf$\ptr->IODevice.readNonBlockingdev(ptr`plusPtr`bufRbbuf)bytescaseresofNothing->return(Nothing,bbuf)Justn->return(Justn,bbuf{bufR=bufRbbuf+n})writeBuf::RawIOdev=>dev->BufferWord8->IO(BufferWord8)writeBufdevbbuf=doletbytes=bufferElemsbbufwithBufferbbuf$\ptr->IODevice.writedev(ptr`plusPtr`bufLbbuf)bytesreturnbbuf{bufL=0,bufR=0}-- XXX ToDowriteBufNonBlocking::RawIOdev=>dev->BufferWord8->IO(Int,BufferWord8)writeBufNonBlockingdevbbuf=doletbytes=bufferElemsbbufres<-withBufferbbuf$\ptr->IODevice.writeNonBlockingdev(ptr`plusPtr`bufLbbuf)bytesreturn(res,bufferAdjustL(bufLbbuf+res)bbuf)
[8]ページ先頭