Friday 15 April 2011

c# - Why this chunk reading code doesn't work? -



c# - Why this chunk reading code doesn't work? -

i don't know why, chunked upload doesn't work. first chunk read right, next chunks contain 0.

/// <summary> /// gibt die bytesequenz des angeforderten chunks zurück /// </summary> /// <param name="chunknumber">die position des chunks im array</param> /// <returns>die bytesequenz des angeforderten chunks</returns> byte[] filetochunk(int chunknumber) { byte[] chunkbuffer = new byte[chunksize]; filestream filestream = new filestream( "c:\\users\\patrick\\downloads\\prog\\unityassets\\start.unitypackage" , filemode.open , fileaccess.read ); filestream.seek((chunknumber - 1) * chunksize, seekorigin.begin); while (filestream.read(chunkbuffer, 0, chunkbuffer.length) > 0); if ( filestream.position != filestream.length && filestream.position != filestream.position - 1 ) filestream.close(); homecoming chunkbuffer; }

so, number of chunk should extracted passed argument, , filestream.seek() want go position of chunk asked. said, first phone call of method returns working chunk, other calls homecoming byte[] - arrays filled 0. chunksize 256kb (1024*256 bytes) , file want read has size of ~ 11 mb (= 11000kb), should split (int)math.ceiling((double)(file.filesize / chunksize)) parts.

what doing wrong?

according documentation, filestream.read returns number of bytes read. next loop:

while (filestream.read(chunkbuffer, 0, chunkbuffer.length) > 0);

will run until end of stream. read chunk want should phone call method once:

filestream.read(chunkbuffer, 0, chunkbuffer.length);

also, consider using using statement instead of manually closing file.

c# .net stream chunks

No comments:

Post a Comment