0

With help of this question C# 4.0: Convert pdf to byte[] and vice versa i was able to convert byte[] to PDF. Byte array length is 25990 approx. When i try to open the PDF it says file is corrupted. What could be the reason?

I tried the BinaryWriter but it creates PDF of 0 KB.

It's a response from a Web Service

Sample Code

WebResponse resp = request.GetResponse();
var buffer = new byte[4096];
Stream responseStream = resp.GetResponseStream();
{
  int count;
  do
  {
    count = responseStream.Read(buffer, 0, buffer.Length);
    memoryStream.Write(buffer, 0, responseStream.Read(buffer, 0, buffer.Length));
  } while (count != 0);
}
resp.Close();
byte[] memoryBuffer = memoryStream.ToArray();
System.IO.File.WriteAllBytes(@"E:\sample1.pdf", memoryBuffer);
int s = memoryBuffer.Length;

BinaryWriter binaryWriter = new BinaryWriter(File.Open(@"E:\sample2.pdf", FileMode.Create));
binaryWriter.Write(memoryBuffer);
Community
  • 1
  • 1
Gopi
  • 5,656
  • 22
  • 80
  • 146
  • 1
    Where does this `byte[]` come from? What exactly does it represent? What path has it followed before it arrives as a variable in your code? Didn't you massage it forth and back to characters in the meanwhile? – BalusC Mar 16 '10 at 11:56
  • 3
    Can you post your code? The question does not have enough information for an answer. By the way = 25990 bytes **are** about 25KB. – Oded Mar 16 '10 at 11:57
  • We're not clairvoyants, post the code please otherwise we will end up shooting ourselves in the foot and risk downvotes for incorrect answers that does not satisfy you! – t0mm13b Mar 16 '10 at 12:01
  • that line is useless - int s = memoryBuffer.Length; Where's memorystream? You're not using the *using* clause... – t0mm13b Mar 16 '10 at 12:03
  • Sorry folks. i have updated the question @Oded Since it says file is corrupted i thought still some byte might not written properly. – Gopi Mar 16 '10 at 12:05
  • possible duplicate of [C# 4.0: Convert pdf to byte\[\] and vice versa](http://stackoverflow.com/questions/2451864/c-sharp-4-0-convert-pdf-to-byte-and-vice-versa) – jjxtra May 15 '14 at 15:24
  • It looks like your do/while loop is bugged in that your Write() call always starts at index 0, rather than at `count`, so you're continuously overwriting the first part of the buffer. This could work properly if the first read happens to get the entire file, though. – Conspicuous Compiler May 15 '14 at 16:06
  • @PsychoDad Considering the cause of the problem might be the bug I've pointed out, I don't think this is an exact duplicate. Maybe let OP figure out the problem with our aid first, then we can close it if it turns out to be a dupe. – Conspicuous Compiler May 15 '14 at 16:07

2 Answers2

9

You are reading twice from the stream but only writing one buffer. Change this:

count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, responseStream.Read(buffer, 0, buffer.Length));

To this:

count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
2

It seems your missing some bytes there because you have one unnecessary read. Try this:

  do
  {
    count = responseStream.Read(buffer, 0, buffer.Length);
    memoryStream.Write(buffer, 0, count);
  } while (count != 0);
bruno conde
  • 47,767
  • 15
  • 98
  • 117