2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 ==============================================================================
26 #include "FileInputStream.h"
30 static int64
water_fileSetPosition (void* handle
, int64 pos
);
32 //==============================================================================
33 FileInputStream::FileInputStream (const File
& f
)
42 int64
FileInputStream::getTotalLength()
44 // You should always check that a stream opened successfully before using it!
47 return file
.getSize();
50 int FileInputStream::read (void* buffer
, int bytesToRead
)
52 // You should always check that a stream opened successfully before using it!
55 // The buffer should never be null, and a negative size is probably a
56 // sign that something is broken!
57 wassert (buffer
!= nullptr && bytesToRead
>= 0);
59 const size_t num
= readInternal (buffer
, (size_t) bytesToRead
);
60 currentPosition
+= (int64
) num
;
65 bool FileInputStream::isExhausted()
67 return currentPosition
>= getTotalLength();
70 int64
FileInputStream::getPosition()
72 return currentPosition
;
75 bool FileInputStream::setPosition (int64 pos
)
77 // You should always check that a stream opened successfully before using it!
80 if (pos
!= currentPosition
)
81 currentPosition
= water_fileSetPosition (fileHandle
, pos
);
83 return currentPosition
== pos
;
87 FileInputStream::~FileInputStream()
89 CloseHandle ((HANDLE
) fileHandle
);
92 void FileInputStream::openHandle()
94 HANDLE h
= CreateFileA (file
.getFullPathName().toUTF8(), GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0,
95 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_SEQUENTIAL_SCAN
, 0);
97 if (h
!= INVALID_HANDLE_VALUE
)
98 fileHandle
= (void*) h
;
100 status
= getResultForLastError();
103 size_t FileInputStream::readInternal (void* buffer
, size_t numBytes
)
108 if (! ReadFile ((HANDLE
) fileHandle
, buffer
, (DWORD
) numBytes
, &actualNum
, 0))
110 status
= getResultForLastError();
114 return (size_t) actualNum
;
120 FileInputStream::~FileInputStream()
123 close (getFD (fileHandle
));
126 void FileInputStream::openHandle()
128 const int f
= open (file
.getFullPathName().toUTF8(), O_RDONLY
, 00644);
131 fileHandle
= fdToVoidPointer (f
);
133 status
= getResultForErrno();
136 size_t FileInputStream::readInternal (void* const buffer
, const size_t numBytes
)
142 result
= ::read (getFD (fileHandle
), buffer
, numBytes
);
146 status
= getResultForErrno();
151 return (size_t) result
;