[Vala] Chunking a file in multiple chunks



Hi,

I'm totally new to the Vala language, so there might be something
obvious I'm missing. I'm trying to chunk a file into different chunks of
500 k (in order to send it over the network) and then calculate its
md5sum (to send the md5sum over the network to a service to allow it to
verify the file was sent correctly).

Here is the code I've written so far:

var file = File.new_for_path(source_file);
var file_stream = file.read();
var data_stream = new DataInputStream(file_stream);
data_stream.set_byte_order (DataStreamByteOrder.LITTLE_ENDIAN);
size_t chunk_size = 500000;
size_t bytes_read = 500000;
string[] chunks = new string[0];
uint8[] chunk = new uint8[chunk_size];
string photo_data = "";
var bytes = new ByteArray();
string original_sum;
string chunk_string = "";
do {
        data_stream.read_all(chunk, chunk_size, out bytes_read);
        chunk_string = (string) chunk;
        bytes.append(chunk);
        chunks += chunk_string;
        
        debug("bytes read: %Zd", bytes_read);
        debug("string size: %Zd", chunk_string.size());
}
while (bytes_read == 500000);
      
original_sum = Checksum.compute_for_data(ChecksumType.MD5, bytes.data);
debug("Original sum: " + original_sum);


The number of bytes read in the first debug call seems fine (the file
I'm trying to read is 1.9 Mb and I get 4 "bytes read" outputs, which
correspond to the size of the file). However, the "string size" seems
wrong (the first chunk gets a size of 4, the second one 25 etc... and
finally, the original_sum computed is totally wrong as well (does not
correspond to the md5sum of the file).

Any help would be greatly appreciated.

Thanks a lot.

Guillaume Viguier-Just




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]