OpenShot Library | libopenshot-audio  0.2.0
juce_SubregionStream.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 
23 namespace juce
24 {
25 
27  int64 start, int64 length,
28  bool deleteSourceWhenDestroyed)
29  : source (sourceStream, deleteSourceWhenDestroyed),
30  startPositionInSourceStream (start),
31  lengthOfSourceStream (length)
32 {
34 }
35 
37 {
38 }
39 
41 {
42  auto srcLen = source->getTotalLength() - startPositionInSourceStream;
43 
44  return lengthOfSourceStream >= 0 ? jmin (lengthOfSourceStream, srcLen)
45  : srcLen;
46 }
47 
49 {
50  return source->getPosition() - startPositionInSourceStream;
51 }
52 
53 bool SubregionStream::setPosition (int64 newPosition)
54 {
55  return source->setPosition (jmax ((int64) 0, newPosition + startPositionInSourceStream));
56 }
57 
58 int SubregionStream::read (void* destBuffer, int maxBytesToRead)
59 {
60  jassert (destBuffer != nullptr && maxBytesToRead >= 0);
61 
62  if (lengthOfSourceStream < 0)
63  return source->read (destBuffer, maxBytesToRead);
64 
65  maxBytesToRead = (int) jmin ((int64) maxBytesToRead, lengthOfSourceStream - getPosition());
66 
67  if (maxBytesToRead <= 0)
68  return 0;
69 
70  return source->read (destBuffer, maxBytesToRead);
71 }
72 
74 {
75  if (lengthOfSourceStream >= 0 && getPosition() >= lengthOfSourceStream)
76  return true;
77 
78  return source->isExhausted();
79 }
80 
81 //==============================================================================
82 #if JUCE_UNIT_TESTS
83 
84 struct SubregionInputStreamTests : public UnitTest
85 {
86  SubregionInputStreamTests()
87  : UnitTest ("SubregionInputStream", "Streams")
88  {}
89 
90  void runTest() override
91  {
92  const MemoryBlock data ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52);
93  MemoryInputStream mi (data, true);
94 
95  const int offset = getRandom().nextInt ((int) data.getSize());
96  const size_t subregionSize = data.getSize() - (size_t) offset;
97 
98  SubregionStream stream (&mi, offset, (int) subregionSize, false);
99 
100  beginTest ("Read");
101 
102  expectEquals (stream.getPosition(), (int64) 0);
103  expectEquals (stream.getTotalLength(), (int64) subregionSize);
104  expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
105  expect (! stream.isExhausted());
106 
107  size_t numBytesRead = 0;
108  MemoryBlock readBuffer (subregionSize);
109 
110  while (numBytesRead < subregionSize)
111  {
112  numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
113 
114  expectEquals (stream.getPosition(), (int64) numBytesRead);
115  expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
116  expect (stream.isExhausted() == (numBytesRead == subregionSize));
117  }
118 
119  expectEquals (stream.getPosition(), (int64) subregionSize);
120  expectEquals (stream.getNumBytesRemaining(), (int64) 0);
121  expect (stream.isExhausted());
122 
123  const MemoryBlock memoryBlockToCheck (data.begin() + (size_t) offset, data.getSize() - (size_t) offset);
124  expect (readBuffer == memoryBlockToCheck);
125 
126  beginTest ("Skip");
127 
128  stream.setPosition (0);
129  expectEquals (stream.getPosition(), (int64) 0);
130  expectEquals (stream.getTotalLength(), (int64) subregionSize);
131  expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
132  expect (! stream.isExhausted());
133 
134  numBytesRead = 0;
135  const int64 numBytesToSkip = 5;
136 
137  while (numBytesRead < subregionSize)
138  {
139  stream.skipNextBytes (numBytesToSkip);
140  numBytesRead += numBytesToSkip;
141  numBytesRead = std::min (numBytesRead, subregionSize);
142 
143  expectEquals (stream.getPosition(), (int64) numBytesRead);
144  expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
145  expect (stream.isExhausted() == (numBytesRead == subregionSize));
146  }
147 
148  expectEquals (stream.getPosition(), (int64) subregionSize);
149  expectEquals (stream.getNumBytesRemaining(), (int64) 0);
150  expect (stream.isExhausted());
151  }
152 };
153 
154 static SubregionInputStreamTests subregionInputStreamTests;
155 
156 #endif
157 
158 } // namespace juce
size_t getSize() const noexcept
Returns the block&#39;s current allocated size, in bytes.
~SubregionStream() override
Destructor.
int64 getPosition() override
Returns the offset of the next byte that will be read from the stream.
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
Wraps another input stream, and reads from a specific part of it.
virtual void skipNextBytes(int64 numBytesToSkip)
Reads and discards a number of bytes from the stream.
The base class for streams that read data.
This is a base class for classes that perform a unit test.
Definition: juce_UnitTest.h:73
bool setPosition(int64 newPosition) override
Tries to move the current read position of the stream.
bool isExhausted() override
Returns true if the stream has no more data to read.
int64 getNumBytesRemaining()
Returns the number of bytes available for reading, or a negative value if the remaining length is not...
SubregionStream(InputStream *sourceStream, int64 startPositionInSourceStream, int64 lengthOfSourceStream, bool deleteSourceWhenDestroyed)
Creates a SubregionStream from an input source.
char * begin() const noexcept
Returns an iterator for the data.
int read(void *destBuffer, int maxBytesToRead) override
Reads some data from the stream into a memory buffer.
A class to hold a resizable block of raw data.
Allows a block of data to be accessed as a stream.