本文整理汇总了PHP中stream_set_chunk_size函数的典型用法代码示例。如果您正苦于以下问题:PHP stream_set_chunk_size函数的具体用法?PHP stream_set_chunk_size怎么用?PHP stream_set_chunk_size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stream_set_chunk_size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param resource $resource Stream resource.
* @param bool $autoClose True to close the resource on destruct, false to leave it open.
*/
public function __construct($resource, bool $autoClose = true)
{
parent::__construct($resource, $autoClose);
stream_set_write_buffer($resource, 0);
stream_set_chunk_size($resource, self::CHUNK_SIZE);
$this->writeQueue = new \SplQueue();
$this->onCancelled = function (Throwable $exception) {
$this->free($exception);
};
}
示例2: __construct
/**
* @param resource $resource Stream resource.
* @param bool $autoClose True to close the resource on destruct, false to leave it open.
*/
public function __construct($resource, bool $autoClose = true)
{
parent::__construct($resource, $autoClose);
stream_set_read_buffer($resource, 0);
stream_set_chunk_size($resource, self::CHUNK_SIZE);
$this->queue = new \SplQueue();
$this->poll = $this->createPoll($resource, $this->queue);
$this->onCancelled = function () {
$this->poll->cancel();
$this->queue->shift();
};
}
示例3: getPointer
/**
* Returns the pointer to the random source.
* @return resource The pointer to the random source.
*/
private function getPointer()
{
if (!isset($this->pointer)) {
$this->pointer = fopen($this->source, 'r');
// File read buffering is not supported on HHVM
if (!defined('HHVM_VERSION')) {
stream_set_chunk_size($this->pointer, 32);
stream_set_read_buffer($this->pointer, 32);
}
}
return $this->pointer;
}
示例4: __construct
function __construct($host, $port, $timeout_ms = 2000)
{
$timeout_f = (double) $timeout_ms / 1000;
$this->sock = @stream_socket_client("{$host}:{$port}", $errno, $errstr, $timeout_f);
if (!$this->sock) {
throw new SSDBException("{$errno}: {$errstr}");
}
$timeout_sec = intval($timeout_ms / 1000);
$timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000;
@stream_set_timeout($this->sock, $timeout_sec, $timeout_usec);
if (function_exists('stream_set_chunk_size')) {
@stream_set_chunk_size($this->sock, 1024 * 1024);
}
}
示例5: __construct
function __construct($stream, $eol = false)
{
$this->eol = $eol ?: null;
$this->ceol = strlen($eol ?: PHP_EOL);
if (!$eol) {
ini_set('auto_detect_line_endings', true);
}
if (is_resource($stream)) {
$this->stream = $stream;
} else {
$this->stream = fopen($stream, 'rb+');
}
stream_set_chunk_size($this->stream, $this->blocksize);
}
示例6: __construct
public function __construct($host, $port, $connection_timeout, $read_write_timeout = null, $context = null, $blocking = false)
{
$errstr = $errno = null;
$this->sock = null;
if ($context) {
$remote = sprintf('tls://%s:%s', $host, $port);
$this->sock = @stream_socket_client($remote, $errno, $errstr, $connection_timeout, STREAM_CLIENT_CONNECT, $context);
} else {
$remote = sprintf('tcp://%s:%s', $host, $port);
$this->sock = @stream_socket_client($remote, $errno, $errstr, $connection_timeout, STREAM_CLIENT_CONNECT);
}
if (!$this->sock) {
throw new RuntimeException("Error Connecting to server({$errno}): {$errstr} ");
}
if (null !== $read_write_timeout) {
if (!stream_set_timeout($this->sock, $read_write_timeout)) {
throw new \Exception("Timeout (stream_set_timeout) could not be set");
}
}
/**
* Manually set blocking & write buffer settings and make sure they are successfuly set
* Use non-blocking as we dont want to stuck waiting for socket data while fread() w/o timeout
*/
if (!stream_set_blocking($this->sock, $blocking)) {
throw new \Exception("Blocking could not be set");
}
$rbuff = stream_set_read_buffer($this->sock, 0);
if (!(0 === $rbuff)) {
throw new \Exception("Read buffer could not be set");
}
/**
* ! this is not reliably returns success (0)
* ! but default buffer is pretty high (few Kb), so will not affect sending single small pushes
*
* Output using fwrite() is normally buffered at 8K.
* This means that if there are two processes wanting to write to the same output stream (a file),
* each is paused after 8K of data to allow the other to write.
*
* Ensures that all writes with fwrite() are completed
* before other processes are allowed to write to that output stream
*/
stream_set_write_buffer($this->sock, 0);
/**
* Set small chunk size (default=4096/8192)
* Setting this to small values (100bytes) still does NOT help detecting feof()
*/
stream_set_chunk_size($this->sock, 1024);
}
示例7: testFeofReadWithoutStreamChunking
/**
* @group feof
* @group filesystemfunc
*/
function testFeofReadWithoutStreamChunking()
{
$fullFile = "{$this->basePath}/test";
$this->put('/test', 'abcd');
$h = fopen($fullFile, "r");
// See stream_chunk_ensure_quirk
stream_set_chunk_size($h, 1);
$this->assertFalse(feof($h));
fread($h, 2);
$this->assertFalse(feof($h));
fread($h, 2);
$this->assertFalse(feof($h));
fread($h, 1);
$this->assertTrue(feof($h));
fseek($h, 2);
$this->assertFalse(feof($h));
}
示例8: init
public function init()
{
parent::init();
$timeout_f = (double) $this->timeout_ms / 1000;
$this->sock = @stream_socket_client("{$this->host}:{$this->port}", $errno, $errstr, $timeout_f);
if (!$this->sock) {
throw new Exception("{$errno}: {$errstr}");
}
$timeout_sec = intval($this->timeout_ms / 1000);
$timeout_usec = ($this->timeout_ms - $timeout_sec * 1000) * 1000;
@stream_set_timeout($this->sock, $timeout_sec, $timeout_usec);
if (function_exists('stream_set_chunk_size')) {
@stream_set_chunk_size($this->sock, 1024 * 1024);
}
if (true === $this->easy) {
$this->easy();
}
}
示例9: __construct
/**
* A little bit messy but mainy to give useful errors. It's probably going to be the most common thing people hit.
*
* AbstractRegister constructor.
* @param BoardInterface $board
* @throws InternalFailureException
*/
public function __construct(BoardInterface $board)
{
//If there's a direct mapping file, try to use it.
//Kept this generic in case we eventually get a /dev/spimem, /dev/pwmmem etc
$dm_file = static::getDirectMemoryFile();
if ($dm_file !== null && file_exists($dm_file)) {
$this->mmap = @mmap_open($dm_file, self::MMAP_BLOCK_SIZE, static::getOffset());
if ($this->mmap === false) {
$reg_reflect = new \ReflectionClass($this);
throw new InternalFailureException(sprintf('Couldn\'t map %s register. You must either run as root, or be a member of the %s group.', $reg_reflect->getShortName(), posix_getgrgid(filegroup($dm_file))['name']));
}
} else {
$this->mmap = @mmap_open('/dev/mem', self::MMAP_BLOCK_SIZE, $board->getPeripheralBaseAddress() + static::getOffset());
if ($this->mmap === false) {
$reg_reflect = new \ReflectionClass($this);
throw new InternalFailureException(sprintf('Couldn\'t map %s register. Are you running as root?', $reg_reflect->getShortName()));
}
}
//Only read 4 bytes at a time, not PHP's 8k default
stream_set_chunk_size($this->mmap, 4);
//Should there be a 'register backup' in here that gets replayed on destruct?
}
示例10: __construct
/**
* @param resource $socket
* @param bool $autoClose True to close the resource on destruct, false to leave it open.
*/
public function __construct($socket, bool $autoClose = true)
{
parent::__construct($socket, $autoClose);
stream_set_read_buffer($socket, 0);
stream_set_write_buffer($socket, 0);
stream_set_chunk_size($socket, self::MAX_PACKET_SIZE);
$this->readQueue = new \SplQueue();
$this->writeQueue = new \SplQueue();
$this->poll = $this->createPoll($socket, $this->readQueue);
$this->await = $this->createAwait($socket, $this->writeQueue);
$this->onReceiveCancelled = function () {
$this->poll->cancel();
$this->readQueue->shift();
};
$this->onSendCancelled = function (\Exception $exception) {
$this->free($exception);
};
try {
list($this->address, $this->port) = Socket\getName($socket, false);
} catch (FailureException $exception) {
$this->close();
}
}
示例11: get_random_bytes
/**
* Get random bytes
*
* @param int $length Output length
* @return string
*/
public function get_random_bytes($length)
{
if (empty($length) or !ctype_digit((string) $length)) {
return FALSE;
}
// Unfortunately, none of the following PRNGs is guaranteed to exist ...
if (defined('MCRYPT_DEV_URANDOM') && ($output = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)) !== FALSE) {
return $output;
}
if (is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== FALSE) {
// Try not to waste entropy ...
is_php('5.4') && stream_set_chunk_size($fp, $length);
$output = fread($fp, $length);
fclose($fp);
if ($output !== FALSE) {
return $output;
}
}
if (function_exists('openssl_random_pseudo_bytes')) {
return openssl_random_pseudo_bytes($length);
}
return FALSE;
}
示例12: generateRandomKey
/**
* Generates specified number of random bytes.
* Note that output may not be ASCII.
* @see generateRandomString() if you need a string.
*
* @param int $length the number of bytes to generate
* @return string the generated random bytes
* @throws InvalidParamException if wrong length is specified
* @throws Exception on failure.
*/
public function generateRandomKey($length = 32)
{
if (!is_int($length)) {
throw new InvalidParamException('First parameter ($length) must be an integer');
}
if ($length < 1) {
throw new InvalidParamException('First parameter ($length) must be greater than 0');
}
// always use random_bytes() if it is available
if (function_exists('random_bytes')) {
return random_bytes($length);
}
// The recent LibreSSL RNGs are faster and likely better than /dev/urandom.
// Parse OPENSSL_VERSION_TEXT because OPENSSL_VERSION_NUMBER is no use for LibreSSL.
// https://bugs.php.net/bug.php?id=71143
if ($this->_useLibreSSL === null) {
$this->_useLibreSSL = defined('OPENSSL_VERSION_TEXT') && preg_match('{^LibreSSL (\\d\\d?)\\.(\\d\\d?)\\.(\\d\\d?)$}', OPENSSL_VERSION_TEXT, $matches) && 10000 * $matches[1] + 100 * $matches[2] + $matches[3] >= 20105;
}
// Since 5.4.0, openssl_random_pseudo_bytes() reads from CryptGenRandom on Windows instead
// of using OpenSSL library. LibreSSL is OK everywhere but don't use OpenSSL on non-Windows.
if ($this->_useLibreSSL || DIRECTORY_SEPARATOR !== '/' && substr_compare(PHP_OS, 'win', 0, 3, true) === 0 && function_exists('openssl_random_pseudo_bytes')) {
$key = openssl_random_pseudo_bytes($length, $cryptoStrong);
if ($cryptoStrong === false) {
throw new Exception('openssl_random_pseudo_bytes() set $crypto_strong false. Your PHP setup is insecure.');
}
if ($key !== false && StringHelper::byteLength($key) === $length) {
return $key;
}
}
// mcrypt_create_iv() does not use libmcrypt. Since PHP 5.3.7 it directly reads
// CryptGenRandom on Windows. Elsewhere it directly reads /dev/urandom.
if (function_exists('mcrypt_create_iv')) {
$key = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
if (StringHelper::byteLength($key) === $length) {
return $key;
}
}
// If not on Windows, try to open a random device.
if ($this->_randomFile === null && DIRECTORY_SEPARATOR === '/') {
// urandom is a symlink to random on FreeBSD.
$device = PHP_OS === 'FreeBSD' ? '/dev/random' : '/dev/urandom';
// Check random device for special character device protection mode. Use lstat()
// instead of stat() in case an attacker arranges a symlink to a fake device.
$lstat = @lstat($device);
if ($lstat !== false && ($lstat['mode'] & 0170000) === 020000) {
$this->_randomFile = fopen($device, 'rb') ?: null;
if (is_resource($this->_randomFile)) {
// Reduce PHP stream buffer from default 8192 bytes to optimize data
// transfer from the random device for smaller values of $length.
// This also helps to keep future randoms out of user memory space.
$bufferSize = 8;
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($this->_randomFile, $bufferSize);
}
// stream_set_read_buffer() isn't implemented on HHVM
if (function_exists('stream_set_chunk_size')) {
stream_set_chunk_size($this->_randomFile, $bufferSize);
}
}
}
}
if (is_resource($this->_randomFile)) {
$buffer = '';
$stillNeed = $length;
while ($stillNeed > 0) {
$someBytes = fread($this->_randomFile, $stillNeed);
if ($someBytes === false) {
break;
}
$buffer .= $someBytes;
$stillNeed -= StringHelper::byteLength($someBytes);
if ($stillNeed === 0) {
// Leaving file pointer open in order to make next generation faster by reusing it.
return $buffer;
}
}
fclose($this->_randomFile);
$this->_randomFile = null;
}
throw new Exception('Unable to generate a random key');
}
示例13: random_bytes
/**
* Unless open_basedir is enabled, use /dev/urandom for
* random numbers in accordance with best practices
*
* Why we use /dev/urandom and not /dev/random
* @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
static $fp = null;
/**
* This block should only be run once
*/
if (empty($fp)) {
/**
* We use /dev/urandom if it is a char device.
* We never fall back to /dev/random
*/
$fp = fopen('/dev/urandom', 'rb');
if (!empty($fp)) {
$st = fstat($fp);
if (($st['mode'] & 0170000) !== 020000) {
fclose($fp);
$fp = false;
}
}
if (!empty($fp)) {
/**
* stream_set_read_buffer() does not exist in HHVM
*
* If we don't set the stream's read buffer to 0, PHP will
* internally buffer 8192 bytes, which can waste entropy
*
* stream_set_read_buffer returns 0 on success
*/
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER);
}
if (function_exists('stream_set_chunk_size')) {
stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER);
}
}
}
try {
$bytes = RandomCompat_intval($bytes);
} catch (TypeError $ex) {
throw new TypeError('random_bytes(): $bytes must be an integer');
}
if ($bytes < 1) {
throw new Error('Length must be greater than 0');
}
/**
* This if() block only runs if we managed to open a file handle
*
* It does not belong in an else {} block, because the above
* if (empty($fp)) line is logic that should only be run once per
* page load.
*/
if (!empty($fp)) {
$remaining = $bytes;
$buf = '';
/**
* We use fread() in a loop to protect against partial reads
*/
do {
$read = fread($fp, $remaining);
if ($read === false) {
/**
* We cannot safely read from the file. Exit the
* do-while loop and trigger the exception condition
*/
$buf = false;
break;
}
/**
* Decrease the number of bytes returned from remaining
*/
$remaining -= RandomCompat_strlen($read);
$buf .= $read;
} while ($remaining > 0);
/**
* Is our result valid?
*/
if ($buf !== false) {
if (RandomCompat_strlen($buf) === $bytes) {
/**
* Return our random entropy buffer here:
*/
return $buf;
}
}
}
/**
* If we reach here, PHP has failed us.
//.........这里部分代码省略.........
示例14: _openSubprocess
//.........这里部分代码省略.........
$this->_debug($agentCommandLine);
$this->_agentProcess = proc_open($agentCommandLine, $agentDescriptorSpec, $this->_agentPipes, null, $env, array('binary_pipes' => true));
if (!is_resource($this->_agentProcess)) {
throw new Crypt_GPG_OpenSubprocessException('Unable to open gpg-agent subprocess.', 0, $agentCommandLine);
}
// Get GPG_AGENT_INFO and set environment variable for gpg process.
// This is a blocking read, but is only 1 line.
$agentInfo = fread($this->_agentPipes[self::FD_OUTPUT], self::CHUNK_SIZE);
// For GnuPG 2.1 we need to read both stderr and stdout
if ($version21) {
$agentInfo .= "\n" . fread($this->_agentPipes[self::FD_ERROR], self::CHUNK_SIZE);
}
if ($agentInfo) {
foreach (explode("\n", $agentInfo) as $line) {
if ($version21) {
if (preg_match('/listening on socket \'([^\']+)/', $line, $m)) {
$this->_agentInfo = $m[1];
} else {
if (preg_match('/gpg-agent\\[([0-9]+)\\].* started/', $line, $m)) {
$this->_agentInfo .= ':' . $m[1] . ':1';
}
}
} else {
if (preg_match('/GPG_AGENT_INFO=([^;]+)/', $line, $m)) {
$this->_agentInfo = $m[1];
break;
}
}
}
}
$this->_debug('GPG-AGENT-INFO: ' . $this->_agentInfo);
$env['GPG_AGENT_INFO'] = $this->_agentInfo;
// gpg-agent daemon is started, we can close the launching process
$this->_closeAgentLaunchProcess();
// Terminate processes if something went wrong
register_shutdown_function(array($this, '__destruct'));
}
// "Register" GPGConf existence for _closeIdleAgents()
if (version_compare($version, '2.1.0', 'ge')) {
if ($this->_gpgconf === null) {
$this->_gpgconf = $this->_getGPGConf();
}
} else {
$this->_gpgconf = false;
}
$commandLine = $this->_binary;
$defaultArguments = array('--status-fd ' . escapeshellarg(self::FD_STATUS), '--command-fd ' . escapeshellarg(self::FD_COMMAND), '--no-secmem-warning', '--no-tty', '--no-default-keyring', '--no-options');
if (version_compare($version, '1.0.7', 'ge')) {
if (version_compare($version, '2.0.0', 'lt')) {
$defaultArguments[] = '--no-use-agent';
}
$defaultArguments[] = '--no-permission-warning';
}
if (version_compare($version, '1.4.2', 'ge')) {
$defaultArguments[] = '--exit-on-status-write-error';
}
if (version_compare($version, '1.3.2', 'ge')) {
$defaultArguments[] = '--trust-model always';
} else {
$defaultArguments[] = '--always-trust';
}
// Since 2.1.13 we can use "loopback mode" instead of gpg-agent
if (version_compare($version, '2.1.13', 'ge')) {
$defaultArguments[] = '--pinentry-mode loopback';
}
$arguments = array_merge($defaultArguments, $this->_arguments);
if ($this->_homedir) {
$arguments[] = '--homedir ' . escapeshellarg($this->_homedir);
// the random seed file makes subsequent actions faster so only
// disable it if we have to.
if (!is_writeable($this->_homedir)) {
$arguments[] = '--no-random-seed-file';
}
}
if ($this->_publicKeyring) {
$arguments[] = '--keyring ' . escapeshellarg($this->_publicKeyring);
}
if ($this->_privateKeyring) {
$arguments[] = '--secret-keyring ' . escapeshellarg($this->_privateKeyring);
}
if ($this->_trustDb) {
$arguments[] = '--trustdb-name ' . escapeshellarg($this->_trustDb);
}
$commandLine .= ' ' . implode(' ', $arguments) . ' ' . $this->_operation;
$descriptorSpec = array(self::FD_INPUT => array('pipe', $rb), self::FD_OUTPUT => array('pipe', $wb), self::FD_ERROR => array('pipe', $wb), self::FD_STATUS => array('pipe', $wb), self::FD_COMMAND => array('pipe', $rb), self::FD_MESSAGE => array('pipe', $rb));
$this->_debug('OPENING GPG SUBPROCESS WITH THE FOLLOWING COMMAND:');
$this->_debug($commandLine);
$this->_process = proc_open($commandLine, $descriptorSpec, $this->_pipes, null, $env, array('binary_pipes' => true));
if (!is_resource($this->_process)) {
throw new Crypt_GPG_OpenSubprocessException('Unable to open GPG subprocess.', 0, $commandLine);
}
// Set streams as non-blocking. See Bug #18618.
foreach ($this->_pipes as $pipe) {
stream_set_blocking($pipe, 0);
stream_set_write_buffer($pipe, self::CHUNK_SIZE);
stream_set_chunk_size($pipe, self::CHUNK_SIZE);
stream_set_read_buffer($pipe, self::CHUNK_SIZE);
}
$this->_openPipes = $this->_pipes;
}
示例15: get_random_bytes
/**
* Get random bytes
*
* @ref https://github.com/paragonie/random_compat/
*
* @param int $length Output length
*
* @return string|false false on error
*/
public static function get_random_bytes($length)
{
if (!$length || !ctype_digit((string) $length)) {
return false;
} else {
$length = (int) $length;
}
if (function_exists('random_bytes') && self::is_php('7.0')) {
/**
* PHP 7 -> http://php.net/manual/de/function.random-bytes.php
*/
try {
$return = random_bytes($length);
} catch (\Exception $e) {
$return = false;
}
return $return;
} else {
/**
* PHP 5.2.0 - 5.6.x way to implement random_bytes()
*
* // WARNING: Unfortunately, none of the following PRNGs is guaranteed to exist ...
*
* In order of preference:
* 1. PHP-Module: "mcrypt" via mcrypt_create_iv()
* 2. Linux / BSD: "/dev/urandom" via fread()
* 3. Windows: \COM('CAPICOM.Utilities.1')->GetRandom()
* 4. PHP+OpenSSL: openssl_random_pseudo_bytes()
*/
/**
* 1. PHP-Module
*/
if (extension_loaded('mcrypt') && defined(MCRYPT_DEV_URANDOM) === true) {
$output = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
if ($output !== false && UTF8::strlen($output, '8bit') === $length) {
return $output;
}
}
/**
* 2. Linux / BSD
*/
if (!ini_get('open_basedir') && is_readable('/dev/urandom')) {
$fp = fopen('/dev/urandom', 'rb');
if (!empty($fp)) {
$st = fstat($fp);
// In C, this would be: (stat_mode & S_IFMT) !== S_IFCHR
if (($st['mode'] & 0170000) !== 020000) {
fclose($fp);
$fp = false;
}
unset($st);
}
}
if (isset($fp) && $fp !== false) {
/**
* stream_set_read_buffer() / stream_set_chunk_size does not exist in HHVM
*
* If we don't set the stream's read buffer to 0, PHP will
* internally buffer 8192 bytes, which can waste entropy
*
* stream_set_read_buffer returns 0 on success
*/
if (function_exists('stream_set_chunk_size')) {
stream_set_chunk_size($fp, $length);
}
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($fp, $length);
}
$remaining = $length;
$buf = '';
do {
$read = fread($fp, $remaining);
// We cannot safely read from the file, so exit the do-while loop.
if ($read === false) {
$buf = false;
break;
}
// Decrease the number of bytes returned from remaining.
$remaining -= UTF8::strlen($read, '8bit');
$buf .= $read;
} while ($remaining > 0);
fclose($fp);
if ($buf !== false) {
if (UTF8::strlen($buf, '8bit') === $length) {
return $buf;
}
}
}
/*
* 3. Windows
*
//.........这里部分代码省略.........