瀏覽代碼

[java/cs] sys.io.File complete; various fixes to haxe.io.*

Caue Waneck 13 年之前
父節點
當前提交
cdd02dd040

+ 1 - 1
gencommon.ml

@@ -2369,7 +2369,7 @@ struct
               | _ -> 
                 [] 
             in
-            { e with eexpr = TTry(run ttry, nowrap_catches @ dyn_catch) }
+            { e with eexpr = TTry(run ttry, (List.rev nowrap_catches) @ dyn_catch) }
           | _ -> Type.map_expr run e
     in
     run

+ 96 - 0
std/cs/_std/sys/io/File.hx

@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2005-2012, 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 sys.io;
+
+/**
+	API for reading and writing to files.
+**/
+class File {
+
+	public static function getContent( path : String ) : String
+	{
+		var f = read(path, false);
+		var ret = f.readAll().toString();
+		f.close();
+		return ret;
+	}
+	
+	public static function saveContent( path : String, content : String ) : Void
+	{
+		var f = write(path, false);
+		f.writeString(content);
+		f.close();
+	}
+	
+	public static function getBytes( path : String ) : haxe.io.Bytes
+	{
+		var f = read(path, true);
+		var ret = f.readAll();
+		f.close();
+		return ret;
+	}
+	
+	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void
+	{
+		var f = write(path, true);
+		f.writeBytes(bytes, 0, bytes.length);
+		f.close();
+	}
+	
+	public static function read( path : String, binary : Bool = true ) : FileInput
+	{
+		#if std-buffer //standardize 4kb buffers
+		var stream = new system.io.FileStream(path, Open, Read, ReadWrite, 4096);
+		#else
+		var stream = new system.io.FileStream(path, Open, Read, ReadWrite);
+		#end
+		return new FileInput(stream);
+	}
+	
+	public static function write( path : String, binary : Bool = true ) : FileOutput
+	{
+		#if std-buffer //standardize 4kb buffers
+		var stream = new system.io.FileStream(path, Create, Write, ReadWrite, 4096);
+		#else
+		var stream = new system.io.FileStream(path, Create, Write, ReadWrite);
+		#end
+		return new FileOutput(stream);
+	}
+	
+	public static function append( path : String, binary : Bool = true ) : FileOutput
+	{
+		#if std-buffer //standardize 4kb buffers
+		var stream = new system.io.FileStream(path, Append, Write, ReadWrite, 4096);
+		#else
+		var stream = new system.io.FileStream(path, Append, Write, ReadWrite);
+		#end
+		return new FileOutput(stream);
+	}
+	
+	public static function copy( src : String, dst : String ) : Void
+	{
+		system.io.File.Copy(src, dst);
+	}
+}

+ 35 - 0
std/cs/_std/sys/io/FileInput.hx

@@ -0,0 +1,35 @@
+/*
+ * 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 sys.io;
+
+/**
+	Use [sys.io.File.read] to create a [FileInput]
+**/
+class FileInput extends NativeInput {
+	public function new(stream:system.io.FileStream)
+	{
+		super(stream);
+	}
+}

+ 35 - 0
std/cs/_std/sys/io/FileOutput.hx

@@ -0,0 +1,35 @@
+/*
+ * 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 sys.io;
+
+/**
+	Use [sys.io.File.write] to create a [FileOutput]
+**/
+class FileOutput extends NativeOutput {
+	public function new(stream:system.io.FileStream)
+	{
+		super(stream);
+	}
+}

+ 58 - 0
std/cs/_std/sys/io/NativeInput.hx

@@ -0,0 +1,58 @@
+package sys.io;
+import haxe.Int64;
+import haxe.io.Bytes;
+import haxe.io.Input;
+
+class NativeInput extends Input
+{
+	public var canSeek(get_canSeek, null):Bool;
+	
+	var stream:system.io.Stream;
+	public function new(stream)
+	{
+		this.stream = stream;
+		if (!stream.CanRead) throw "Write-only stream";
+	}
+	
+	override public function readByte():Int 
+	{
+		return stream.ReadByte();
+	}
+	
+	override public function readBytes(s:Bytes, pos:Int, len:Int):Int 
+	{
+		return stream.Read(s.getData(), pos, len);
+	}
+	
+	override public function close():Void
+	{
+		stream.Close();
+	}
+	
+	private function get_canSeek():Bool
+	{
+		return stream.CanSeek;
+	}
+	
+	public function seek( p : Int, pos : FileSeek ) : Void
+	{
+		var p = switch(pos)
+		{
+			case SeekBegin: system.io.SeekOrigin.Begin;
+			case SeekCur: system.io.SeekOrigin.Current;
+			case SeekEnd: system.io.SeekOrigin.End;
+		};
+		
+		stream.Seek(cast(p, Int64), p);
+	}
+	
+	public function tell() : Int
+	{
+		return cast(stream.Position, Int);
+	}
+	
+	public function eof() : Bool
+	{
+		return stream.Position == stream.Length;
+	}
+}

+ 59 - 0
std/cs/_std/sys/io/NativeOutput.hx

@@ -0,0 +1,59 @@
+package sys.io;
+import haxe.Int64;
+import haxe.io.Bytes;
+import haxe.io.Output;
+
+class NativeOutput extends Output
+{
+	public var canSeek(get_canSeek, null):Bool;
+	
+	var stream:system.io.Stream;
+	public function new(stream)
+	{
+		this.stream = stream;
+		if (!stream.CanWrite) throw "Read-only stream";
+	}
+	
+	override public function writeByte(c:Int):Void 
+	{
+		stream.WriteByte(c);
+	}
+	
+	override public function close():Void
+	{
+		stream.Close();
+	}
+	
+	override public function flush():Void
+	{
+		stream.Flush();
+	}
+	
+	override public function prepare(nbytes:Int):Void
+	{
+		//TODO see if implementation is correct
+		stream.SetLength(haxe.Int64.add(stream.Length, cast(nbytes, Int64)));
+	}
+	
+	private function get_canSeek():Bool
+	{
+		return stream.CanSeek;
+	}
+	
+	public function seek( p : Int, pos : FileSeek ) : Void
+	{
+		var p = switch(pos)
+		{
+			case SeekBegin: system.io.SeekOrigin.Begin;
+			case SeekCur: system.io.SeekOrigin.Current;
+			case SeekEnd: system.io.SeekOrigin.End;
+		};
+		
+		stream.Seek(cast(p, Int64), p);
+	}
+	
+	public function tell() : Int
+	{
+		return cast(stream.Position, Int);
+	}
+}

+ 2 - 2
std/cs/_std/system/io/Stream.hx

@@ -9,8 +9,8 @@ import haxe.io.BytesData;
 	var CanSeek(default, null):Bool;
 	var CanTimeout(default, null):Bool;
 	var CanWrite(default, null):Bool;
-	var Length(default, null):Bool;
-	var Position(default, null):Bool;
+	var Length(default, null):Int64;
+	var Position(default, null):Int64;
 	var ReadTimeout:Bool;
 	var WriteTimeout:Bool;
 	

+ 16 - 34
std/haxe/io/Input.hx

@@ -213,21 +213,14 @@ class Input {
 		#elseif java
 			if (helper == null) helper = java.nio.ByteBuffer.allocateDirect(8);
 			var helper = helper;
+			helper.order(bigEndian ? java.nio.ByteOrder.BIG_ENDIAN : java.nio.ByteOrder.LITTLE_ENDIAN);
 			
-			if ( (helper.order() == java.nio.ByteOrder.BIG_ENDIAN) == bigEndian )
-			{
-				helper.putChar(0, readByte());
-				helper.putChar(1, readByte());
-				helper.putChar(2, readByte());
-				helper.putChar(3, readByte());
-			} else {
-				helper.putChar(3, readByte());
-				helper.putChar(2, readByte());
-				helper.putChar(1, readByte());
-				helper.putChar(0, readByte());
-			}
+			helper.put(0, readByte());
+			helper.put(1, readByte());
+			helper.put(2, readByte());
+			helper.put(3, readByte());
 			
-			return helper.getFloat();
+			return helper.getFloat(0);
 		#else
 			var bytes = [];
 			bytes.push(readByte());
@@ -301,29 +294,18 @@ class Input {
 		#elseif java
 		if (helper == null) helper = java.nio.ByteBuffer.allocateDirect(8);
 		var helper = helper;
+		helper.order(bigEndian ? java.nio.ByteOrder.BIG_ENDIAN : java.nio.ByteOrder.LITTLE_ENDIAN);
 		
-		if ( (helper.order() == java.nio.ByteOrder.BIG_ENDIAN) == bigEndian )
-		{
-			helper.putChar(0, readByte());
-			helper.putChar(1, readByte());
-			helper.putChar(2, readByte());
-			helper.putChar(3, readByte());
-			helper.putChar(4, readByte());
-			helper.putChar(5, readByte());
-			helper.putChar(6, readByte());
-			helper.putChar(7, readByte());
-		} else {
-			helper.putChar(7, readByte());
-			helper.putChar(6, readByte());
-			helper.putChar(5, readByte());
-			helper.putChar(4, readByte());
-			helper.putChar(3, readByte());
-			helper.putChar(2, readByte());
-			helper.putChar(1, readByte());
-			helper.putChar(0, readByte());
-		}
+		helper.put(0, readByte());
+		helper.put(1, readByte());
+		helper.put(2, readByte());
+		helper.put(3, readByte());
+		helper.put(4, readByte());
+		helper.put(5, readByte());
+		helper.put(6, readByte());
+		helper.put(7, readByte());
 		
-		return helper.getDouble();
+		return helper.getDouble(0);
 		#else
 		return throw "not implemented";
 		#end

+ 15 - 32
std/haxe/io/Output.hx

@@ -121,20 +121,13 @@ class Output {
 		#elseif java
 		if (helper == null) helper = java.nio.ByteBuffer.allocateDirect(8);
 		var helper = helper;
+		helper.order(bigEndian ? java.nio.ByteOrder.BIG_ENDIAN : java.nio.ByteOrder.LITTLE_ENDIAN);
 		
 		helper.putFloat(0, x);
-		if ( (helper.order() == java.nio.ByteOrder.BIG_ENDIAN) == bigEndian )
-		{
-			writeByte(helper.get(0));
-			writeByte(helper.get(1));
-			writeByte(helper.get(2));
-			writeByte(helper.get(3));
-		} else {
-			writeByte(helper.get(3));
-			writeByte(helper.get(2));
-			writeByte(helper.get(1));
-			writeByte(helper.get(0));
-		}
+		writeByte(helper.get(0));
+		writeByte(helper.get(1));
+		writeByte(helper.get(2));
+		writeByte(helper.get(3));
 		#else
 		if (x == 0.0)
 		{
@@ -190,28 +183,18 @@ class Output {
 		#elseif java
 		if (helper == null) helper = java.nio.ByteBuffer.allocateDirect(8);
 		var helper = helper;
+		helper.order(bigEndian ? java.nio.ByteOrder.BIG_ENDIAN : java.nio.ByteOrder.LITTLE_ENDIAN);
 		
 		helper.putDouble(0, x);
-		if ( (helper.order() == java.nio.ByteOrder.BIG_ENDIAN) == bigEndian )
-		{
-			writeByte(helper.get(0));
-			writeByte(helper.get(1));
-			writeByte(helper.get(2));
-			writeByte(helper.get(3));
-			writeByte(helper.get(4));
-			writeByte(helper.get(5));
-			writeByte(helper.get(6));
-			writeByte(helper.get(7));
-		} else {
-			writeByte(helper.get(7));
-			writeByte(helper.get(6));
-			writeByte(helper.get(5));
-			writeByte(helper.get(4));
-			writeByte(helper.get(3));
-			writeByte(helper.get(2));
-			writeByte(helper.get(1));
-			writeByte(helper.get(0));
-		}
+	
+		writeByte(helper.get(0));
+		writeByte(helper.get(1));
+		writeByte(helper.get(2));
+		writeByte(helper.get(3));
+		writeByte(helper.get(4));
+		writeByte(helper.get(5));
+		writeByte(helper.get(6));
+		writeByte(helper.get(7));
 		#else
 		if (x == 0.0) 
 		{

+ 129 - 0
std/java/_std/sys/io/File.hx

@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2005-2012, 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 sys.io;
+
+/**
+	API for reading and writing to files.
+**/
+class File {
+
+	public static function getContent( path : String ) : String
+	{
+		var f = read(path, false);
+		var ret = f.readAll().toString();
+		f.close();
+		return ret;
+	}
+	
+	public static function saveContent( path : String, content : String ) : Void
+	{
+		var f = write(path, false);
+		f.writeString(content);
+		f.close();
+	}
+	
+	public static function getBytes( path : String ) : haxe.io.Bytes
+	{
+		var f = read(path, true);
+		var ret = f.readAll();
+		f.close();
+		return ret;
+	}
+	
+	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void
+	{
+		var f = write(path, true);
+		f.writeBytes(bytes, 0, bytes.length);
+		f.close();
+	}
+	
+	public static function read( path : String, binary : Bool = true ) : FileInput
+	{
+		try
+		{
+			return new FileInput( new java.io.RandomAccessFile(new java.io.File(path), "r") );
+		}
+		catch (e:Dynamic) //swallow checked exceptions
+		{
+			throw e;
+		}
+	}
+	
+	public static function write( path : String, binary : Bool = true ) : FileOutput
+	{
+		var f = new java.io.File(path);
+		if (f.exists())
+		{
+			f.delete();
+		}
+		
+		try
+		{
+			return new FileOutput( new java.io.RandomAccessFile(f, "rw") );
+		}
+		catch (e:Dynamic) //swallow checked exceptions
+		{
+			throw e;
+		}
+	}
+	
+	public static function append( path : String, binary : Bool = true ) : FileOutput
+	{
+		var f = new java.io.File(path);
+		
+		try
+		{
+			var ra = new java.io.RandomAccessFile(f, "rw");
+			if (f.exists())
+			{
+				ra.seek(f.length());
+			}
+			return new FileOutput( ra );
+		}
+		catch (e:Dynamic) //swallow checked exceptions
+		{
+			throw e;
+		}
+	}
+	
+	public static function copy( src : String, dst : String ) : Void
+	{
+		var r:FileInput = null;
+		var w:FileOutput = null;
+		try
+		{
+			r = read(src);
+			w = write(dst);
+			w.writeInput(r);
+		}
+		
+		catch (e:Dynamic)
+		{
+			if (r != null) r.close();
+			if (w != null) w.close();
+			throw e;
+		}
+	}
+}

+ 118 - 0
std/java/_std/sys/io/FileInput.hx

@@ -0,0 +1,118 @@
+/*
+ * 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 sys.io;
+import haxe.Int64;
+import haxe.io.Bytes;
+import haxe.io.Eof;
+import haxe.io.Input;
+import java.io.Exceptions;
+
+/**
+	Use [sys.io.File.read] to create a [FileInput]
+**/
+class FileInput extends Input {
+	var f:java.io.RandomAccessFile;
+	public function new(f)
+	{
+		this.f = f;
+	}
+	
+	override public function readByte():Int 
+	{
+		try
+		{
+			return f.readUnsignedByte();
+		}
+		
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function readBytes(s:Bytes, pos:Int, len:Int):Int 
+	{
+		try
+		{
+			return f.read(s.getData(), pos, len);
+		}
+		
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	public function seek( p : Int, pos : FileSeek ) : Void
+	{
+		try
+		{
+			switch(pos)
+			{
+				case SeekBegin: f.seek(cast p);
+				case SeekCur: f.seek(haxe.Int64.add(f.getFilePointer(), cast(p, Int64)));
+				case SeekEnd: f.seek(haxe.Int64.add(f.length(), cast p));
+			}
+		}
+		
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	function tell() : Int
+	{
+		try
+		{
+			return cast f.getFilePointer();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	function eof() : Bool
+	{
+		try
+		{
+			return f.getFilePointer() == f.length();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+}

+ 110 - 0
std/java/_std/sys/io/FileOutput.hx

@@ -0,0 +1,110 @@
+/*
+ * 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 sys.io;
+import haxe.io.Bytes;
+import haxe.io.Eof;
+import haxe.io.Output;
+
+import java.io.Exceptions;
+
+/**
+	Use [sys.io.File.write] to create a [FileOutput]
+**/
+class FileOutput extends Output {
+	var f:java.io.RandomAccessFile;
+	public function new(f)
+	{
+		this.f = f;
+	}
+	
+	override public function writeByte(c:Int):Void 
+	{
+		try
+		{
+			this.f.write(c);
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function write(s:Bytes):Void 
+	{
+		try
+		{
+			this.f.write(s.getData());
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function writeBytes(s:Bytes, pos:Int, len:Int):Int 
+	{
+		try
+		{
+			this.f.write(s.getData(), pos, len);
+			return len;
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	public function seek( p : Int, pos : FileSeek ) : Void
+	{
+		try
+		{
+			switch(pos)
+			{
+				case SeekBegin: f.seek(cast p);
+				case SeekCur: f.seek(haxe.Int64.add(f.getFilePointer(), cast(p, Int64)));
+				case SeekEnd: f.seek(haxe.Int64.add(f.length(), cast p));
+			}
+		}
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	function tell() : Int
+	{
+		try
+		{
+			return cast f.getFilePointer();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+}

+ 58 - 0
std/java/_std/sys/io/NativeInput.hx

@@ -0,0 +1,58 @@
+package sys.io;
+import haxe.Int64;
+import haxe.io.Bytes;
+import haxe.io.Eof;
+import haxe.io.Input;
+import java.io.Exceptions;
+
+class NativeInput extends Input
+{
+	var stream:java.io.InputStream;
+	public function new(stream)
+	{
+		this.stream = stream;
+	}
+	
+	override public function readByte():Int 
+	{
+		try
+		{
+			return stream.read();
+		} 
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function readBytes(s:Bytes, pos:Int, len:Int):Int 
+	{
+		try
+		{
+			return stream.Read(s.getDate(), pos, len);
+		}
+		
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function close():Void
+	{
+		try
+		{
+			stream.close();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+}

+ 55 - 0
std/java/_std/sys/io/NativeOutput.hx

@@ -0,0 +1,55 @@
+package sys.io;
+import haxe.Int64;
+import haxe.io.Bytes;
+import haxe.io.Eof;
+import haxe.io.Output;
+import java.io.Exceptions;
+
+class NativeOutput extends Output
+{
+	var stream:java.io.OutputStream;
+	public function new(stream)
+	{
+		this.stream = stream;
+	}
+	
+	override public function writeByte(c:Int):Void 
+	{
+		try
+		{
+			stream.write(c);
+		}
+		
+		catch (e:EOFException) {
+			throw new Eof();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function close():Void
+	{
+		try
+		{
+			stream.close();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+	
+	override public function flush():Void
+	{
+		try
+		{
+			stream.flush();
+		}
+		
+		catch (e:IOException) {
+			throw haxe.io.Error.Custom(e);
+		}
+	}
+}

+ 5 - 0
std/java/io/Exceptions.hx

@@ -0,0 +1,5 @@
+package java.io;
+import java.lang.Throwable;
+
+extern class IOException extends Exception { }
+extern class EOFException extends IOException { }

+ 50 - 0
std/java/io/RandomAccessFile.hx

@@ -0,0 +1,50 @@
+package java.io;
+import haxe.Int64;
+import haxe.io.BytesData;
+import java.StdTypes;
+
+//FIXME: this is incomplete
+extern class RandomAccessFile 
+{
+	function new(f:File, mode:String):Void;
+	
+	function close():Void;
+	function getFilePointer():Int64;
+	function length():Int64;
+	@:overload(function(b:BytesData, pos:Int, len:Int):Int {})
+	function read():Int;
+	
+	function readBoolean():Bool;
+	function readByte():Int8;
+	function readChar():Char16;
+	function readDouble():Float;
+	function readFloat():Single;
+	@:overload(function(b:BytesData, off:Int, len:Int):Void {})
+	function readFully(b:BytesData):Void;
+	function readInt():Int;
+	function readLine():String;
+	function readLong():Int64;
+	function readShort():Int16;
+	function readUnsignedByte():Int;
+	function readUnsignedShort():Int;
+	function readUTF():String;
+	
+	function seek(pos:Int64):Void;
+	function setLength(newLength:Int):Void;
+	function skipBytes(n:Int):Int;
+	
+	@:overload(function(b:BytesData, off:Int, len:Int):Void {})
+	@:overload(function(b:BytesData):Void {})
+	function write(i:Int):Void;
+	
+	function writeBoolean(b:Bool):Void;
+	function writeByte(v:Int):Void;
+	function writeChar(v:Int):Void;
+	function writeChars(s:String):Void;
+	function writeDouble(v:Float):Void;
+	function writeFloat(v:Single):Void;
+	function writeInt(v:Int):Void;
+	function writeLong(v:Int64):Void;
+	function writeShort(v:Int16):Void;
+	function writeUTF(str:String):Void;
+}