Created
July 8, 2017 10:32
-
-
Save kosmolot/29ca797de5408afa3ffebf927ee6bb8e to your computer and use it in GitHub Desktop.
Reads all bytes from an InputStream (Vala)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public uint8[] read_all_bytes_from_stream (InputStream stream, ulong buf_size) throws Error { | |
uint8[] data = {}; | |
uint8[] buf = new uint8[buf_size]; | |
ssize_t bytes; | |
ulong total = 0; | |
while (true) { | |
bytes = stream.read(buf); | |
if (bytes <= 0) break; | |
ulong size = (ulong) (total + bytes); | |
if (size > data.length) { | |
ulong new_len = MathUtils.next_pot(size); | |
uint8[] prev = data; | |
data = new uint8[new_len]; | |
Memory.copy(&data[0], &prev[0], total); | |
prev = null; | |
} | |
Memory.copy(&data[0] + total, &buf[0], bytes); | |
total += (ulong) bytes; | |
} | |
return data[0:total]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment