0

I'm developing software to send and receive files.

The file is divided into small parts to be sent on the network. Initially I sent byte arrays directly, without headers or anything like that. But now I have seen that it is possible to send byte arrays through the JSON. The JSON now encodes the bytes in base64 and creates a text string. The problem is that this text string far exceeds the actual length of the bytes entered.

For example, a file of 200 mb size, encoded in base64, increases to a size of about 300 mb. Even if once reconverted to bytes the value returns to 200 mb, to send it, the extra 100 mb is a problem.

The class to be serialized :

[JsonProperty]
public long filePointer { get; set; }

[JsonProperty]
public byte[] fileData { get; set; }

The main code :

FilePart filePart = new FilePart {
    filePointer = getFilePointer(),
    fileData = getFileData()
};

JsonConvert.SerializeObject(filePart);

I would like to know if there is a way to optimize this length increment or if there are better ways to send byte arrays.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Why not streaming the file? I think your approach is very very slow. – Transcendent Dec 19 '18 at 14:48
  • I think Transcendent has it right. Straight bytes will be fastest. But perhaps you NEED to send Json (not up to me to determine your business case). If so, you've got more text to send. Now you have a trade-off. You're chewing up bandwidth; longer send times. You might investigate options like compressing json locally (text compresses very well), sending it, then decompressing on the other end. Yeah, you're spending some more time locally, but you're not bogging down the wire as long. Bottom line, there are trade-offs everywhere. – markaaronky Dec 19 '18 at 15:29
  • You might look at [Binary Data in JSON String. Something better than Base64](https://stackoverflow.com/q/1443158) or [What is the most efficient binary to text encoding?](https://stackoverflow.com/q/971211). – dbc Dec 19 '18 at 20:38

0 Answers0