Writing a stream to a file does not work

I am making a program that transfers a file to pieces over TCP. Now, every time I move the file a little to the end, it does not seem to be written, and therefore, if I try to transfer the image in the lower right corner, there are crashes . Now, my first idea was that when I read, transmit and write the last piece, which is less than the length of the buffer, I also write a lot of zeros. So I tried to resize the last buffer accordingly. Then I tried this using a small text file in which only HELLO WORLD is written, but when I write it and then open the file, it is empty.

Here is the read and send code, where range [0] is the first part, and range [1] is the last part:

byte[] buffer = new byte[DATA_BUFF_SIZE];
using (Stream input = File.OpenRead(file.Path))
{
    Console.WriteLine("SENT PARTS # ");
    for (int i = range[0]; i <= range[1]; i++)
    {
        Console.Write("PART " + i + ", ");

        if (i == range[1])
        {
            buffer = new byte[input.Length - input.Position];
        }

        input.Position = i * DATA_BUFF_SIZE;
        input.Read(buffer, 0, buffer.Length);

        netStream.Write(buffer, 0, buffer.Length);

    }
    Console.WriteLine("LENGTH = " + input.Length);
}

here is the reception and recording code:

int bytesReceived = 0;
int index = partRange.First;
int partNum = 0;
byte[] receiveBuffer = new byte[BUFF_SIZE];

if (index == partRange.Last)
{
    receiveBuffer = new byte[fileToDownload.Length - index * BUFF_SIZE];
}

while ((bytesReceived = netStream.Read(receiveBuffer, 0, receiveBuffer.Length)) > 0)
{
    Console.Write("INDEX:" + index + ", ");
    output.Position = index * BUFF_SIZE;
    output.Write(receiveBuffer, 0, receiveBuffer.Length);
    index++;
    partNum++;

    if (partNum > (partRange.Last - partRange.First))
    {
        break;
    }

    if (index == partRange.Last)
    {
        receiveBuffer = new byte[fileToDownload.Length - index * BUFF_SIZE];
    }
}
Console.WriteLine();

? , .

, .

+4
3

netStream.Flush();

, , . . . Flush() , .

+1
output.Write(receiveBuffer, 0, receiveBuffer.Length);

output.Write(receiveBuffer, 0, bytesReceived);

netStream.Read receiveBuffer.

, , .

EDIT: , , . (

0

Update:


, . 4.5.1 81920 . Stream.CopyTo :

private void InternalCopyTo(Stream destination, int bufferSize)
{
     Contract.Requires(destination != null);
     Contract.Requires(CanRead);
     Contract.Requires(destination.CanWrite);
     Contract.Requires(bufferSize > 0);

     byte[] buffer = new byte[bufferSize];
     int read;
     while ((read = Read(buffer, 0, buffer.Length)) != 0)
          destination.Write(buffer, 0, read);
}

, :

input.CopyTo(output);

. reset .

using(MemoryStream destination = new MemoryStream())
     using(FileStream source = File.Open("..."))
     {
          source.CopyTo(destination);
     }

:

public static MemoryStream GetStream(this FileStream source)
{
     using(MemoryStream stream = new MemoryStream())
     {
          source.CopyTo(stream);
          return stream;
     }
}

, , , Streams. . Microsoft .

0
source

Source: https://habr.com/ru/post/1622252/


All Articles