Monday, November 08, 2010 at 12:07 AM.


<<Script: system.verbs.builtins.webserver.util.readBytes; Version 1; Date: Fri, 15 May 1998 07:52:22 GMT; ID: RAB
on readBytes (stream, bytesToRead, timeoutSecs=10) {
	<<March 19, 1998 at 5:02:05 PM by WMF
		<<This script reads a certain number of bytes from a TCP stream
		<<It returns the data that was read
		<<An error will be thrown if no data arrives before the timeout expires
	local (bytes = 0, data = "", timeout);
	timeout = clock.ticks () + 60 * timeoutSecs; // convert to ticks
	bytesToRead = number (bytesToRead);
	while (sizeOf (data) < bytesToRead) {
		<<we haven't read in the whole data yet
		if (clock.ticks () > timeout) { // the client timed out
			scriptError ("The connection timed out before enough data was read.")};
		case tcp.statusStream (stream, @bytes) {
			"DATA" {
				data = data + tcp.readStream (stream, bytes);
				timeout = clock.ticks () + 60 * timeoutSecs}; // extend the timeout
			"OPEN" {
				sys.systemTask(); //yield a little within this thread for the system
				thread.sleepfor (1)}; //yield this thread for a second to allow normal frontier processing
			"CLOSED" {
				scriptError ("The connection broke before enough data was read.")}}};
	if (sizeOf (data) < bytesToRead) {
		scriptError ("Something impossible happened before enough data was read.")};
	return (data)}

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.