ソースを参照

added haxe.io

Nicolas Cannasse 17 年 前
コミット
86be783bd6

+ 2 - 1
doc/CHANGES.txt

@@ -18,7 +18,6 @@ TODO inlining : substitute class+function type parameters in order to have fully
 	dynamic is now a keyword
 	f9dynamic is now dynamic and is mandatory on all platforms
 	public/private/dynamic are inherited by default when overriding a method
-	added Bytes api
 	removed Reflect.empty() : use {} instead
 	changed #else by #elseif, added #else
 	flash9 : optimized Hash,IntHash,StringBuf (use typed value)
@@ -26,6 +25,8 @@ TODO inlining : substitute class+function type parameters in order to have fully
 	optimized haxe.Md5 : don't use statics
 	allow up to 8 parameters in Reflect.createInstance
 	flash9 : some minor optimizations in haxe.Serializer
+	added haxe.io package (removed things from neko.io)
+	TODO read/write int31+int32
 
 2008-04-05: 1.19
 	fixed flash9 Array.toString

+ 22 - 9
std/haxe/ImportAll.hx

@@ -60,11 +60,21 @@ import haxe.Template;
 import haxe.Timer;
 import haxe.Unserializer;
 
-import haxe.remoting.AsyncAdapter;
+import haxe.io.Bytes;
+import haxe.io.BytesBuffer;
+import haxe.io.Input;
+import haxe.io.Output;
+import haxe.io.Eof;
+import haxe.io.Error;
+import haxe.io.BytesInput;
+import haxe.io.BytesOutput;
+
+import haxe.remoting.Connection;
 import haxe.remoting.AsyncConnection;
+/*
+import haxe.remoting.AsyncAdapter;
 import haxe.remoting.AsyncDebugConnection;
 import haxe.remoting.AsyncProxy;
-import haxe.remoting.Connection;
 import haxe.remoting.DelayedConnection;
 #if !neko
 import haxe.remoting.FlashJsConnection;
@@ -81,6 +91,7 @@ import haxe.remoting.SocketProtocol;
 #if flash
 import haxe.remoting.SocketWrapper;
 #end
+*/
 
 import haxe.rtti.Infos;
 import haxe.rtti.Type;
@@ -281,7 +292,7 @@ import flash.sampler.NewObjectSample;
 import flash.sampler.Sample;
 import flash.sampler.StackFrame;
 
-#else flash
+#elseif flash
 
 import flash.Boot;
 import flash.Lib;
@@ -292,7 +303,9 @@ import flash.Camera;
 import flash.Color;
 import flash.ContextMenu;
 import flash.ContextMenuItem;
+#if flash_lite
 import flash.ExtendedKey;
+#end
 import flash.Key;
 import flash.LoadVars;
 import flash.LocalConnection;
@@ -392,12 +405,12 @@ import neko.db.Transaction;
 import neko.net.Host;
 import neko.net.Poll;
 import neko.net.ProxyDetect;
-import neko.net.RemotingServer;
+//import neko.net.RemotingServer;
 import neko.net.ServerLoop;
 import neko.net.Socket;
 import neko.net.SocketInput;
 import neko.net.SocketOutput;
-import neko.net.ThreadRemotingServer;
+//import neko.net.ThreadRemotingServer;
 import neko.net.ThreadServer;
 
 import neko.vm.Loader;
@@ -418,7 +431,7 @@ import js.Dom;
 import js.Selection;
 import js.SWFObject;
 import js.XMLHttpRequest;
-import js.XMLSocket;
+//import js.XMLSocket;
 
 #end
 
@@ -426,9 +439,9 @@ import js.XMLSocket;
 
 #if neko
 
-import tools.haxedoc.Main;
-import tools.haxelib.Main;
-import tools.haxelib.Site;
+//import tools.haxedoc.Main;
+//import tools.haxelib.Main;
+//import tools.haxelib.Site;
 //import tools.hxinst.Main -> needs xCross
 
 #end

+ 19 - 18
std/Bytes.hx → std/haxe/io/Bytes.hx

@@ -22,15 +22,16 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  */
+package haxe.io;
 
 class Bytes {
 
 	public var length(default,null) : Int;
 	#if neko
 	var b : Void; // neko-string
-	#else flash9
+	#elseif flash9
 	var b : flash.utils.ByteArray;
-	#else true
+	#else
 	var b : Array<Int>;
 	#end
 
@@ -42,9 +43,9 @@ class Bytes {
 	public inline function get( pos : Int ) : Int {
 		#if neko
 		return untyped __dollar__sget(b,pos);
-		#else flash9
+		#elseif flash9
 		return b[pos];
-		#else true
+		#else
 		return b[pos];
 		#end
 	}
@@ -52,9 +53,9 @@ class Bytes {
 	public inline function set( pos : Int, v : Int ) : Void {
 		#if neko
 		untyped __dollar__sset(b,pos,v);
-		#else flash9
+		#elseif flash9
 		b[pos] = v;
-		#else true
+		#else
 		b[pos] = v;
 		#end
 	}
@@ -65,10 +66,10 @@ class Bytes {
 		#end
 		#if neko
 		try untyped __dollar__sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw "Outside bounds";
-		#else flash9
+		#elseif flash9
 		b.position = pos;
 		b.writeBytes(src.b,srcpos,len);
-		#else true
+		#else
 		var b1 = b;
 		var b2 = src.b;
 		if( b1 == b2 && pos > srcpos ) {
@@ -87,7 +88,7 @@ class Bytes {
 	public function compare( other : Bytes ) : Int {
 		#if neko
 		return untyped __dollar__compare(b,other.b);
-		#else flash9
+		#elseif flash9
 		var len = (length < other.length) ? length : other.length;
 		var b1 = b;
 		var b2 = other.b;
@@ -103,7 +104,7 @@ class Bytes {
 			if( b1.readUnsignedByte() != b2.readUnsignedByte() )
 				return b1[b1.position-1] - b2[b2.position-1];
 		return length - other.length;
-		#else true
+		#else
 		var b1 = b;
 		var b2 = other.b;
 		var len = (length < other.length) ? length : other.length;
@@ -120,10 +121,10 @@ class Bytes {
 		#end
 		#if neko
 		return try new String(untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw "Outside bounds";
-		#else flash9
+		#elseif flash9
 		b.position = pos;
 		return b.readUTFBytes(len);
-		#else true
+		#else
 		var s = "";
 		var b = b;
 		var fcc = String.fromCharCode;
@@ -152,10 +153,10 @@ class Bytes {
 	public function toString() : String {
 		#if neko
 		return new String(untyped __dollar__ssub(b,0,length));
-		#else flash9
+		#elseif flash9
 		b.position = 0;
 		return b.readUTFBytes(length);
-		#else true
+		#else
 		return readString(0,length);
 		#end
 	}
@@ -163,11 +164,11 @@ class Bytes {
 	public static function alloc( length : Int ) : Bytes {
 		#if neko
 		return new Bytes(length,untyped __dollar__smake(length));
-		#else flash9
+		#elseif flash9
 		var b = new flash.utils.ByteArray();
 		b.length = length;
 		return new Bytes(length,b);
-		#else true
+		#else
 		var a = new Array();
 		for( i in 0...length )
 			a.push(0);
@@ -178,11 +179,11 @@ class Bytes {
 	public static function ofString( s : String ) : Bytes {
 		#if neko
 		return new Bytes(s.length,untyped __dollar__ssub(s.__s,0,s.length));
-		#else flash9
+		#elseif flash9
 		var b = new flash.utils.ByteArray();
 		b.writeUTFBytes(s);
 		return new Bytes(b.length,b);
-		#else true
+		#else
 		var a = new Array();
 		// utf8-decode
 		for( i in 0...s.length ) {

+ 87 - 0
std/haxe/io/BytesBuffer.hx

@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+class BytesBuffer {
+
+	#if neko
+	var b : Void; // neko string buffer
+	#elseif flash9
+	var b : flash.utils.ByteArray;
+	#else
+	var b : Array<Int>;
+	#end
+
+	public function new() {
+		#if neko
+		b = untyped StringBuf.__make();
+		#elseif flash9
+		b = new flash.utils.ByteArray();
+		#else
+		b = new Array();
+		#end
+	}
+
+	public inline function add( byte : Int ) {
+		#if neko
+		untyped StringBuf.__add_char(b,byte);
+		#elseif flash9
+		b.writeByte(byte);
+		#else
+		b.push(byte);
+		#end
+	}
+
+	public inline function addBytes( src : Bytes, pos : Int, len : Int ) {
+		#if neko
+		untyped StringBuf.__add_sub(b,src.b,pos,len);
+		#elseif flash9
+		b.writeBytes(untyped src.b,pos,len);
+		#else
+		var b1 = b;
+		var b2 = untyped src.b;
+		for( i in pos...pos+len )
+			b.push(b2[i]);
+		#end
+	}
+
+	/**
+		Returns either a copy or a reference of the current bytes.
+		Once called, the buffer can no longer be used.
+	**/
+	public function getBytes() : Bytes untyped {
+		#if neko
+		var str = StringBuf.__string(b);
+		var bytes =  new Bytes(__dollar__ssize(str),str);
+		#elseif flash9
+		var bytes = new Bytes(b.position,b);
+		#else
+		var bytes = new Bytes(b.length,b);
+		#end
+		b = null;
+		return bytes;
+	}
+
+}

+ 72 - 0
std/haxe/io/BytesInput.hx

@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+class BytesInput extends Input {
+
+	#if neko
+	var b : Void; // neko string
+	#else flash9
+	var b : flash.utils.ByteArray;
+	#else
+	var b : Array<Int>;
+	#end
+	var pos : Int;
+	var len : Int;
+
+	public function new( b : Bytes, ?pos : Int, ?len : Int ) {
+		if( pos == null ) pos = 0;
+		if( len == null ) len = b.length - pos;
+		if( pos < 0 || len < 0 || pos + len > b.length )
+			throw "Outside bounds";
+		this.b = untyped b.b;
+		this.pos = pos;
+		this.len = len;
+	}
+
+	public override function readChar() {
+		if( this.len == 0 )
+			throw new Eof();
+		#if neko
+		var c = untyped __dollar__sget(b.__s,pos++);
+		#else
+		var c = b[pos++];
+		#end
+		len--;
+		return c;
+	}
+
+	public override function readBytes( buf : Bytes, bpos, blen ) : Int {
+		if( len == 0 && blen > 0 )
+			throw new Eof();
+		if( len < blen )
+			blen = len;
+		untyped __dollar__sblit(buf.__s,bpos,s.__s,pos,blen);
+		pos += blen;
+		len -= blen;
+		return blen;
+	}
+
+}

+ 48 - 0
std/haxe/io/BytesOutput.hx

@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+class BytesOutput extends Output {
+
+	var b : BytesBuffer;
+
+	public function new() {
+		b = new BytesBuffer();
+	}
+
+	override function writeChar(c) {
+		b.add(c);
+	}
+
+	override function writeBytes( buf, bpos, blen ) : Int {
+		b.addBytes(buf,bpos,blen);
+		return blen;
+	}
+
+	public function getBytes() {
+		return b.getBytes();
+	}
+
+}

+ 36 - 0
std/haxe/io/Eof.hx

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+/**
+	This exception is raised when reading while data is no longer available in the [Input].
+**/
+class Eof {
+	public function new() {
+	}
+	function toString() {
+		return "Eof";
+	}
+}

+ 37 - 0
std/haxe/io/Error.hx

@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+/**
+	The possible IO errors that can occur
+**/
+enum Error {
+	/** The IO is set into nonblocking mode and some data cannot be read or written **/
+	Blocked;
+	/** An operation is outside of its valid range **/
+	Overflow;
+	/** Other errors **/
+	Custom( e : Dynamic );
+}

+ 223 - 0
std/haxe/io/Input.hx

@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+/**
+	An Input is an abstract reader. See other classes in the [haxe.io] package
+	for several possible implementations.
+**/
+class Input {
+
+	public var bigEndian(default,setEndian) : Bool;
+
+	public function readByte() : Int {
+		return throw "Not implemented";
+	}
+
+	public function readBytes( s : Bytes, pos : Int, len : Int ) : Int {
+		var k = len;
+		var b = untyped s.b;
+		#if !neko
+		if( pos < 0 || len < 0 || pos + len > s.length )
+			throw Error.Custom("Outside bounds");
+		#end
+		while( k > 0 ) {
+			#if neko
+				untyped __dollar__sset(b,pos,readByte());
+			#else
+				b[pos] = readByte();
+			#end
+			pos++;
+			k--;
+		}
+		return len;
+	}
+
+	public function close() {
+	}
+
+	function setEndian(b) {
+		bigEndian = b;
+		return b;
+	}
+
+	/* ------------------ API ------------------ */
+
+	public function readAll( ?bufsize : Int ) : Bytes {
+		if( bufsize == null )
+			bufsize = (1 << 14); // 16 Ko
+		var buf = Bytes.alloc(bufsize);
+		var total = new haxe.io.BytesBuffer();
+		try {
+			while( true ) {
+				var len = readBytes(buf,0,bufsize);
+				if( len == 0 )
+					throw Error.Blocked;
+				total.addBytes(buf,0,len);
+			}
+		} catch( e : Eof ) {
+		}
+		return total.getBytes();
+	}
+
+	public function readFullBytes( s : Bytes, pos : Int, len : Int ) {
+		while( len > 0 ) {
+			var k = readBytes(s,pos,len);
+			pos += k;
+			len -= k;
+		}
+	}
+
+	public function read( nbytes : Int ) : Bytes {
+		var s = Bytes.alloc(nbytes);
+		var p = 0;
+		while( nbytes > 0 ) {
+			var k = readBytes(s,p,nbytes);
+			if( k == 0 ) throw Error.Blocked;
+			p += k;
+			nbytes -= k;
+		}
+		return s;
+	}
+
+	public function readUntil( end : Int ) : String {
+		var buf = new StringBuf();
+		var last : Int;
+		while( (last = readByte()) != end )
+			buf.addChar( last );
+		return buf.toString();
+	}
+
+	public function readLine() : String {
+		var buf = new StringBuf();
+		var last : Int;
+		var s;
+		try {
+			while( (last = readByte()) != 10 )
+				buf.addChar( last );
+			s = buf.toString();
+			if( s.charCodeAt(s.length-1) == 13 ) s = s.substr(0,-1);
+		} catch( e : Eof ) {
+			s = buf.toString();
+			if( s.length == 0 )
+				#if neko neko.Lib.rethrow #else throw #end (e);
+		}
+		return s;
+	}
+
+	public function readFloat() : Float {
+		#if neko
+			return _float_of_bytes(untyped read(4).b,bigEndian);
+		#else
+			throw "Not implemented";
+			return 0;
+		#end
+	}
+
+	public function readDouble() : Float {
+		#if neko
+			return _double_of_bytes(untyped read(8).b,bigEndian);
+		#else
+			throw "Not implemented";
+			return 0;
+		#end
+	}
+
+	public function readInt8() {
+		var n = readByte();
+		if( n >= 128 )
+			return n - 256;
+		return n;
+	}
+
+	public function readInt16() {
+		var ch1 = readByte();
+		var ch2 = readByte();
+		var n = bigEndian ? ch2 | (ch1 << 8) : ch1 | (ch2 << 8);
+		if( n & 0x8000 != 0 )
+			return n - 0x10000;
+		return n;
+	}
+
+	public function readUInt16() {
+		var ch1 = readByte();
+		var ch2 = readByte();
+		return bigEndian ? ch2 | (ch1 << 8) : ch1 | (ch2 << 8);
+	}
+
+	public function readInt24() {
+		var ch1 = readByte();
+		var ch2 = readByte();
+		var ch3 = readByte();
+		var n = bigEndian ? ch3 | (ch2 << 8) | (ch1 << 16) : ch1 | (ch2 << 8) | (ch3 << 16);
+		if( n & 0x800000 != 0 )
+			return n - 0x1000000;
+		return n;
+	}
+
+	public function readUInt24() {
+		var ch1 = readByte();
+		var ch2 = readByte();
+		var ch3 = readByte();
+		return bigEndian ? ch3 | (ch2 << 8) | (ch1 << 16) : ch1 | (ch2 << 8) | (ch3 << 16);
+	}
+
+	public function readInt31() {
+		var ch1,ch2,ch3,ch4;
+		if( bigEndian ) {
+			ch4 = readByte();
+			ch3 = readByte();
+			ch2 = readByte();
+			ch1 = readByte();
+		} else {
+			ch1 = readByte();
+			ch2 = readByte();
+			ch3 = readByte();
+			ch4 = readByte();
+		}
+		if( (ch4 & 128) != 0 ) {
+			if( ch4 & 64 == 0 ) throw Error.Overflow;
+			return ch1 | (ch2 << 8) | (ch3 << 16) | ((ch4 & 127) << 24);
+		} else {
+			if( ch4 & 64 != 0 ) throw Error.Overflow;
+			return ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
+		}
+	}
+
+	public function readUInt31() {
+		var ch1 = readByte();
+		var ch2 = readByte();
+		var ch3 = readByte();
+		var ch4 = readByte();
+		if( (bigEndian?ch1:ch4) >= 64 ) throw Error.Overflow;
+		return bigEndian ? ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24) : ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
+	}
+
+#if neko
+	static var _float_of_bytes = neko.Lib.load("std","float_of_bytes",2);
+	static var _double_of_bytes = neko.Lib.load("std","double_of_bytes",2);
+#end
+
+}

+ 206 - 0
std/haxe/io/Output.hx

@@ -0,0 +1,206 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.io;
+
+/**
+	An Output is an abstract write. A specific output implementation will only
+	have to override the [writeChar] and maybe the [write], [flush] and [close]
+	methods. See [File.write] and [String.write] for two ways of creating an
+	Output.
+**/
+class Output {
+
+	public var bigEndian(default,setEndian) : Bool;
+
+	public function writeByte( c : Int ) : Void {
+		throw "Not implemented";
+	}
+
+	public function writeBytes( s : Bytes, pos : Int, len : Int ) : Int {
+		var k = len;
+		var b = untyped s.b;
+		#if !neko
+		if( pos < 0 || len < 0 || pos + len > s.length )
+			throw Error.Custom("Outside bounds");
+		#end
+		while( k > 0 ) {
+			#if neko
+				writeByte(untyped __dollar__sget(b,pos));
+			#else
+				writeByte(b[pos]);
+			#end
+			pos++;
+			k--;
+		}
+		return len;
+	}
+
+	public function flush() {
+	}
+
+	public function close() {
+	}
+
+	function setEndian( b ) {
+		bigEndian = b;
+		return b;
+	}
+
+	/* ------------------ API ------------------ */
+
+	public function write( s : Bytes ) : Void {
+		var l = s.length;
+		var p = 0;
+		while( l > 0 ) {
+			var k = writeBytes(s,p,l);
+			if( k == 0 ) throw Error.Blocked;
+			p += k;
+			l -= k;
+		}
+	}
+
+	public function writeFullBytes( s : Bytes, pos : Int, len : Int ) {
+		while( len > 0 ) {
+			var k = writeBytes(s,pos,len);
+			pos += k;
+			len -= k;
+		}
+	}
+
+	public function writeFloat( c : Float ) {
+		#if neko
+		write(untyped new Bytes(4,_float_bytes(c,bigEndian)));
+		#else
+		throw "Not implemented";
+		#end
+	}
+
+	public function writeDouble( c : Float ) {
+		#if neko
+		write(untyped new Bytes(8,_double_bytes(c,bigEndian)));
+		#else
+		throw "Not implemented";
+		#end
+	}
+
+	public function writeInt8( c : Int ) {
+		if( c < -0x80 || c > 0x7F )
+			throw Error.Overflow;
+		writeByte(c & 0xFF);
+	}
+
+	public function writeInt16( x : Int ) {
+		if( x < 0 )
+			writeUInt16(0x10000 + x);
+		else
+			writeUInt16(x);
+	}
+
+	public function writeUInt16( x : Int ) {
+		if( x < 0 || x > 0xFFFF ) throw Error.Overflow;
+		if( bigEndian ) {
+			writeByte(x >> 8);
+			writeByte(x & 0xFF);
+		} else {
+			writeByte(x & 0xFF);
+			writeByte(x >> 8);
+		}
+	}
+
+	public function writeInt24( x : Int ) {
+		if( x < 0 )
+			writeUInt24(0x1000000 + x);
+		else
+			writeUInt24(x);
+	}
+
+	public function writeUInt24( x : Int ) {
+		if( x < 0 || x > 0xFFFFFF ) throw Error.Overflow;
+		if( bigEndian ) {
+			writeByte(x >> 16);
+			writeByte((x >> 8) & 0xFF);
+			writeByte(x & 0xFF);
+		} else {
+			writeByte(x & 0xFF);
+			writeByte((x >> 8) & 0xFF);
+			writeByte(x >> 16);
+		}
+	}
+
+	public function writeInt31( x : Int ) {
+		writeUInt31(x);
+	}
+
+	public function writeUInt31( x : Int ) {
+		if( bigEndian ) {
+			writeByte(x >>> 24);
+			writeByte((x >> 16) & 0xFF);
+			writeByte((x >> 8) & 0xFF);
+			writeByte(x & 0xFF);
+		} else {
+			writeByte(x & 0xFF);
+			writeByte((x >> 8) & 0xFF);
+			writeByte((x >> 16) & 0xFF);
+			writeByte(x >>> 24);
+		}
+	}
+
+	/**
+		Inform that we are about to write at least a specified number of bytes.
+		The underlying implementation can allocate proper working space depending
+		on this information, or simply ignore it. This is not a mandatory call
+		but a tip and is only used in some specific cases.
+	**/
+	public function prepare( nbytes : Int ) {
+	}
+
+	public function writeInput( i : Input, ?bufsize : Int ) {
+		if( bufsize == null )
+			bufsize = 4096;
+		var buf = Bytes.alloc(bufsize);
+		try {
+			while( true ) {
+				var len = i.readBytes(buf,0,bufsize);
+				if( len == 0 )
+					throw Error.Blocked;
+				var p = 0;
+				while( len > 0 ) {
+					var k = writeBytes(buf,p,len);
+					if( k == 0 )
+						throw Error.Blocked;
+					p += k;
+					len -= k;
+				}
+			}
+		} catch( e : Eof ) {
+		}
+	}
+
+#if neko
+	static var _float_bytes = neko.Lib.load("std","float_bytes",2);
+	static var _double_bytes = neko.Lib.load("std","double_bytes",2);
+#end
+
+}

+ 7 - 1
tests/unit/Test.hx

@@ -6,10 +6,12 @@ class Test {
 	}
 
 	function eq<T>( v : T, v2 : T, ?pos : haxe.PosInfos ) {
+		count++;
 		if( v != v2 ) report(v+" != "+v2,pos);
 	}
 
 	function exc( f : Void -> Void, ?pos : haxe.PosInfos ) {
+		count++;
 		try {
 			f();
 			report("No exception occured",pos);
@@ -18,6 +20,7 @@ class Test {
 	}
 
 	function unspec( f : Void -> Void, ?pos : haxe.PosInfos ) {
+		count++;
 		try {
 			f();
 		} catch( e : Dynamic ) {
@@ -25,6 +28,7 @@ class Test {
 	}
 
 	function allow<T>( v : T, values : Array<T>, ?pos : haxe.PosInfos ) {
+		count++;
 		for( v2 in values )
 			if( v == v2 )
 				return;
@@ -35,6 +39,7 @@ class Test {
 		reportInfos = m;
 	}
 
+	static var count = 0;
 	static var reportInfos = null;
 	static var reportCount = 0;
 
@@ -54,6 +59,7 @@ class Test {
 		#end
 		var classes = [
 			new TestBytes(),
+			new TestIO(),
 		];
 		var current = null;
 		try {
@@ -63,7 +69,7 @@ class Test {
 					if( f.substr(0,4) == "test" )
 						Reflect.callMethod(inst,Reflect.field(inst,f),[]);
 			}
-			report("DONE",here);
+			report("DONE ["+count+" tests]",here);
 		} catch( e : Dynamic ) {
 			reportInfos = null;
 			var msg = "???";

+ 17 - 5
tests/unit/TestBytes.hx

@@ -3,7 +3,7 @@
 class TestBytes extends Test {
 
 	function test() {
-		var b = Bytes.alloc(10);
+		var b = haxe.io.Bytes.alloc(10);
 		eq(b.length,10);
 		// get-set
 		for( i in 0...10 )
@@ -16,13 +16,13 @@ class TestBytes extends Test {
 		unspec(function() b.set(11,20));
 		unspec(function() b.set(0,1000));
 		// ofString
-		var b2 = Bytes.ofString("ABCD");
+		var b2 = haxe.io.Bytes.ofString("ABCD");
 		eq(b2.length,4);
 		eq(b2.get(0),"A".charCodeAt(0));
 		eq(b2.get(1),"B".charCodeAt(0));
 		eq(b2.get(2),"C".charCodeAt(0));
 		eq(b2.get(3),"D".charCodeAt(0));
-		var b3 = Bytes.ofString("é");
+		var b3 = haxe.io.Bytes.ofString("é");
 		eq(b3.length,2);
 		eq(b3.get(0),0xC3);
 		eq(b3.get(1),0xA9);
@@ -56,7 +56,7 @@ class TestBytes extends Test {
 		eq(b.get(6),"D".charCodeAt(0));
 		eq(b.get(7),0);
 		// readString
-		var bs = Bytes.ofString("One é accent");
+		var bs = haxe.io.Bytes.ofString("One é accent");
 		bs.set(3,0); // cut betwen "One" and "é"
 		eq(bs.readString(0,3),"One");
 		eq(bs.readString(4,bs.length-4),"é accent");
@@ -82,7 +82,7 @@ class TestBytes extends Test {
 		];
 		for( s1 in strings )
 			for( s2 in strings ) {
-				var c = Bytes.ofString(s1).compare(Bytes.ofString(s2));
+				var c = haxe.io.Bytes.ofString(s1).compare(haxe.io.Bytes.ofString(s2));
 				infos("compare "+s1+" and "+s2);
 				eq( c < 0, s1 < s2 );
 				eq( c > 0, s1 > s2 );
@@ -91,4 +91,16 @@ class TestBytes extends Test {
 		infos(null);
 	}
 
+	function testBuffer() {
+		var out = new haxe.io.BytesBuffer();
+		for( i in 1...6 )
+			out.add(i);
+		out.addBytes( haxe.io.Bytes.ofString("ABCDEF"),1,3 );
+		var b = out.getBytes();
+		var str = "\x01\x02\x03\x04\x05BCD";
+		eq( b.length, str.length );
+		for( i in 0...str.length )
+			eq( b.get(i), str.charCodeAt(i) );
+	}
+
 }

+ 8 - 0
tests/unit/TestIO.hx

@@ -0,0 +1,8 @@
+package unit;
+
+class TestIO extends Test {
+
+	public function test() {
+	}
+
+}

+ 1 - 0
tests/unit/unit.hxp

@@ -6,5 +6,6 @@
   <files path="/">
     <file path="Test.hx" />
     <file path="TestBytes.hx" />
+    <file path="TestIO.hx" />
   </files>
 </haxe>