-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package sys.io; | ||
|
||
import haxe.io.Bytes; | ||
import haxe.io.Eof; | ||
import haxe.io.Error; | ||
import js.node.Buffer; | ||
import js.node.Fs; | ||
|
||
@:coreApi | ||
class FileOutput extends haxe.io.Output { | ||
var fd:Int; | ||
var pos:Int; | ||
|
||
@:allow(sys.io.File) | ||
function new(fd:Int) { | ||
this.fd = fd; | ||
pos = 0; | ||
} | ||
|
||
override public function writeByte( b : Int ) : Void { | ||
var buf = new Buffer(1); | ||
buf[0] = b; | ||
Fs.writeSync(fd, buf, 0, 1, pos); | ||
pos++; | ||
} | ||
|
||
override public function writeBytes( s : Bytes, pos : Int, len : Int ) : Int { | ||
var buf = Buffer.hxFromBytes(s); | ||
var wrote = Fs.writeSync(fd, buf, pos, len, this.pos); | ||
this.pos += wrote; | ||
return wrote; | ||
} | ||
|
||
override public function close() : Void { | ||
Fs.closeSync(fd); | ||
} | ||
|
||
public function seek( p : Int, pos : FileSeek ) : Void { | ||
switch (pos) { | ||
case SeekBegin: | ||
this.pos = p; | ||
case SeekEnd: | ||
this.pos = cast Fs.fstatSync(fd).size + p; | ||
case SeekCur: | ||
this.pos += p; | ||
} | ||
} | ||
|
||
public function tell() : Int { | ||
return pos; | ||
} | ||
|
||
} |