From 85774de2310141f4311bc3df1946d44df9ddde59 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Fri, 6 Mar 2009 20:12:08 +0000 Subject: * Improve memory usage when writing OARs * This should make saving large OARs a somewhat better experience * However, the problem where saving an archive pulls large numbers of assets into the asset cache isn't yet resolved * This patch also removes lots of archive writing spam that crept in --- .../CoreModules/World/Archiver/TarArchiveWriter.cs | 98 ++++++++++------------ 1 file changed, 44 insertions(+), 54 deletions(-) (limited to 'OpenSim/Region/CoreModules/World/Archiver/TarArchiveWriter.cs') diff --git a/OpenSim/Region/CoreModules/World/Archiver/TarArchiveWriter.cs b/OpenSim/Region/CoreModules/World/Archiver/TarArchiveWriter.cs index 09ea7ec..506fcc5 100644 --- a/OpenSim/Region/CoreModules/World/Archiver/TarArchiveWriter.cs +++ b/OpenSim/Region/CoreModules/World/Archiver/TarArchiveWriter.cs @@ -39,65 +39,80 @@ namespace OpenSim.Region.CoreModules.World.Archiver { //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - protected Dictionary m_files = new Dictionary(); - protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding(); /// - /// Add a directory to the tar archive. We can only handle one path level right now! + /// Binary writer for the underlying stream + /// + protected BinaryWriter m_bw; + + public TarArchiveWriter(Stream s) + { + m_bw = new BinaryWriter(s); + } + + /// + /// Write a directory entry to the tar archive. We can only handle one path level right now! /// /// - public void AddDir(string dirName) + public void WriteDir(string dirName) { // Directories are signalled by a final / if (!dirName.EndsWith("/")) dirName += "/"; - AddFile(dirName, new byte[0]); + WriteFile(dirName, new byte[0]); } - + /// - /// Add a file to the tar archive + /// Write a file to the tar archive /// /// /// - public void AddFile(string filePath, string data) + public void WriteFile(string filePath, string data) { - AddFile(filePath, m_asciiEncoding.GetBytes(data)); + WriteFile(filePath, m_asciiEncoding.GetBytes(data)); } /// - /// Add a file to the tar archive + /// Write a file to the tar archive /// /// /// - public void AddFile(string filePath, byte[] data) + public void WriteFile(string filePath, byte[] data) { - m_files[filePath] = data; + if (filePath.Length > 100) + WriteEntry("././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L'); + + char fileType; + + if (filePath.EndsWith("/")) + { + fileType = '5'; + } + else + { + fileType = '0'; + } + + WriteEntry(filePath, data, fileType); } /// - /// Write the raw tar archive data to a stream. The stream will be closed on completion. + /// Finish writing the raw tar archive data to a stream. The stream will be closed on completion. /// /// Stream to which to write the data /// - public void WriteTar(Stream s) + public void Close() { - BinaryWriter bw = new BinaryWriter(s); - - foreach (string filePath in m_files.Keys) - { - WriteFile(bw, filePath, m_files[filePath]); - } - //m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks"); // Write two consecutive 0 blocks to end the archive byte[] finalZeroPadding = new byte[1024]; - bw.Write(finalZeroPadding); + m_bw.Write(finalZeroPadding); - bw.Flush(); - bw.Close(); + m_bw.Flush(); + m_bw.Close(); } public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding) @@ -119,39 +134,14 @@ namespace OpenSim.Region.CoreModules.World.Archiver return oBytes; } - - /// - /// Write a particular file of data - /// - /// - /// - protected void WriteFile(BinaryWriter bw, string filePath, byte[] data) - { - if (filePath.Length > 100) - WriteEntry(bw, "././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L'); - - char fileType; - - if (filePath.EndsWith("/")) - { - fileType = '5'; - } - else - { - fileType = '0'; - } - - WriteEntry(bw, filePath, data, fileType); - } /// - /// Write a particular file of data + /// Write a particular entry /// - /// /// /// /// - protected void WriteEntry(BinaryWriter bw, string filePath, byte[] data, char fileType) + protected void WriteEntry(string filePath, byte[] data, char fileType) { byte[] header = new byte[512]; @@ -208,10 +198,10 @@ namespace OpenSim.Region.CoreModules.World.Archiver header[154] = 0; // Write out header - bw.Write(header); + m_bw.Write(header); // Write out data - bw.Write(data); + m_bw.Write(data); if (data.Length % 512 != 0) { @@ -220,7 +210,7 @@ namespace OpenSim.Region.CoreModules.World.Archiver //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired); byte[] padding = new byte[paddingRequired]; - bw.Write(padding); + m_bw.Write(padding); } } } -- cgit v1.1