OpenShot Library | libopenshot-audio 0.2.0
juce_FileInputStream.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
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.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26int64 juce_fileSetPosition (void* handle, int64 pos);
27
28
29//==============================================================================
31{
32 openHandle();
33}
34
36{
37 // You should always check that a stream opened successfully before using it!
38 jassert (openedOk());
39
40 return file.getSize();
41}
42
43int FileInputStream::read (void* buffer, int bytesToRead)
44{
45 // You should always check that a stream opened successfully before using it!
46 jassert (openedOk());
47
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);
51
52 auto num = readInternal (buffer, (size_t) bytesToRead);
53 currentPosition += (int64) num;
54
55 return (int) num;
56}
57
59{
60 return currentPosition >= getTotalLength();
61}
62
64{
65 return currentPosition;
66}
67
69{
70 // You should always check that a stream opened successfully before using it!
71 jassert (openedOk());
72
73 if (pos != currentPosition)
74 currentPosition = juce_fileSetPosition (fileHandle, pos);
75
76 return currentPosition == pos;
77}
78
79//==============================================================================
80#if JUCE_UNIT_TESTS
81
82struct FileInputStreamTests : public UnitTest
83{
84 FileInputStreamTests()
85 : UnitTest ("FileInputStream", "Streams")
86 {}
87
88 void runTest() override
89 {
90 const MemoryBlock data ("abcdefghijklmnopqrstuvwxyz", 26);
91 File f (File::createTempFile (".txt"));
92 f.appendData (data.getData(), data.getSize());
93 FileInputStream stream (f);
94
95 beginTest ("Read");
96
97 expectEquals (stream.getPosition(), (int64) 0);
98 expectEquals (stream.getTotalLength(), (int64) data.getSize());
99 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
100 expect (! stream.isExhausted());
101
102 size_t numBytesRead = 0;
103 MemoryBlock readBuffer (data.getSize());
104
105 while (numBytesRead < data.getSize())
106 {
107 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
108
109 expectEquals (stream.getPosition(), (int64) numBytesRead);
110 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
111 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
112 }
113
114 expectEquals (stream.getPosition(), (int64) data.getSize());
115 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
116 expect (stream.isExhausted());
117
118 expect (readBuffer == data);
119
120 beginTest ("Skip");
121
122 stream.setPosition (0);
123 expectEquals (stream.getPosition(), (int64) 0);
124 expectEquals (stream.getTotalLength(), (int64) data.getSize());
125 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
126 expect (! stream.isExhausted());
127
128 numBytesRead = 0;
129 const int numBytesToSkip = 5;
130
131 while (numBytesRead < data.getSize())
132 {
133 stream.skipNextBytes (numBytesToSkip);
134 numBytesRead += numBytesToSkip;
135 numBytesRead = std::min (numBytesRead, data.getSize());
136
137 expectEquals (stream.getPosition(), (int64) numBytesRead);
138 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
139 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
140 }
141
142 expectEquals (stream.getPosition(), (int64) data.getSize());
143 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
144 expect (stream.isExhausted());
145
146 f.deleteFile();
147 }
148};
149
150static FileInputStreamTests fileInputStreamTests;
151
152#endif
153
154} // namespace juce
int64 getPosition() override
Returns the offset of the next byte that will be read from the stream.
bool setPosition(int64) override
Tries to move the current read position of the stream.
int read(void *, int) override
Reads some data from the stream into a memory buffer.
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
bool isExhausted() override
Returns true if the stream has no more data to read.
bool openedOk() const noexcept
Returns true if the stream opened without problems.
FileInputStream(const File &fileToRead)
Creates a FileInputStream to read from the given file.
Represents a local file or directory.
Definition juce_File.h:45
int64 getSize() const
Returns the size of the file in bytes.
This is a base class for classes that perform a unit test.