0

I have a byte document in a variable, and I want to put it into a FileStream for using it into a StreamReader.

Can I do this?

I saw that FileStream uses a path, but is there a way to read my byte document?

I got something like this, of course, the myByteDocument doesn't work, because it's not a path:

file = new FileStream(myByteDocument, FileMode.Open, FileAccess.Read, FileShare.Read);

reader= new StreamReader(file); 
reader.BaseStream.Seek(0, SeekOrigin.Begin);
string fullText= "";
while (reader.Peek() > -1) 
{
    fullText+= reader.ReadLine();
}
reader.Close();

myByteDocument is obtain like this :

DataRow row = vDs.Tables[0].Rows
byte[] myByteDocument = (byte[])row.ItemArray[0];

I read the document, and put it into a string to replace, some pieces of it, and then after all the replace, I create a new document with the fullText variable, with something like sw.write(fullText), where sw is a StreamWriter.

So, I want to read the file, without knowing the path, but with the byte document directly. Can I do this?

If I'm not clear, don't hesitate, say it.

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
provençal le breton
  • 1,428
  • 4
  • 26
  • 43
  • 1
    What is a byte document exactly? Is it an array of bytes or something similar? Can you show how you create this object `myByteDocument`? – John Willemse Apr 16 '13 at 13:04
  • Yeah, that's not clear... What's your input? byte array? stream? Where does it come from? – ken2k Apr 16 '13 at 13:05
  • Based on your edit with your code for obtaining `myByteDocument` it is still not clear what it is. `DataRow.ItemArray[]` just returns an object. So it could still be anything. I'm guessing the file is stored in a database? If so, how is it stored? – psubsee2003 Apr 16 '13 at 13:41
  • 1
    You should consider using a `StringBuilder` when building the string. – Jim Mischel Apr 16 '13 at 13:44
  • Psubee : forgot to add the cast in here so, (byte[])row.itemArray[0]. And yes, it is stored into a database, as a BLOB. – provençal le breton Apr 16 '13 at 14:02
  • @Jean so it is a `byte[]`... excellent. I added the appropriate info to your question. Please make sure my edit is accurate. – psubsee2003 Apr 16 '13 at 14:09
  • Yes it is the same cast as mine. I forgot to edit with it. – provençal le breton Apr 16 '13 at 14:37

2 Answers2

3

You should look at the MemoryStream class instead of FileStream. It should provide the functionality you need to read the file without knowing the path (provided myByteDocument is a byte array).

var file = new MemoryStream(myByteDocument);

You can basically use your same code just substituting the MemoryStream constructor for the FileStream constructor you were trying to use.

string fullText= "";

using(var file = new MemoryStream(myByteDocument))
using(var reader = new StreamReader(file))
{
    reader.BaseStream.Seek(0, SeekOrigin.Begin);
    while (!reader.EndOfStream)
    {
        fullText += reader.ReadLine();
    }
}

Please note, I also added using blocks for the file access. This is much preferable to just calling reader.Close() as it will ensure the cleanup of the resources even if an Exception occurs where just calling the Close() method will not.

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
  • And if `myByteDocument` is a string, he can use `StringReader`. – Jim Mischel Apr 16 '13 at 13:34
  • @JimMischel true, but if it is a `String`, then it kind of defeats the purpose of all of this as he is just trying to read the file to a string. – psubsee2003 Apr 16 '13 at 13:36
  • Your answer was what I was going to say, which is why I upvoted you. But OP says that he creates a string, does some replacement, etc. It was unclear to me what `myByteDocument` really is, until his latest edit. – Jim Mischel Apr 16 '13 at 13:42
  • @JimMischel Absolutely. All we know is that `row.ItemArray[0]` returns an object, so it could be anything. I was just pointing out that if it were a string, using StringReader just to convert it to a String would be a bit unconventional. – psubsee2003 Apr 16 '13 at 13:44
  • Yes , sorry for forgotten details on 'myByteDocument' type. I tought first, it was say in the question. M y bad. – provençal le breton Apr 16 '13 at 14:46
  • Can I use word automation with the same byte document? – provençal le breton Apr 24 '13 at 15:01
1

What you need is convert your byte array to string, then replace what you need, and write the final result. You don't even need StreamReaders or writers to do this.

Take a look at this post: How to convert byte[] to string?

Here you have different methods to convert your document to a string. The one accepted as an answer is:

string result = System.Text.Encoding.UTF8.GetString(myByteDocument)

Once you've done all your replacements, just save the string into a file, with something like that (most simple way):

File.WriteAllText(path, result);
Community
  • 1
  • 1
ppetrov
  • 3,077
  • 2
  • 15
  • 27