0

I'm trying to convert a .db file to binary so I can stream it across a web server. I'm pretty new to C#. I've gotten as far as looking at code snippets online but I'm not really sure if the code below puts me on the right track. How I can write the data once I read it? Does BinaryReader automatically open up and read the entire file so I can then just write it out in binary format?

class Program
{
    static void Main(string[] args)
    {
        using (FileStream fs = new FileStream("output.bin", FileMode.Create))
        {
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                long totalBytes = new System.IO.FileInfo("input.db").Length;
                byte[] buffer = null;

                BinaryReader binReader = new BinaryReader(File.Open("input.db", FileMode.Open)); 
            }
        }
    }
}

Edit: Code to stream the database:

[WebGet(UriTemplate = "GetDatabase/{databaseName}")]
public Stream GetDatabase(string databaseName)
{
    string fileName = "\\\\computer\\" + databaseName + ".db";

    if (File.Exists(fileName))
    {
        FileStream stream = File.OpenRead(fileName);

        if (WebOperationContext.Current != null)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "binary/.bin";
        }

        return stream;
    }

    return null;
}

When I call my server, I get nothing back. When I use this same type of method for a content-type of image/.png, it works fine.

John H
  • 14,422
  • 4
  • 41
  • 74
J W
  • 868
  • 1
  • 12
  • 25
  • 1
    What do you mean by convert to binary? What is format of `input.db`? – Andrey Aug 07 '12 at 16:53
  • @Audrey .db is a format I can open in Sqlite Manager. Are there other formats used for databases for sqlite? I'm pretty new to this area. Thanks! – J W Aug 07 '12 at 16:59
  • could you please explain what is your ultimate goal? SQLite databases are binary itself. – Andrey Aug 07 '12 at 17:00
  • @Audrey My ultimate goal is to just stream the sqlite database itself from the server to a device. I added code what I have for that above. – J W Aug 07 '12 at 17:02

1 Answers1

2

All the code you posted will actually do is copy the file input.db to the file output.bin. You could accomplish the same using File.Copy.

BinaryReader will just read in all of the bytes of the file. It is a suitable start to streaming the bytes to an output stream that expects binary data.

Once you have the bytes corresponding to your file, you can write them to the web server's response like this:

using (BinaryReader binReader = new BinaryReader(File.Open("input.db", 
                                                 FileMode.Open))) 
{
    byte[] bytes = binReader.ReadBytes(int.MaxValue); // See note below
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.Close();
    Response.End();
}

Note: The code binReader.ReadBytes(int.MaxValue) is for demonstrating the concept only. Don't use it in production code as loading a large file can quickly lead to an OutOfMemoryException. Instead, you should read in the file in chunks, writing to the response stream in chunks.

See this answer for guidance on how to do that

https://stackoverflow.com/a/8613300/141172

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553