void FileAppender::setFileInternal()

in src/main/cpp/fileappender.cpp [265:356]


void FileAppender::setFileInternal(
	const LogString& filename,
	bool append1,
	bool bufferedIO1,
	size_t bufferSize1,
	Pool& p)
{
	// It does not make sense to have immediate flush and bufferedIO.
	if (bufferedIO1)
	{
		setImmediateFlush(false);
	}

	closeWriter();

	bool writeBOM = false;

	if (StringHelper::equalsIgnoreCase(getEncoding(),
			LOG4CXX_STR("utf-16"), LOG4CXX_STR("UTF-16")))
	{
		//
		//    don't want to write a byte order mark if the file exists
		//
		if (append1)
		{
			File outFile;
			outFile.setPath(filename);
			writeBOM = !outFile.exists(p);
		}
		else
		{
			writeBOM = true;
		}
	}

	OutputStreamPtr outStream;

	try
	{
		outStream = FileOutputStreamPtr(new FileOutputStream(filename, append1));
	}
	catch (IOException&)
	{
		LogString parentName = File().setPath(filename).getParent(p);

		if (!parentName.empty())
		{
			File parentDir;
			parentDir.setPath(parentName);

			if (!parentDir.exists(p) && parentDir.mkdirs(p))
			{
				outStream = OutputStreamPtr(new FileOutputStream(filename, append1));
			}
			else
			{
				throw;
			}
		}
		else
		{
			throw;
		}
	}


	//
	//   if a new file and UTF-16, then write a BOM
	//
	if (writeBOM)
	{
		char bom[] = { (char) 0xFE, (char) 0xFF };
		ByteBuffer buf(bom, 2);
		outStream->write(buf, p);
	}

	WriterPtr newWriter(createWriter(outStream));

	if (bufferedIO1)
	{
		newWriter = std::make_shared<BufferedWriter>(newWriter, bufferSize1);
	}

	setWriterInternal(newWriter);

	_priv->fileAppend = append1;
	_priv->bufferedIO = bufferedIO1;
	_priv->fileName = filename;
	_priv->bufferSize = (int)bufferSize1;
	writeHeader(p);

}