2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 int64
juce_fileSetPosition (void* handle
, int64 pos
);
29 //==============================================================================
30 FileInputStream::FileInputStream (const File
& f
) : file (f
)
35 int64
FileInputStream::getTotalLength()
37 // You should always check that a stream opened successfully before using it!
40 return file
.getSize();
43 int FileInputStream::read (void* buffer
, int bytesToRead
)
45 // You should always check that a stream opened successfully before using it!
48 // The buffer should never be null, and a negative size is probably a
49 // sign that something is broken!
50 jassert (buffer
!= nullptr && bytesToRead
>= 0);
52 auto num
= readInternal (buffer
, (size_t) bytesToRead
);
53 currentPosition
+= (int64
) num
;
58 bool FileInputStream::isExhausted()
60 return currentPosition
>= getTotalLength();
63 int64
FileInputStream::getPosition()
65 return currentPosition
;
68 bool FileInputStream::setPosition (int64 pos
)
70 // You should always check that a stream opened successfully before using it!
73 if (pos
!= currentPosition
)
74 currentPosition
= juce_fileSetPosition (fileHandle
, pos
);
76 return currentPosition
== pos
;
80 //==============================================================================
81 //==============================================================================
84 struct FileInputStreamTests
: public UnitTest
86 FileInputStreamTests()
87 : UnitTest ("FileInputStream", UnitTestCategories::streams
)
90 void runTest() override
92 beginTest ("Open stream non-existent file");
94 auto tempFile
= File::createTempFile (".txt");
95 expect (! tempFile
.exists());
97 FileInputStream
stream (tempFile
);
98 expect (stream
.failedToOpen());
101 beginTest ("Open stream existing file");
103 auto tempFile
= File::createTempFile (".txt");
105 expect (tempFile
.exists());
107 FileInputStream
stream (tempFile
);
108 expect (stream
.openedOk());
111 const MemoryBlock
data ("abcdefghijklmnopqrstuvwxyz", 26);
112 File
f (File::createTempFile (".txt"));
113 f
.appendData (data
.getData(), data
.getSize());
114 FileInputStream
stream (f
);
118 expectEquals (stream
.getPosition(), (int64
) 0);
119 expectEquals (stream
.getTotalLength(), (int64
) data
.getSize());
120 expectEquals (stream
.getNumBytesRemaining(), stream
.getTotalLength());
121 expect (! stream
.isExhausted());
123 size_t numBytesRead
= 0;
124 MemoryBlock
readBuffer (data
.getSize());
126 while (numBytesRead
< data
.getSize())
128 numBytesRead
+= (size_t) stream
.read (&readBuffer
[numBytesRead
], 3);
130 expectEquals (stream
.getPosition(), (int64
) numBytesRead
);
131 expectEquals (stream
.getNumBytesRemaining(), (int64
) (data
.getSize() - numBytesRead
));
132 expect (stream
.isExhausted() == (numBytesRead
== data
.getSize()));
135 expectEquals (stream
.getPosition(), (int64
) data
.getSize());
136 expectEquals (stream
.getNumBytesRemaining(), (int64
) 0);
137 expect (stream
.isExhausted());
139 expect (readBuffer
== data
);
144 stream
.setPosition (0);
145 expectEquals (stream
.getPosition(), (int64
) 0);
146 expectEquals (stream
.getTotalLength(), (int64
) data
.getSize());
147 expectEquals (stream
.getNumBytesRemaining(), stream
.getTotalLength());
148 expect (! stream
.isExhausted());
150 size_t numBytesRead
= 0;
151 const int numBytesToSkip
= 5;
153 while (numBytesRead
< data
.getSize())
155 stream
.skipNextBytes (numBytesToSkip
);
156 numBytesRead
+= numBytesToSkip
;
157 numBytesRead
= std::min (numBytesRead
, data
.getSize());
159 expectEquals (stream
.getPosition(), (int64
) numBytesRead
);
160 expectEquals (stream
.getNumBytesRemaining(), (int64
) (data
.getSize() - numBytesRead
));
161 expect (stream
.isExhausted() == (numBytesRead
== data
.getSize()));
164 expectEquals (stream
.getPosition(), (int64
) data
.getSize());
165 expectEquals (stream
.getNumBytesRemaining(), (int64
) 0);
166 expect (stream
.isExhausted());
173 static FileInputStreamTests fileInputStreamTests
;