OpenShot Library | OpenShotAudio  0.2.2
juce_InputStream.h
1 
2 /** @weakgroup juce_core-streams
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /** The base class for streams that read data.
32 
33  Input and output streams are used throughout the library - subclasses can override
34  some or all of the virtual functions to implement their behaviour.
35 
36  @see OutputStream, MemoryInputStream, BufferedInputStream, FileInputStream
37 
38  @tags{Core}
39 */
41 {
42 public:
43  /** Destructor. */
44  virtual ~InputStream() = default;
45 
46  //==============================================================================
47  /** Returns the total number of bytes available for reading in this stream.
48 
49  Note that this is the number of bytes available from the start of the
50  stream, not from the current position.
51 
52  If the size of the stream isn't actually known, this will return -1.
53 
54  @see getNumBytesRemaining
55  */
56  virtual int64 getTotalLength() = 0;
57 
58  /** Returns the number of bytes available for reading, or a negative value if
59  the remaining length is not known.
60  @see getTotalLength
61  */
62  int64 getNumBytesRemaining();
63 
64  /** Returns true if the stream has no more data to read. */
65  virtual bool isExhausted() = 0;
66 
67  //==============================================================================
68  /** Reads some data from the stream into a memory buffer.
69 
70  This is the only read method that subclasses actually need to implement, as the
71  InputStream base class implements the other read methods in terms of this one (although
72  it's often more efficient for subclasses to implement them directly).
73 
74  @param destBuffer the destination buffer for the data. This must not be null.
75  @param maxBytesToRead the maximum number of bytes to read - make sure the
76  memory block passed in is big enough to contain this
77  many bytes. This value must not be negative.
78 
79  @returns the actual number of bytes that were read, which may be less than
80  maxBytesToRead if the stream is exhausted before it gets that far
81  */
82  virtual int read (void* destBuffer, int maxBytesToRead) = 0;
83 
84  ssize_t read (void* destBuffer, size_t maxBytesToRead);
85 
86  /** Reads a byte from the stream.
87  If the stream is exhausted, this will return zero.
88  @see OutputStream::writeByte
89  */
90  virtual char readByte();
91 
92  /** Reads a boolean from the stream.
93  The bool is encoded as a single byte - non-zero for true, 0 for false.
94  If the stream is exhausted, this will return false.
95  @see OutputStream::writeBool
96  */
97  virtual bool readBool();
98 
99  /** Reads two bytes from the stream as a little-endian 16-bit value.
100  If the next two bytes read are byte1 and byte2, this returns (byte1 | (byte2 << 8)).
101  If the stream is exhausted partway through reading the bytes, this will return zero.
102  @see OutputStream::writeShort, readShortBigEndian
103  */
104  virtual short readShort();
105 
106  /** Reads two bytes from the stream as a little-endian 16-bit value.
107  If the next two bytes read are byte1 and byte2, this returns (byte2 | (byte1 << 8)).
108  If the stream is exhausted partway through reading the bytes, this will return zero.
109  @see OutputStream::writeShortBigEndian, readShort
110  */
111  virtual short readShortBigEndian();
112 
113  /** Reads four bytes from the stream as a little-endian 32-bit value.
114 
115  If the next four bytes are byte1 to byte4, this returns
116  (byte1 | (byte2 << 8) | (byte3 << 16) | (byte4 << 24)).
117 
118  If the stream is exhausted partway through reading the bytes, this will return zero.
119 
120  @see OutputStream::writeInt, readIntBigEndian
121  */
122  virtual int readInt();
123 
124  /** Reads four bytes from the stream as a big-endian 32-bit value.
125 
126  If the next four bytes are byte1 to byte4, this returns
127  (byte4 | (byte3 << 8) | (byte2 << 16) | (byte1 << 24)).
128 
129  If the stream is exhausted partway through reading the bytes, this will return zero.
130 
131  @see OutputStream::writeIntBigEndian, readInt
132  */
133  virtual int readIntBigEndian();
134 
135  /** Reads eight bytes from the stream as a little-endian 64-bit value.
136 
137  If the next eight bytes are byte1 to byte8, this returns
138  (byte1 | (byte2 << 8) | (byte3 << 16) | (byte4 << 24) | (byte5 << 32) | (byte6 << 40) | (byte7 << 48) | (byte8 << 56)).
139 
140  If the stream is exhausted partway through reading the bytes, this will return zero.
141 
142  @see OutputStream::writeInt64, readInt64BigEndian
143  */
144  virtual int64 readInt64();
145 
146  /** Reads eight bytes from the stream as a big-endian 64-bit value.
147 
148  If the next eight bytes are byte1 to byte8, this returns
149  (byte8 | (byte7 << 8) | (byte6 << 16) | (byte5 << 24) | (byte4 << 32) | (byte3 << 40) | (byte2 << 48) | (byte1 << 56)).
150 
151  If the stream is exhausted partway through reading the bytes, this will return zero.
152 
153  @see OutputStream::writeInt64BigEndian, readInt64
154  */
155  virtual int64 readInt64BigEndian();
156 
157  /** Reads four bytes as a 32-bit floating point value.
158  The raw 32-bit encoding of the float is read from the stream as a little-endian int.
159  If the stream is exhausted partway through reading the bytes, this will return zero.
160  @see OutputStream::writeFloat, readDouble
161  */
162  virtual float readFloat();
163 
164  /** Reads four bytes as a 32-bit floating point value.
165  The raw 32-bit encoding of the float is read from the stream as a big-endian int.
166  If the stream is exhausted partway through reading the bytes, this will return zero.
167  @see OutputStream::writeFloatBigEndian, readDoubleBigEndian
168  */
169  virtual float readFloatBigEndian();
170 
171  /** Reads eight bytes as a 64-bit floating point value.
172  The raw 64-bit encoding of the double is read from the stream as a little-endian int64.
173  If the stream is exhausted partway through reading the bytes, this will return zero.
174  @see OutputStream::writeDouble, readFloat
175  */
176  virtual double readDouble();
177 
178  /** Reads eight bytes as a 64-bit floating point value.
179  The raw 64-bit encoding of the double is read from the stream as a big-endian int64.
180  If the stream is exhausted partway through reading the bytes, this will return zero.
181  @see OutputStream::writeDoubleBigEndian, readFloatBigEndian
182  */
183  virtual double readDoubleBigEndian();
184 
185  /** Reads an encoded 32-bit number from the stream using a space-saving compressed format.
186  For small values, this is more space-efficient than using readInt() and OutputStream::writeInt()
187  The format used is: number of significant bytes + up to 4 bytes in little-endian order.
188  @see OutputStream::writeCompressedInt()
189  */
190  virtual int readCompressedInt();
191 
192  //==============================================================================
193  /** Reads a UTF-8 string from the stream, up to the next linefeed or carriage return.
194 
195  This will read up to the next "\n" or "\r\n" or end-of-stream.
196 
197  After this call, the stream's position will be left pointing to the next character
198  following the line-feed, but the linefeeds aren't included in the string that
199  is returned.
200  */
201  virtual String readNextLine();
202 
203  /** Reads a zero-terminated UTF-8 string from the stream.
204 
205  This will read characters from the stream until it hits a null character
206  or end-of-stream.
207 
208  @see OutputStream::writeString, readEntireStreamAsString
209  */
210  virtual String readString();
211 
212  /** Tries to read the whole stream and turn it into a string.
213 
214  This will read from the stream's current position until the end-of-stream.
215  It can read from UTF-8 data, or UTF-16 if it detects suitable header-bytes.
216  */
217  virtual String readEntireStreamAsString();
218 
219  /** Reads from the stream and appends the data to a MemoryBlock.
220 
221  @param destBlock the block to append the data onto
222  @param maxNumBytesToRead if this is a positive value, it sets a limit to the number
223  of bytes that will be read - if it's negative, data
224  will be read until the stream is exhausted.
225  @returns the number of bytes that were added to the memory block
226  */
227  virtual size_t readIntoMemoryBlock (MemoryBlock& destBlock,
228  ssize_t maxNumBytesToRead = -1);
229 
230  //==============================================================================
231  /** Returns the offset of the next byte that will be read from the stream.
232  @see setPosition
233  */
234  virtual int64 getPosition() = 0;
235 
236  /** Tries to move the current read position of the stream.
237 
238  The position is an absolute number of bytes from the stream's start.
239 
240  Some streams might not be able to do this, in which case they should do
241  nothing and return false. Others might be able to manage it by resetting
242  themselves and skipping to the correct position, although this is
243  obviously a bit slow.
244 
245  @returns true if the stream manages to reposition itself correctly
246  @see getPosition
247  */
248  virtual bool setPosition (int64 newPosition) = 0;
249 
250  /** Reads and discards a number of bytes from the stream.
251 
252  Some input streams might implement this more efficiently, but the base
253  class will just keep reading data until the requisite number of bytes
254  have been done. For large skips it may be quicker to call setPosition()
255  with the required position.
256  */
257  virtual void skipNextBytes (int64 numBytesToSkip);
258 
259 
260 protected:
261  //==============================================================================
262  InputStream() = default;
263 
264 private:
265  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InputStream)
266 };
267 
268 } // namespace juce
269 
270 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
The base class for streams that read data.
The JUCE String class!
Definition: juce_String.h:42
A class to hold a resizable block of raw data.