Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/main/php/io/streams/FileInputStream.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
}
3 changes: 3 additions & 0 deletions src/main/php/io/streams/FileOutputStream.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions src/test/php/io/unittest/FileInputStreamTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 37 additions & 1 deletion src/test/php/io/unittest/FileOutputStreamTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}
}
Loading