diff --git a/src/main/php/io/streams/FileInputStream.class.php b/src/main/php/io/streams/FileInputStream.class.php index cc9c6512f..ee76ee87b 100755 --- a/src/main/php/io/streams/FileInputStream.class.php +++ b/src/main/php/io/streams/FileInputStream.class.php @@ -72,12 +72,9 @@ public function seek($offset, $whence= SEEK_SET) { $this->file->seek($offset, $whence); } - /** - * Return current offset - * - * @return int offset - */ - public function tell() { - return $this->file->tell(); - } + /** @return int */ + public function tell() { return $this->file->tell(); } + + /** @return int */ + public function size() { return $this->file->size(); } } diff --git a/src/main/php/io/streams/FileOutputStream.class.php b/src/main/php/io/streams/FileOutputStream.class.php index e4f36523a..ed1c7ac69 100755 --- a/src/main/php/io/streams/FileOutputStream.class.php +++ b/src/main/php/io/streams/FileOutputStream.class.php @@ -48,6 +48,9 @@ public function seek($offset, $whence= SEEK_SET) { /** @return int */ public function tell() { return $this->file->tell(); } + /** @return int */ + public function size() { return $this->file->size(); } + /** * Truncate this buffer to a given new size. * diff --git a/src/test/php/io/unittest/FileInputStreamTest.class.php b/src/test/php/io/unittest/FileInputStreamTest.class.php index 3be2336de..3a1d3dda8 100755 --- a/src/test/php/io/unittest/FileInputStreamTest.class.php +++ b/src/test/php/io/unittest/FileInputStreamTest.class.php @@ -54,6 +54,13 @@ public function availability() { }); } + #[Test] + public function size() { + with (new FileInputStream($this->tempFile()), function($stream) { + Assert::equals(30, $stream->size()); + }); + } + #[Test] public function delete() { $file= $this->tempFile(); diff --git a/src/test/php/io/unittest/FileOutputStreamTest.class.php b/src/test/php/io/unittest/FileOutputStreamTest.class.php index e483d8dac..f64e6aa7f 100755 --- a/src/test/php/io/unittest/FileOutputStreamTest.class.php +++ b/src/test/php/io/unittest/FileOutputStreamTest.class.php @@ -6,11 +6,12 @@ use test\{Assert, Expect, Test}; class FileOutputStreamTest { + const INITIAL= 'Created by FileOutputStreamTest'; private $files= []; /** @return io.TempFile */ private function tempFile() { - $file= (new TempFile())->containing('Created by FileOutputStreamTest'); + $file= (new TempFile())->containing(self::INITIAL); $this->files[]= $file; return $file; } @@ -112,4 +113,39 @@ public function truncation() { Assert::equals('Exist', Files::read($file)); }); } + + #[Test] + public function size_initially() { + $stream= new FileOutputStream($this->tempFile()); + Assert::equals(0, $stream->size()); + } + + #[Test] + public function size_after_writing() { + $stream= new FileOutputStream($this->tempFile()); + $stream->write('Test'); + Assert::equals(4, $stream->size()); + } + + #[Test] + public function size_after_truncation() { + $stream= new FileOutputStream($this->tempFile()); + $stream->write('Test'); + $stream->truncate(2); + Assert::equals(2, $stream->size()); + } + + #[Test] + public function size_when_opened_with_append() { + $file= $this->tempFile(); + $file->open(File::APPEND); + + $stream= new FileOutputStream($file); + $stream->write('Test'); + $size= $stream->size(); + $stream->close(); + + Assert::equals(strlen(self::INITIAL) + 4, $size); + Assert::equals(self::INITIAL.'Test', Files::read($file)); + } } \ No newline at end of file