class · System.IO
FileStream
public class FileStream : Stream
A file read and written in ranges of bytes, at a position, through a buffer.
- Kind
- class
- Namespace
- System.IO
- Assembly
- System.Core
- Access
- public
- Source file
Stream.ant
Inheritance
Constructors
new(str path, FileMode mode, FileAccess access)
#
new(str path, FileMode mode, FileAccess access, i32 bufferSize)
#
Properties
Methods
override Seek(i64 offset, SeekOrigin origin) : i64
#
Moves to offset from the start (0), the position (1) or the end (2): the new position.
override Flush()
#
override Close()
#
Private and internal members (5)
Inherited members (12)
SetLengthmethod from StreamReadmethod from StreamWritemethod from StreamReadExactlymethod from StreamReadToEndmethod from StreamCopyTomethod from StreamCopyTomethod from StreamGetTypemethod from AnyUndescribedmethod from AnyToStringmethod from AnyHashmethod from Anydestroymethod from Any
Source
FileStream as Antril, decompiled from its Larva HIR.
// System.IO.FileStream — System.Core (decompiled from Larva HIR)
import System;
into namespace System.IO
{
/// A file read and written in ranges of bytes, at a position, through a buffer.
public class FileStream : Stream
{
BufferedStream buffered;
str path;
bool reads;
bool writes;
static extern("stream.open") OpenAt(u16[] path, i64 mode) : i64;
public:
new(str path, FileMode mode) : (path, mode, mode == FileMode.Append ? FileAccess.Write : mode == FileMode.Open ? FileAccess.Read : FileAccess.ReadWrite)
{
}
new(str path, FileMode mode, FileAccess access) : (path, mode, access, 4096)
{
}
new(str path, FileMode mode, FileAccess access, i32 bufferSize)
{
this.path = path;
i64 bits = access == FileAccess.Read ? 1 : access == FileAccess.Write ? 2 : 3;
if (mode == FileMode.Create)
bits = bits | 4 | 8;
else if (mode == FileMode.CreateNew)
bits = bits | 4 | 32;
else if (mode == FileMode.OpenOrCreate)
bits = bits | 4;
else if (mode == FileMode.Truncate)
bits = bits | 8;
else if (mode == FileMode.Append)
bits = bits | 2 | 4 | 16;
i64 handle = OpenAt(Unicode.ToUtf16(path), bits);
if (handle < 0)
throw new IOError("cannot open " + path);
reads = access != FileAccess.Write;
writes = access != FileAccess.Read;
buffered = new BufferedStream(new HandleStream(handle, path, reads, writes, true), bufferSize);
}
override Read(u8[] buffer, i32 offset, i32 count) : i32
{
if (!reads)
throw new IOError("the file was not opened for reading: " + path);
return buffered.Read(buffer, offset, count);
}
override ReadByte() : i32 => buffered.ReadByte();
override Write(u8[] buffer, i32 offset, i32 count)
{
if (!writes)
throw new IOError("the file was not opened for writing: " + path);
buffered.Write(buffer, offset, count);
}
override WriteByte(u8 value)
{
buffered.WriteByte(value);
}
override Seek(i64 offset, SeekOrigin origin) : i64 => buffered.Seek(offset, origin);
/// Moves to `offset` from the start (0), the position (1) or the end (2): the new position.
Seek(i64 offset, i32 origin) : i64
{
return buffered.Seek(offset, origin == 0 ? SeekOrigin.Begin : origin == 1 ? SeekOrigin.Current : SeekOrigin.End);
}
override Flush()
{
buffered.Flush();
}
override Close()
{
buffered.Close();
}
/// The path it was opened with.
str Name
{
get => path;
}
override bool CanRead
{
get => buffered.CanRead;
}
override bool CanWrite
{
get => buffered.CanWrite;
}
override bool CanSeek
{
get => buffered.CanSeek;
}
override i64 Length
{
get => buffered.Length;
}
override i64 Position
{
get => buffered.Position;
set
{
buffered.Position = value;
}
}
}
}