| 1 | <?php |
||
| 13 | class FileWriter extends OutputStreamWriter |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * constructor |
||
| 17 | * @param mixed $file ファイルオブジェクトまたはファイルパス |
||
| 18 | */ |
||
| 19 | public function __construct($file, bool $isAppend = false, int $bufferSize = null) |
||
| 20 | { |
||
| 21 | parent::__construct(new FileOutputStream($file, $isAppend)); |
||
| 22 | |||
| 23 | // fwriteのデフォルトバッファリングサイズは8KBなので、指定無しの場合は8KBになる |
||
| 24 | // また、同じストリームに対して出力を行うプロセスが複数ある場合、8KBごとに停止する |
||
| 25 | // see: http://php.net/manual/ja/function.stream-set-write-buffer.php |
||
| 26 | if ($bufferSize !== null && stream_set_write_buffer($this->stream, $bufferSize) !== 0) { |
||
| 27 | throw new IOException("Failed to change the buffer size."); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * ファイルに書き込む |
||
| 33 | * @param mixed $data 書き込みデータ |
||
| 34 | */ |
||
| 35 | public function write($data) |
||
| 36 | { |
||
| 37 | $this->stream->write($data); |
||
| 38 | } |
||
| 39 | } |
||
| 40 |