Turning a Word Document into a Blob

G

Guest

I'm working in Visual Studio 2005 and C#.

I'm using the following code to turn a Word 2002 document into a blob. This
blob will be passed as a parameter to a stored procedure in an Oracle
database. The error I'm getting makes me think I'm not converting the entire
document. Anybody see anything obviously wrong with the code below?

Thanks,
Randy


byte[] blobDoc;
using (FileStream fs = new FileStream(_filenameSave, FileMode.Open,
FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
byte[] byteArray = new byte[fs.Length];

int length = Convert.ToInt32(fs.Length);
br.Read(byteArray, 0, length);
blobDoc = byteArray;
}
 
A

Arnshea

I'm working in Visual Studio 2005 and C#.

I'm using the following code to turn a Word 2002 document into a blob. This
blob will be passed as a parameter to a stored procedure in an Oracle
database. The error I'm getting makes me think I'm not converting the entire
document. Anybody see anything obviously wrong with the code below?

Thanks,
Randy

byte[] blobDoc;
using (FileStream fs = new FileStream(_filenameSave, FileMode.Open,
FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
byte[] byteArray = new byte[fs.Length];

int length = Convert.ToInt32(fs.Length);
br.Read(byteArray, 0, length);
blobDoc = byteArray;



}- Hide quoted text -

- Show quoted text -

You don't want to use a BinaryReader - it's for reading primitives
like int or double from a file created from a BinaryWriter.

Read directly from the FileStream into the byte[]. Something like:

FileStream fs = new FileStream(_filenameSave, FileMode.Open);
byte[] ary = new byte[fs.Length];
fs.Read(ary, 0, ary.Length);
 
N

Nicholas Paldino [.NET/C# MVP]

Randy,

If all you are doing is reading the bytes from the stream, then there is
no need for the binary reader. Also, I don't see why you don't use the
ReadAllBytes method on the File class, like so:

byte[] blobDoc = File.ReadAllBytes(_filenameSave);

In the case where you need to use a stream, I would look at Jon Skeet's
explanation as to why you shouldn't read from a stream the way you are:

http://www.yoda.arachsys.com/csharp/readbinary.html

Basically, the Read method (on the stream and on the BinaryReader) is
not guaranteed to return all the data you ask for.
 
G

Guest

Thanks for the responses. The reason I'm using BinaryReader is to control the
encoding. I need to ensure the encoding is UTF8. Is there a way to ensure
that with these shorter methods?

Thanks,
Randy

Nicholas Paldino said:
Randy,

If all you are doing is reading the bytes from the stream, then there is
no need for the binary reader. Also, I don't see why you don't use the
ReadAllBytes method on the File class, like so:

byte[] blobDoc = File.ReadAllBytes(_filenameSave);

In the case where you need to use a stream, I would look at Jon Skeet's
explanation as to why you shouldn't read from a stream the way you are:

http://www.yoda.arachsys.com/csharp/readbinary.html

Basically, the Read method (on the stream and on the BinaryReader) is
not guaranteed to return all the data you ask for.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

randy1200 said:
I'm working in Visual Studio 2005 and C#.

I'm using the following code to turn a Word 2002 document into a blob.
This
blob will be passed as a parameter to a stored procedure in an Oracle
database. The error I'm getting makes me think I'm not converting the
entire
document. Anybody see anything obviously wrong with the code below?

Thanks,
Randy


byte[] blobDoc;
using (FileStream fs = new FileStream(_filenameSave, FileMode.Open,
FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
byte[] byteArray = new byte[fs.Length];

int length = Convert.ToInt32(fs.Length);
br.Read(byteArray, 0, length);
blobDoc = byteArray;
}
 
N

Nicholas Paldino [.NET/C# MVP]

Randy,

No, you don't need to ensure the encoding is UTF-8. The Word document
is a binary file, encodings mean nothing. While the Word application will
show text, the format of the file itself is not text.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

randy1200 said:
Thanks for the responses. The reason I'm using BinaryReader is to control
the
encoding. I need to ensure the encoding is UTF8. Is there a way to ensure
that with these shorter methods?

Thanks,
Randy

Nicholas Paldino said:
Randy,

If all you are doing is reading the bytes from the stream, then there
is
no need for the binary reader. Also, I don't see why you don't use the
ReadAllBytes method on the File class, like so:

byte[] blobDoc = File.ReadAllBytes(_filenameSave);

In the case where you need to use a stream, I would look at Jon
Skeet's
explanation as to why you shouldn't read from a stream the way you are:

http://www.yoda.arachsys.com/csharp/readbinary.html

Basically, the Read method (on the stream and on the BinaryReader) is
not guaranteed to return all the data you ask for.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

randy1200 said:
I'm working in Visual Studio 2005 and C#.

I'm using the following code to turn a Word 2002 document into a blob.
This
blob will be passed as a parameter to a stored procedure in an Oracle
database. The error I'm getting makes me think I'm not converting the
entire
document. Anybody see anything obviously wrong with the code below?

Thanks,
Randy


byte[] blobDoc;
using (FileStream fs = new FileStream(_filenameSave, FileMode.Open,
FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
byte[] byteArray = new byte[fs.Length];

int length = Convert.ToInt32(fs.Length);
br.Read(byteArray, 0, length);
blobDoc = byteArray;
}
 
A

Arnshea

Randy,

If all you are doing is reading the bytes from the stream, then there is
no need for the binary reader. Also, I don't see why you don't use the
ReadAllBytes method on the File class, like so:

byte[] blobDoc = File.ReadAllBytes(_filenameSave);

In the case where you need to use a stream, I would look at Jon Skeet's
explanation as to why you shouldn't read from a stream the way you are:

http://www.yoda.arachsys.com/csharp/readbinary.html

Basically, the Read method (on the stream and on the BinaryReader) is
not guaranteed to return all the data you ask for.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I'm working in Visual Studio 2005 and C#.
I'm using the following code to turn a Word 2002 document into a blob.
This
blob will be passed as a parameter to a stored procedure in an Oracle
database. The error I'm getting makes me think I'm not converting the
entire
document. Anybody see anything obviously wrong with the code below?
Thanks,
Randy

byte[] blobDoc;
using (FileStream fs = new FileStream(_filenameSave, FileMode.Open,
FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
byte[] byteArray = new byte[fs.Length];
int length = Convert.ToInt32(fs.Length);
br.Read(byteArray, 0, length);
blobDoc = byteArray;
}- Hide quoted text -

- Show quoted text -

Good point. If, like me, you're currently using the .net1.1 framework
then the following should do the trick:

FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read,
FileShare.Read);
MemoryStream memBuf = new MemoryStream();

int bytesRead = -1;
byte[] buf = new byte[4096];

while ( (bytesRead = fs.Read(buf, 0, buf.Length)) > 0 )
{
memBuf.Write(buf, 0, bytesRead);
}

fs.Close();
memBuf.Close();

byte[] allBytes = memBuf.GetBuffer();
 
V

Vadym Stetsiak

Hello, randy1200!

There are several issues here:
1. You're allocating memory for an entire file at once
2. You do not check the amount of bytes read from the stream.

int read;
int offset;
while ( (read = br.Read(byteArray, offset, length-offset)) > 0)
{
offset += read;
}

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com


You wrote on Wed, 12 Sep 2007 09:34:02 -0700:

r> I'm working in Visual Studio 2005 and C#.

r> I'm using the following code to turn a Word 2002 document into a
r> blob. This blob will be passed as a parameter to a stored procedure
r> in an Oracle database. The error I'm getting makes me think I'm not
r> converting the entire document. Anybody see anything obviously wrong
r> with the code below?

r> Thanks,
r> Randy


r> byte[] blobDoc;
r> using (FileStream fs = new FileStream(_filenameSave, FileMode.Open,
r> FileAccess.Read))
r> {
r> BinaryReader br = new BinaryReader(fs,
r> System.Text.Encoding.UTF8);
r> byte[] byteArray = new byte[fs.Length];

r> int length = Convert.ToInt32(fs.Length);
r> br.Read(byteArray, 0, length);
r> blobDoc = byteArray;
r> }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top