Skip to content

Instantly share code, notes, and snippets.

@kosmolot
Created July 8, 2017 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosmolot/29ca797de5408afa3ffebf927ee6bb8e to your computer and use it in GitHub Desktop.
Save kosmolot/29ca797de5408afa3ffebf927ee6bb8e to your computer and use it in GitHub Desktop.
Reads all bytes from an InputStream (Vala)
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