Dart: dart:builtin

dart:builtin are the runtime libraries available to the DartVM. They provide some of the more system level API you would expect such as streams I/O, filesystem I/O, basic TCP socket I/O, platform information, native process invocation.  Most of these class implementations do call “native”, which from the point of view of the DartVM means invoking native VM code. “native” tells the VM to look up in a list for the entry that maps to the following argument after “native”.

_DirectoryNative

nativedirectory.png

While Dart is still Alpha these builtin may not be written in stone, so try not to depend on them too much. They can and do provide you with some neat access to native functionality you would commonly need when written shell/system applications.

By including #import(‘dart:builtin’); you can view the builtin dart files from DartEditor. Currently the DartEditor might show  warnings when including this module but runtime should not be affected. The truth of the matter is, if you explicitly include ‘dart:builtin’ or not, it is still available to you from implicit inclusion in the DartVM.

dartbuiltin

The following should give an idea of what built in objects are accessible and how they may be used. The examples should not be considered best practices, just an idea of how you could use them and that they exist. The code is taken from a sample that can be found on Github

Directory

    Directory d;
    if (l.length >= 2) {
      d = new Directory(l[1]);
    } else {
      d = new Directory(".");
    }
    d.dirHandler = (var dir) {
      print("${dir}");
    };
    d.fileHandler = (var file) {
      print("${file}");
    };
    d.list(true);

File

    File f = new File(l[0]);
    if (!f.existsSync()) {
      print("Error: file does not exist");
      return;
    }
    RandomAccessFile r = f.openSync(FileMode.READ);
    int length = r.lengthSync();
    List buffer = new List(length);
    int readLength = r.readListSync(buffer, 0, length);
    r.close();
    String s = new String.fromCharCodes(buffer);
    print(s);

ChunkedInputStream

    ChunkedInputStream chunkedInputStream = new ChunkedInputStream(f.openInputStream(), 16);
    List c = chunkedInputStream.read();
    while(c!=null) {
      StringBuffer sb = new StringBuffer();
      c.forEach((int b) {
        sb.add("0x");
        if (b.toRadixString(16).length == 1) {
          sb.add("0");
        }
        sb.add(b.toRadixString(16));
        sb.add(" ");
      });
      print(sb.toString());
      c = chunkedInputStream.read();
    }

InputStream
OutputStream

    File src = new File(arg[1]);
    File dest = new File(arg[2]);
    var srcStream = src.openInputStream();
    var destStream = dest.openOutputStream();
    destStream.write(srcStream.read());
    srcStream.close();
    destStream.close();

StringInputStream
DynamicListInputStream
ListInputStream
ListOutputStream

Platform

 Platform p = new Platform();
 print('{"operatingSystem":${p.operatingSystem()},"numberOfProcessors":${p.numberOfProcessors()},"pathSeparator":${p.pathSeparator()}}');

Process

    Process ps = new Process.start('ps', []);
    StringBuffer messages = new StringBuffer();
    ps.exitHandler = (int status) {
      ps.close();
      print(messages.toString());
    };
    ps.stdout.dataHandler = () => _readAll(ps.stdout, messages);
    ps.stderr.dataHandler = () => _readAll(ps.stderr, messages);

ServerSocket

    // initialize the server
    serverSocket = new ServerSocket(host, 0, 5);
    if (serverSocket == null) {
      throw "can't get server socket";
    }
    serverSocket.connectionHandler = onConnect;
    print("accepting connections on ${host}:${serverSocket.port}");

Socket

    sendSocket = new Socket(host, serverSocket.port);
    if (sendSocket == null) {
      throw "can't get send socket";
    }
    // send first 4 bytes immediately
    for (int i = 0; i < 4; i++) {
      sendByte();
    }
    // send next 4 bytes slowly
    new Timer.repeating((Timer t) {
      sendByte();
      if (bytesSent == bytesTotal) {
        sendSocket.close();
        t.cancel();
        print("finished sending");
      }
    }, 500);

SocketInputStream
SocketOutputStream

Timer

<pre>    if (l.length >= 2) {
      try {
        milliSeconds = (Math.parseInt(l[1]).abs()*milliSeconds);
      } catch (BadNumberFormatException ex) {}
    }
    timer = new Timer((var t) {
      print("\nTime elapsed on Timer");
    }, milliSeconds);
About these ads

About financecoding
I enjoy coding in dart.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: