[MonkeyScript] [TitleIndex] [WordIndex

Stream API

The stream API is used by all types of Streams, Files, Pipes, and Sockets.

   1 // Read
   2 stream.read();         // read till the end of the stream or return false if no more
   3 stream.read(bytes);    // read(); a certain number of bytes off the stream
   4 stream.readTo(string); // read(); from the stream till a certain string is encountered
   5 stream.readLine();     // readTo(eol);
   6 stream.seek(bytes);
   7 
   8 // Write
   9 stream.write(string);        // write a string into the stream
  10 stream.write(string, bytes); // write(); a specific length of the string into the stream, if smaller then pad with \0
  11 stream.writeLine(string);    // write(); a line terminated string into the stream
  12 
  13 // Meta info
  14 stream.position;
  15 stream.length;    // number of bytes in the stream
  16 stream.available; // how many bytes are left in the stream; stream.read(stream.available); is roughly stream.read();
  17                   // if(stream.available) can be a shorter check than if((data = stream.read()) !== false);
  18 stream.isReadable; // Can be read from (On File this instead checks if the file can be read)
  19 stream.isWritable; // Can be written to (On File this instead checks if the file is writable)
  20 

File API

Files implement the Stream API once you call an .open method on the file instance.

   1 // Constants
   2 File.RDONLY
   3 File.WRONLY
   4 File.RDWR
   5 File.CREATE_FILE
   6 File.APPEND
   7 File.TRUNCATE
   8 File.SYNC
   9 File.EXCL 
  10 
  11 // Construct
  12 new File(path or path object);
  13 
  14 // Open/Close; Open returns the file object so it can be chained
  15 file.open(File.READ);
  16 file.open('r');
  17 file.open(File.WRITE);
  18 file.open('w');
  19 file.close();
  20 file.isOpen;
  21 
  22 // File operations
  23 file.copy(File object|Path object|Path string);
  24 file.move(File object|Path object|Path string);
  25 file.delete();
  26 file.touch();
  27 
  28 // Shortcuts & Meta info
  29 file.exist;   // Check if the file exists
  30 file.size;    // Return the number of bytes in the file
  31 file.content; // file.open(File.READ).read(); file.close();
  32 file.content = ...; // file.open(File.WRITE).write(...); file.close();
  33 file.content = undefined; delete file.content; // same as file.delete();
  34 
  35 file.created; // should we use creationTime instead?
  36 file.lastModified; // last modified UNIX timestamp; Should we use modifyTime instead?
  37 file.lastAccessed; // atime?
  38 

Directory API

   1 new Directory(path or path object);
   2 
   3 directory.open();
   4 
   5 directory.make();
   6 directory.remove(); // .delete?
   7 directory.list([flags]);
   8 directory.glob;

Path

FilePath

   1 // Static
   2 FilePath.cwd; // currentWorkingDirectory;
   3 FilePath.home; // home directory
   4 FilePath.pathSeparator; // the path separator; Nix is / while Windows is \
   5 FilePath.listSeparator; // the list separator; Nix is : while Windows is ;
   6 
   7 // Constructing
   8 new FilePath(pathString); // similar to Path.cwd.resolve(pathString);
   9 
  10 fp.dirname;
  11 fp.name; // basename
  12 fp.ext; // last .
  13 // should we support .namebase without ext http://code.google.com/p/paver/source/browse/trunk/paver/path.py
  14 fp.hidden;
  15 fp.absolute; // returns absolute FilePath
  16 fp.path; // return pathstring
  17 // should we support .drive for windows only?
  18 
  19 fp.parent; // return path for parent directory
  20 fp.resolve(pathString); // create a new path resolved against the current path
  21 
  22 // Shortcuts to File/Directory operations
  23 fp.exist;
  24 fp.isDirectory;
  25 fp.isFile;
  26 fp.isReadable;
  27 fp.isWritable;
  28 fp.file; // returns File instance
  29 fp.directory; // returns Directory instance
  30 

Discussions

.exist; .isReadable; .isFile; This doesn't seam to follow a consistent naming pattern? Should we make .exist .exists instead? Should we use .readable? If we use .readable then do we use .file? what happends to Path's .file for returning a file object? Should we make .file return false when not a file, and true when it is one? Wouldn't that break the ability to create the file? Ruby uses ".writable?" ".readable?" and ".file?"


2010-07-23 04:25