OpenShot Audio Library | OpenShotAudio 0.3.2
Loading...
Searching...
No Matches
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//==============================================================================
81//==============================================================================
82#if JUCE_UNIT_TESTS
83
84struct FileInputStreamTests : public UnitTest
85{
87 : UnitTest ("FileInputStream", UnitTestCategories::streams)
88 {}
89
90 void runTest() override
91 {
92 const MemoryBlock data ("abcdefghijklmnopqrstuvwxyz", 26);
93 File f (File::createTempFile (".txt"));
94 f.appendData (data.getData(), data.getSize());
95 FileInputStream stream (f);
96
97 beginTest ("Read");
98
99 expectEquals (stream.getPosition(), (int64) 0);
100 expectEquals (stream.getTotalLength(), (int64) data.getSize());
101 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
102 expect (! stream.isExhausted());
103
104 size_t numBytesRead = 0;
105 MemoryBlock readBuffer (data.getSize());
106
107 while (numBytesRead < data.getSize())
108 {
109 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
110
111 expectEquals (stream.getPosition(), (int64) numBytesRead);
112 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
113 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
114 }
115
116 expectEquals (stream.getPosition(), (int64) data.getSize());
117 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
118 expect (stream.isExhausted());
119
120 expect (readBuffer == data);
121
122 beginTest ("Skip");
123
124 stream.setPosition (0);
125 expectEquals (stream.getPosition(), (int64) 0);
126 expectEquals (stream.getTotalLength(), (int64) data.getSize());
127 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
128 expect (! stream.isExhausted());
129
130 numBytesRead = 0;
131 const int numBytesToSkip = 5;
132
133 while (numBytesRead < data.getSize())
134 {
135 stream.skipNextBytes (numBytesToSkip);
137 numBytesRead = std::min (numBytesRead, data.getSize());
138
139 expectEquals (stream.getPosition(), (int64) numBytesRead);
140 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
141 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
142 }
143
144 expectEquals (stream.getPosition(), (int64) data.getSize());
145 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
146 expect (stream.isExhausted());
147
148 f.deleteFile();
149 }
150};
151
152static FileInputStreamTests fileInputStreamTests;
153
154#endif
155
156} // namespace juce
Array()=default
ElementType * data() noexcept
Definition juce_Array.h:360
bool setPosition(int64) override
int read(void *, int) override
bool openedOk() const noexcept
FileInputStream(const File &fileToRead)
static File createTempFile(StringRef fileNameEnding)