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 //==============================================================================
27 /** Wraps another input stream, and reads from it using an intermediate buffer
29 If you're using an input stream such as a file input stream, and making lots of
30 small read accesses to it, it's probably sensible to wrap it in one of these,
31 so that the source stream gets accessed in larger chunk sizes, meaning less
32 work for the underlying stream.
36 class JUCE_API BufferedInputStream
: public InputStream
39 //==============================================================================
40 /** Creates a BufferedInputStream from an input source.
42 @param sourceStream the source stream to read from
43 @param bufferSize the size of reservoir to use to buffer the source
44 @param deleteSourceWhenDestroyed whether the sourceStream that is passed in should be
45 deleted by this object when it is itself deleted.
47 BufferedInputStream (InputStream
* sourceStream
,
49 bool deleteSourceWhenDestroyed
);
51 /** Creates a BufferedInputStream from an input source.
53 @param sourceStream the source stream to read from - the source stream must not
54 be deleted until this object has been destroyed.
55 @param bufferSize the size of reservoir to use to buffer the source
57 BufferedInputStream (InputStream
& sourceStream
, int bufferSize
);
61 This may also delete the source stream, if that option was chosen when the
62 buffered stream was created.
64 ~BufferedInputStream() override
;
67 //==============================================================================
68 /** Returns the next byte that would be read by a call to readByte() */
71 int64
getTotalLength() override
;
72 int64
getPosition() override
;
73 bool setPosition (int64 newPosition
) override
;
74 int read (void* destBuffer
, int maxBytesToRead
) override
;
75 String
readString() override
;
76 bool isExhausted() override
;
80 //==============================================================================
81 OptionalScopedPointer
<InputStream
> source
;
82 Range
<int64
> bufferedRange
;
83 int64 position
, bufferLength
, lastReadPos
= 0, bufferOverlap
= 128;
84 HeapBlock
<char> buffer
;
85 bool ensureBuffered();
87 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferedInputStream
)