So, I'm writing a byte array (`seq[byte]`) to a FileStream.

Initially, I went for:
    
    
    f.write(len(barr)     # write length of bytearray
    for b in barr:          # write bytes one by one
        f.write(b)
    
    
    Run

which looks unnecessarily verbose and inefficient.

So, I decided to write/read it as data:
    
    
    # writing bytearray to stream
    f.write(len(fbarr))      # write length of bytearray
    f.writeData(addr(barr[0]), barr.len) # write the whole block of barr bytes, 
at once
    
    # reading bytearray back from stream
    var sz: int
    f.read(sz)                      # read length of bytearray
    
    var barr = newSeq[byte](sz)
    discard f.readData(addr(barr[0]), sz) # read the whole block of barr bytes, 
at once
    
    
    Run

It seems to be working fine, but I feel kind-of insecure about it.

Do you see anything wrong with my approach? How would you handle it? 

Reply via email to