Пример #1
0
size_t IStream::ReadToStream(size_t size, IStream& dest, size_t bufferSize/*=1024*/)const
{
	RETURN_ZERO_IF_FALSE(CanRead() && dest.CanWrite());

	if (dest.IsPtrAvailable())//could directly write
	{
		dest.ReserveLeftSize(size);
		byte* buffer = dest.MutablePtr();
		MemoryData destBuffer = MemoryData::FromStatic(buffer, size);
		return ReadDataTo(destBuffer, DataReadingMode::AlwaysCopy);
	}
	else
	{
		//should use temp  buffer
		size_t count = 0;
		size_t realBufferSize = Math::Min(LeftLength(), bufferSize, size);

		MemoryData tempBuffer = MemoryData::Alloc(realBufferSize);
		do
		{
			size_t readSize = Math::Min(size, realBufferSize);
			tempBuffer.ForceSetSize(readSize);

			readSize = ReadDataTo(tempBuffer, DataReadingMode::AlwaysCopy);
			BREAK_IF_ZERO(readSize);
			tempBuffer.ForceSetSize(readSize);
			count += dest.WriteData(tempBuffer);
			tempBuffer.ForceSetSize(realBufferSize);
			size -= readSize;
		} while (size > 0);
		return count;
	}

}
Пример #2
0
size_t IStream::CopyTo(IStream& dest, size_t bufferSize/*=1024*/)const
{
	RETURN_ZERO_IF_FALSE(CanRead() && dest.CanWrite());

	size_t realBufferSize = Math::Min(LeftLength(), bufferSize);
	size_t count = 0;

	MemoryData buffer = MemoryData::Alloc(realBufferSize);
	do
	{
		size_t readSize = ReadDataTo(buffer, DataReadingMode::AlwaysCopy);
		BREAK_IF_ZERO(readSize);
		buffer.ForceSetSize(readSize);
		count += dest.WriteData(buffer);
		buffer.ForceSetSize(realBufferSize);
	} while (true);



	return count;
}
Пример #3
0
MemoryData IStream::ReadData(size_t size, DataReadingMode mode/*=DataReadingMode::AlwaysCopy*/)const
{
	if (LeftLength() >= size)
	{
		if (Ptr() == nullptr)	//not support direct move
		{
			mode = DataReadingMode::AlwaysCopy;
		}

		switch (mode)
		{
		case DataReadingMode::AlwaysCopy:
		{
			MemoryData data = MemoryData::Alloc(size);
			size_t readSize = ReadDataTo(data, mode);
			data.ForceSetSize(readSize);

			return data;
		}
		break;
		case DataReadingMode::DirectMove:
		{
			MemoryData data;
			data.ForceSetSize(size);
			size_t readSize = ReadDataTo(data, mode);
			data.ForceSetSize(readSize);

			return data;
		}
		break;
		default:
			break;
		}

	}
	return MemoryData::Empty;
}
Пример #4
0
bool File::Copy(StringRef srcFilePath, StringRef destFilePath)
{
	FileStream srcFile;
	FileStream destFile;
	byte buffer[1024];
	MemoryData data = MemoryData::FromStatic(buffer);
	if (srcFile.OpenReadBinary(srcFilePath) && destFile.OpenNewWriteBinary(destFilePath))
	{
		while (true)
		{
			size_t readCount = srcFile.ReadDataTo(data);
			if (readCount == 0)
			{
				return true;
			}
			data.ForceSetSize(readCount);
			destFile.WriteData(data);
		}

	}

	return false;
}