#region License and Copyright
/* -------------------------------------------------------------------------
* Dotnet Commons IO
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
*
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
* -------------------------------------------------------------------------
*/
#endregion
using System;
using System.Collections;
using System.Globalization;
using System.IO;
namespace Dotnet.Commons.IO
{
///
///
/// This class provides basic facilities for manipulating files and file paths.
///
/// File-related methods
/// There are methods to
///
/// - copy a file to another file,
/// - compare the content of 2 files,
/// - delete files using the wildcard character,
/// - etc
///
///
///
public sealed class FileUtils
{
///
/// Reads data from the beginning of a stream until the end is reached. The
/// data is returned as a byte array.
///
/// The stream to read data from
/// thrown if any of the underlying IO calls fail
/// Use this method if you don't know the length of the stream in advance
/// (for instance a network stream) and just want to read the whole lot into a buffer.
///
/// Note:
/// This method of reading the stream is not terribly efficient.
///
///
public static byte[] GetBytes(Stream stream)
{
if (stream is MemoryStream)
return ((MemoryStream)stream).ToArray();
byte[] byteArray = new byte[1024];
using (MemoryStream ms = new MemoryStream())
{
stream.Position = 0;
while (true)
{
int readLen = stream.Read(byteArray, 0, byteArray.Length);
if (readLen <= 0)
return ms.ToArray();
ms.Write(byteArray, 0, readLen);
}
}
}
///
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array.
///
/// The stream to read data from
/// The initial buffer length. If the length is < 1,
/// then the default value of will be used.
///
/// thrown if any of the underlying IO calls fail
/// Use this method to get the data if you know the expected length of data to start with.
public static byte[] GetBytes(Stream stream, long initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < 1)
initialLength = Int16.MaxValue;
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
/// ---------------------------------------------------------------
///
/// Read binary context of a file.
///
/// Full path (directory + filename) of file to read
/// content in byte array
/// ---------------------------------------------------------------
public static byte[] ReadBinaryFile(string path)
{
FileInfo fi = new FileInfo(path);
byte[] data = null;
if (!fi.Exists)
throw new IOException(path + " does not exists");
// Read File
using (Stream stream = File.OpenRead(path))
{
// Get Data from The Stream
data = GetBytes(stream, fi.Length);
// Close Stream
stream.Close();
}
fi.Refresh();
// Return Data
return (data);
}
}
}