diff --git a/res/modules/internal/stream_providers/bytearray.lua b/res/modules/internal/stream_providers/bytearray.lua index 35a15b654..0f168c851 100644 --- a/res/modules/internal/stream_providers/bytearray.lua +++ b/res/modules/internal/stream_providers/bytearray.lua @@ -98,10 +98,6 @@ function lib.tell(descriptor) return positions[descriptor] end -function lib.flush(descriptor) - requireDescriptor(descriptor) -end - function lib.available(descriptor) requireDescriptor(descriptor) diff --git a/res/modules/internal/stream_providers/named_pipe_unix.lua b/res/modules/internal/stream_providers/named_pipe_unix.lua index 614c9aaf4..933f6292b 100644 --- a/res/modules/internal/stream_providers/named_pipe_unix.lua +++ b/res/modules/internal/stream_providers/named_pipe_unix.lua @@ -60,18 +60,6 @@ function lib.write(fd, bytearray) end end -function lib.seek(fd, mode, offset) - error("cannot seek the named pipe") -end - -function lib.tell(fd) - error("cannot tell the named pipe") -end - -function lib.flush(fd) - -- no flush on unix -end - function lib.available(fd) if fd == nil or fd < 0 then return 0 end diff --git a/res/modules/internal/stream_providers/named_pipe_windows.lua b/res/modules/internal/stream_providers/named_pipe_windows.lua index d79335f7d..817925b08 100644 --- a/res/modules/internal/stream_providers/named_pipe_windows.lua +++ b/res/modules/internal/stream_providers/named_pipe_windows.lua @@ -97,14 +97,6 @@ function lib.write(handle, bytearray) end end -function lib.seek(handle, mode, offset) - error("cannot seek the named pipe") -end - -function lib.tell(handle) - error("cannot tell the named pipe") -end - function lib.flush(handle) C.FlushFileBuffers(handle) end diff --git a/res/modules/internal/stream_providers/socket.lua b/res/modules/internal/stream_providers/socket.lua index 88684b0ff..0faedd695 100644 --- a/res/modules/internal/stream_providers/socket.lua +++ b/res/modules/internal/stream_providers/socket.lua @@ -32,18 +32,6 @@ function lib.write(descriptor, data) sockets[descriptor]:send(data) end -function lib.seek(descriptor, mode, offset) - error("cannot seek socket") -end - -function lib.tell(descriptor) - error("cannot tell socket") -end - -function lib.flush(descriptor) - requireDescriptor(descriptor) -end - function lib.available(descriptor) requireDescriptor(descriptor) diff --git a/res/modules/io_stream.lua b/res/modules/io_stream.lua index 3e91daa4e..ef1150237 100644 --- a/res/modules/io_stream.lua +++ b/res/modules/io_stream.lua @@ -47,15 +47,14 @@ binaryMode - if enabled, most methods will expect bytes instead of strings ioLib - I/O library. Should include the following functions: read(descriptor: int, length: int) -> Bytearray May return bytearray with a smaller size if bytes have not arrived yet or have run out + May throw error if descriptor isn't readable write(descriptor: int, data: Bytearray) - seek(descriptor: int, mode: string, offset: int) + May throw error if descriptor isn't writeable + [optional] seek(descriptor: int, mode: string, offset: int) Mode may be 'b' (relative begin), 'c' (relative current), 'e' (relative end + 1) - May throw error if environment is not support seek - tell(descriptor: int) -> int - May throw error if environment is not support tell - flush(descriptor: int) - available(descriptor: int) -> int - May return 0 if environment is not support available method + [optional] tell(descriptor: int) -> int + [optional] flush(descriptor: int) + [optional] available(descriptor: int) -> int is_alive(descriptor: int) -> bool close(descriptor: int) --]] @@ -135,7 +134,7 @@ function io_stream:available(length) available = #self.readBuffer else - available = self.ioLib.available(self.descriptor) + available = self.ioLib.available and self.ioLib.available(self.descriptor) or 0 end if not length then @@ -388,10 +387,18 @@ function io_stream:write(arg, ...) end function io_stream:seek(mode, offset) + if not self.ioLib.seek then + error("cannot seek this stream") + end + self.ioLib.seek(self.descriptor, mode, offset) end function io_stream:tell() + if not self.ioLib.tell then + error("cannot tell this stream") + end + return self.ioLib.tell(self.descriptor) end @@ -418,7 +425,11 @@ function io_stream:flush() self.writeBuffer:clear() end - if self.flushMode ~= FLUSH_MODE_ONLY_BUFFER then self.ioLib.flush(self.descriptor) end + if self.flushMode ~= FLUSH_MODE_ONLY_BUFFER then + if self.ioLib.flush then + self.ioLib.flush(self.descriptor) + elseif self:is_closed() then error("stream is closed") end + end end return io_stream