FileUtilities, finální implementace

Tomáš Holan       03.02.2011       I/O operace       10583 zobrazení

Zde je pro úplnost uvedená kompletní implementace třídy FileUtilities, která byla vytvořená v tomto příspěvku: část 1, část 2

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
    
internal static class FileUtilities
{
    #region member types definition
    private sealed class WebResponseReader : TextReader, IDisposable
    {
        #region member varible and default property initialization
        private WebResponse response;
        private StreamReader reader;
        #endregion

        #region constructors and destructors
        public WebResponseReader(WebRequest request, Encoding encoding)
        {
            this.response = request.GetResponse();
            try
            {
                this.reader = new StreamReader(response.GetResponseStream(), encoding);
            }
            catch
            {
                response.Close();
                throw;
            }
        }
        #endregion

        #region action methods
        public override string ReadLine()
        {
            return this.reader.ReadLine();
        }
        #endregion

        #region private member functions
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                reader.Close();
                response.Close();
            }
        }
        #endregion
    }
    #endregion

    #region action methods
#if FRAMEWORK35
    public static IEnumerable<string> ReadLines(string path, Encoding encoding)
    {
        if (path == null)
        {
            throw new ArgumentNullException("path");
        }
        if (encoding == null)
        {
            throw new ArgumentNullException("encoding");
        }
        if (path.Length == 0)
        {
            throw new ArgumentException("path is empty string.", "path");
        }

        return ReadLinesInternal(new StreamReader(path, encoding));
    }

    public static IEnumerable<string> ReadLines(string path)
    {
        if (path == null)
        {
            throw new ArgumentNullException("path");
        }
        if (path.Length == 0)
        {
            throw new ArgumentException("path is empty string.", "path");
        }

        return ReadLinesInternal(new StreamReader(path, Encoding.UTF8));
    }
#endif

    public static IEnumerable<string> ReadLines(Uri url, Encoding encoding)
    {
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }
        if (encoding == null)
        {
            throw new ArgumentNullException("encoding");
        }

        return ReadLinesInternal(new WebResponseReader(WebRequest.Create(url), encoding));
    }

    public static IEnumerable<string> ReadLines(Uri url)
    {
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }

        return ReadLinesInternal(new WebResponseReader(WebRequest.Create(url), Encoding.UTF8));
    }

    public static IEnumerable<string> ReadHeaderAndLines(string path, Encoding encoding, out string header)
    {
        if (path == null)
        {
            throw new ArgumentNullException("path");
        }
        if (encoding == null)
        {
            throw new ArgumentNullException("encoding");
        }
        if (path.Length == 0)
        {
            throw new ArgumentException("path is empty string.", "path");
        }

        var reader = new StreamReader(path, encoding);
        try
        {
            header = reader.ReadLine();
            if (header == null)
            {
                throw new EndOfStreamException("Header row not found.");
            }
        }
        catch
        {
            reader.Dispose();
            throw;
        }

        return ReadLinesInternal(reader);
    }

    public static IEnumerable<string> ReadHeaderAndLines(string path, out string header)
    {
        return ReadHeaderAndLines(path, Encoding.UTF8, out header);
    }

    public static IEnumerable<string> ReadHeaderAndLines(Uri url, Encoding encoding, out string header)
    {
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }
        if (encoding == null)
        {
            throw new ArgumentNullException("encoding");
        }

        var reader = new WebResponseReader(WebRequest.Create(url), encoding);
        try
        {
            header = reader.ReadLine();
            if (header == null)
            {
                throw new EndOfStreamException("Header row not found");
            }
        }
        catch
        {
            reader.Dispose();
            throw;
        }

        return ReadLinesInternal(reader);
    }

    public static IEnumerable<string> ReadHeaderAndLines(Uri url, out string header)
    {
        return ReadHeaderAndLines(url, Encoding.UTF8, out header);
    }
    #endregion

    #region private member functions
    private static IEnumerable<string> ReadLinesInternal(TextReader reader)
    {
        using (reader)
        {
            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }

                yield return line;
            }
        }
    }
    #endregion
}

 

hodnocení článku

1 bodů / 1 hlasů       Hodnotit mohou jen registrované uživatelé.

 

Nový příspěvek

 

                       
Nadpis:
Antispam: Komu se občas házejí perly?
Příspěvek bude publikován pod identitou   anonym.

Nyní zakládáte pod článkem nové diskusní vlákno.
Pokud chcete reagovat na jiný příspěvek, klikněte na tlačítko "Odpovědět" u některého diskusního příspěvku.

Nyní odpovídáte na příspěvek pod článkem. Nebo chcete raději založit nové vlákno?

 

  • Administrátoři si vyhrazují právo komentáře upravovat či mazat bez udání důvodu.
    Mazány budou zejména komentáře obsahující vulgarity nebo porušující pravidla publikování.
  • Pokud nejste zaregistrováni, Vaše IP adresa bude zveřejněna. Pokud s tímto nesouhlasíte, příspěvek neodesílejte.

přihlásit pomocí externího účtu

přihlásit pomocí jména a hesla

Uživatel:
Heslo:

zapomenuté heslo

 

založit nový uživatelský účet

zaregistrujte se

 
zavřít

Nahlásit spam

Opravdu chcete tento příspěvek nahlásit pro porušování pravidel fóra?

Nahlásit Zrušit

Chyba

zavřít

feedback