Skip to content

Commit 3a9ee3e

Browse files
authored
Merge pull request #353 from thekid/feature/file-stream-size
Add size() to both FileInputStream and FileOutputStream
2 parents 42093b7 + ea45472 commit 3a9ee3e

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

src/main/php/io/streams/FileInputStream.class.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@ public function seek($offset, $whence= SEEK_SET) {
7272
$this->file->seek($offset, $whence);
7373
}
7474

75-
/**
76-
* Return current offset
77-
*
78-
* @return int offset
79-
*/
80-
public function tell() {
81-
return $this->file->tell();
82-
}
75+
/** @return int */
76+
public function tell() { return $this->file->tell(); }
77+
78+
/** @return int */
79+
public function size() { return $this->file->size(); }
8380
}

src/main/php/io/streams/FileOutputStream.class.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function seek($offset, $whence= SEEK_SET) {
4848
/** @return int */
4949
public function tell() { return $this->file->tell(); }
5050

51+
/** @return int */
52+
public function size() { return $this->file->size(); }
53+
5154
/**
5255
* Truncate this buffer to a given new size.
5356
*

src/test/php/io/unittest/FileInputStreamTest.class.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public function availability() {
5454
});
5555
}
5656

57+
#[Test]
58+
public function size() {
59+
with (new FileInputStream($this->tempFile()), function($stream) {
60+
Assert::equals(30, $stream->size());
61+
});
62+
}
63+
5764
#[Test]
5865
public function delete() {
5966
$file= $this->tempFile();

src/test/php/io/unittest/FileOutputStreamTest.class.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use test\{Assert, Expect, Test};
77

88
class FileOutputStreamTest {
9+
const INITIAL= 'Created by FileOutputStreamTest';
910
private $files= [];
1011

1112
/** @return io.TempFile */
1213
private function tempFile() {
13-
$file= (new TempFile())->containing('Created by FileOutputStreamTest');
14+
$file= (new TempFile())->containing(self::INITIAL);
1415
$this->files[]= $file;
1516
return $file;
1617
}
@@ -112,4 +113,39 @@ public function truncation() {
112113
Assert::equals('Exist', Files::read($file));
113114
});
114115
}
116+
117+
#[Test]
118+
public function size_initially() {
119+
$stream= new FileOutputStream($this->tempFile());
120+
Assert::equals(0, $stream->size());
121+
}
122+
123+
#[Test]
124+
public function size_after_writing() {
125+
$stream= new FileOutputStream($this->tempFile());
126+
$stream->write('Test');
127+
Assert::equals(4, $stream->size());
128+
}
129+
130+
#[Test]
131+
public function size_after_truncation() {
132+
$stream= new FileOutputStream($this->tempFile());
133+
$stream->write('Test');
134+
$stream->truncate(2);
135+
Assert::equals(2, $stream->size());
136+
}
137+
138+
#[Test]
139+
public function size_when_opened_with_append() {
140+
$file= $this->tempFile();
141+
$file->open(File::APPEND);
142+
143+
$stream= new FileOutputStream($file);
144+
$stream->write('Test');
145+
$size= $stream->size();
146+
$stream->close();
147+
148+
Assert::equals(strlen(self::INITIAL) + 4, $size);
149+
Assert::equals(self::INITIAL.'Test', Files::read($file));
150+
}
115151
}

0 commit comments

Comments
 (0)