0

I need to stream bytes over a socket which includes a binary file data. I would like to have a the file name and its size at the beginning of the stream so at the receiving point I can extract them.

How can I slice up the byte array into three parts based on a pre-defined pattern at the destination?

| String as byte | Integer as byte | byte
| Filename       | Size            | data     
3
  • How can you tell the end of the filename? Is it null-terminated, or of a fixed size? Same with the file size—are you only sending files up to length 255, or might the numeric file size take multiple bytes? Commented Jun 16, 2014 at 15:26
  • @JeffBowman, I was thinking to have namestart[Filename]nameend pattern. Commented Jun 16, 2014 at 15:28
  • 1
    Thanks! Adding that detail to your question will help. Adding those kind of tags do put you on the hook for escaping, though, or else you may have trouble representing filenames similar to your tags—prefacing with "filename length" may be more robust. Commented Jun 16, 2014 at 15:32

1 Answer 1

1

A potential solution would be to have some meta-data in the transmission, such as the length of the filename/size in bytes. The structure of the transmission could look like:

|num_bytes_in_file_name|num_bytes_in_size_of_file|filename_in_bytes ....|size_in_bytes...|data...|

The purpose of tracking the number of bytes is so that the index interval can be found. The first value of the byte array gives you the length of the filename, the second value the size of the file. Then, index 2 though 2+num_bytes_in_file_name contain the filename, 2+num_bytes_in_file_name through 2+num_bytes_in_file_name+num_bytes_in_size_of_file would contain the size of the file, and everything beyond would be the data.

For example:

index:  [0]     [1]    [2]   [3]    [4]   [5]    [6-...]

    |(3)|(1)|'c'|'a'|'t'|10|(data)|

Then you know that the name is on the interval of [2-4], the size is the first value after that [5], and everything beyond [6-...] is the data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.