views:

131

answers:

5

I have the below methods:

    public static byte[] ConvertFileToBytes(string filePath)
    {
        var fInfo = new FileInfo(filePath);
        var numBytes = fInfo.Length;
        var dLen = Convert.ToDouble(fInfo.Length / 1000000);

        var fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var br = new BinaryReader(fStream);

        var data = br.ReadBytes((int)numBytes);
        br.Close();

        fStream.Close();
        fStream.Dispose();

        return data;
    }

    public static void ConvertBytesToFile(byte[] file, string filePath)
    {
        var ms = new MemoryStream(file);

        var fs = new FileStream(filePath, FileMode.Create);

        ms.WriteTo(fs);

        ms.Close();
        fs.Close();
        fs.Dispose();
    }

What is the correct to name these methods? (because Convert*XXX*to*YYY* just doesn't cut it in a Utilities library)

+9  A: 

How about File.ReadAllBytes and File.WriteAllBytes ;)

HTH, Kent

Kent Boogaart
...haha! i just continue learning a ton of new things everyday. Cheers mate!
Andreas Grech
Not trying to be a jerk but I have seen this a couple of times. This answer doesn't answer the actual question posed. It is a good suggestion though. The question is about his method signatures not implementation. Would thnk that SO would want to keep these somewhat consistent.
Chad Grant
@Deviant: I think it does in that I'm inferring that the correct names should be "Read" and "Write". Since we're dealing specifically with files, "(de)serialize" is too generic. Same for "marshal", which I think also has slightly different connotations.
Kent Boogaart
+2  A: 

The terms usually used are "serialize" and "deserialize" (or sometimes "marshal" and "demarshal").

clee
To me, both "serialize" and "marshal" imply that you're dealing with some sort of data structure that needs special logic for converting it to bytes - but we already have bytes, they just need to be written to a file (which is nothing but a bunch of bytes as well).
Michael Borgwardt
A: 

Marshalling/Unmarshalling might be the appropriate term.

http://en.wikipedia.org/wiki/Marshalling_(computer_science)

Andy White
hmm, so what's the difference between marshalling and serializing?
Andreas Grech
Marshalling is traditionally used when dealing with XML, serializing when dealing with in-memory data structures. Neither really applies to the simple task of writing bytes directly to a file.
Michael Borgwardt
A: 

In C++ they would be called read and write.

anon
A: 

The WriteAllBytes and ReadAllBytes are a good suggestion, but to answer your Question ...

Save() would be a good choice for renaming of ConvertToFile() and Object.CreateFromFile() for the reverse.

Chad Grant