Difference between revisions of "NIO"

From HaskellWiki
Jump to: navigation, search
 
Line 29: Line 29:
 
seek :: Handle -> SeekMode -> Integer -> IO ()
 
seek :: Handle -> SeekMode -> Integer -> IO ()
 
close :: Handle -> IO ()
 
close :: Handle -> IO ()
  +
truncate :: Handle -> Integer -> IO () -- should throw some kind of exception
 
</haskell>
 
</haskell>
   
Line 41: Line 42:
 
== References ==
 
== References ==
   
1. http://www.python.org/dev/peps/pep-3116/
+
# http://www.python.org/dev/peps/pep-3116/
2.
+
#

Revision as of 21:34, 20 July 2008

New I/O

This page is currently being created. Please do not edit.

Rationale and Goals

Haskell 98 specifies and number of I/O actions. All these actions accept and return Strings. However, Strings are not a good type for performing I/O in all cases. Their structure give them bad cache locality and they take up more memory per byte or character than more compact representations like ByteStrings. It is also conceptually the wrong type for some operations. For example, sockets receive and send bytes while file I/O often deals in terms of characters and yet both use String to represent these two different concepts.

We need to first create a low-level API that covers the basic I/O functionality provided by the operating system which other, more high-level libraries can build upon.

Raw I/O

The new I/O library resides in the New I/O (NIO) module.

module System.Nio

All I/O actions deal in terms of ByteStrings.

import Data.ByteString
read :: Handle -> Int -> IO ByteString
write :: Handle -> ByteString -> IO Int
tell :: Handle -> IO Integer
seek :: Handle -> SeekMode -> Integer -> IO ()
close :: Handle -> IO ()
truncate :: Handle -> Integer -> IO ()  -- should throw some kind of exception

Buffered I/O

Text I/O

Non-blocking I/O

Extensibility

References

  1. http://www.python.org/dev/peps/pep-3116/