-
Notifications
You must be signed in to change notification settings - Fork 5
/
InputStream.cpp
50 lines (39 loc) · 868 Bytes
/
InputStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "InputStream.hpp"
#include "MemoryFile.hpp"
#include "PartFile.hpp"
#include "Exception.hpp"
#include <algorithm>
BEGIN_INANITY
bigsize_t InputStream::Skip(bigsize_t size)
{
BEGIN_TRY();
char buffer[0x1000];
bigsize_t skipped = 0;
while(size)
{
size_t read = Read(buffer, (size_t)std::min<bigsize_t>(size, sizeof(buffer)));
if(!read)
break;
size -= read;
skipped += read;
}
return skipped;
END_TRY("Can't skip data in input stream");
}
ptr<File> InputStream::Read(size_t size)
{
BEGIN_TRY();
// read data
ptr<File> file = NEW(MemoryFile(size));
size_t read = Read(file->GetData(), size);
// if read less data than requested, slice the file
if(read < size)
file = file->Slice(0, read);
return file;
END_TRY("Can't read data as file from input stream");
}
bool InputStream::IsAtEnd() const
{
return false;
}
END_INANITY