diff --git a/examples/Audio/AudioRecordingDemo.h b/examples/Audio/AudioRecordingDemo.h index 38f9d8effb..159ee16f2f 100644 --- a/examples/Audio/AudioRecordingDemo.h +++ b/examples/Audio/AudioRecordingDemo.h @@ -78,15 +78,13 @@ public: { // Create an OutputStream to write to our destination file... file.deleteFile(); - std::unique_ptr fileStream (file.createOutputStream()); - if (fileStream.get() != nullptr) + if (auto fileStream = std::unique_ptr (file.createOutputStream())) { // Now create a WAV writer object that writes to our output stream... WavAudioFormat wavFormat; - auto* writer = wavFormat.createWriterFor (fileStream.get(), sampleRate, 1, 16, {}, 0); - if (writer != nullptr) + if (auto writer = wavFormat.createWriterFor (fileStream.get(), sampleRate, 1, 16, {}, 0)) { fileStream.release(); // (passes responsibility for deleting the stream to the writer object that is now using it) diff --git a/examples/GUI/CameraDemo.h b/examples/GUI/CameraDemo.h index 7a9b0ef6a3..6bf79a2d7b 100644 --- a/examples/GUI/CameraDemo.h +++ b/examples/GUI/CameraDemo.h @@ -340,25 +340,25 @@ private: #if JUCE_ANDROID || JUCE_IOS auto imageFile = File::getSpecialLocation (File::tempDirectory).getNonexistentChildFile ("JuceCameraPhotoDemo", ".jpg"); - if (auto stream = std::unique_ptr (imageFile.createOutputStream())) + FileOutputStream stream (imageFile); + + if (stream.openedOk() + && JPEGImageFormat().writeImageToStream (image, stream)) { - if (JPEGImageFormat().writeImageToStream (image, *stream)) - { - URL url (imageFile); + URL url (imageFile); - snapshotButton .setEnabled (false); - recordMovieButton.setEnabled (false); - contentSharingPending = true; + snapshotButton .setEnabled (false); + recordMovieButton.setEnabled (false); + contentSharingPending = true; - SafePointer safeThis (this); + SafePointer safeThis (this); - juce::ContentSharer::getInstance()->shareFiles ({url}, - [safeThis] (bool success, const String&) mutable - { - if (safeThis) - safeThis->sharingFinished (success, true); - }); - } + juce::ContentSharer::getInstance()->shareFiles ({url}, + [safeThis] (bool success, const String&) mutable + { + if (safeThis) + safeThis->sharingFinished (success, true); + }); } #endif } diff --git a/examples/GUI/DialogsDemo.h b/examples/GUI/DialogsDemo.h index 65b9717be1..67ffdc0bef 100644 --- a/examples/GUI/DialogsDemo.h +++ b/examples/GUI/DialogsDemo.h @@ -335,9 +335,11 @@ private: fileToSave = fileToSave.getChildFile ("JUCE.png"); fileToSave.deleteFile(); - std::unique_ptr outStream (fileToSave.createOutputStream()); - std::unique_ptr inStream (createAssetInputStream ("juce_icon.png")); - outStream->writeFromInputStream (*inStream, -1); + FileOutputStream outStream (fileToSave); + + if (outStream.openedOk()) + if (auto inStream = std::unique_ptr (createAssetInputStream ("juce_icon.png"))) + outStream.writeFromInputStream (*inStream, -1); } fc.reset (new FileChooser ("Choose a file to save...", diff --git a/examples/Utilities/NetworkingDemo.h b/examples/Utilities/NetworkingDemo.h index ce35429b0e..830011041d 100644 --- a/examples/Utilities/NetworkingDemo.h +++ b/examples/Utilities/NetworkingDemo.h @@ -103,15 +103,16 @@ public: StringPairArray responseHeaders; int statusCode = 0; - std::unique_ptr stream (url.createInputStream (false, nullptr, nullptr, {}, - 10000, // timeout in millisecs - &responseHeaders, &statusCode)); - if (stream.get() != nullptr) + if (auto stream = std::unique_ptr (url.createInputStream (false, nullptr, nullptr, {}, + 10000, // timeout in millisecs + &responseHeaders, &statusCode))) + { return (statusCode != 0 ? "Status code: " + String (statusCode) + newLine : String()) + "Response headers: " + newLine + responseHeaders.getDescription() + newLine + "----------------------------------------------------" + newLine + stream->readEntireStreamAsString(); + } if (statusCode != 0) return "Failed to connect, status code = " + String (statusCode); diff --git a/extras/Projucer/Source/Application/jucer_CommandLine.cpp b/extras/Projucer/Source/Application/jucer_CommandLine.cpp index 3ee7584a67..d498a83d33 100644 --- a/extras/Projucer/Source/Application/jucer_CommandLine.cpp +++ b/extras/Projucer/Source/Application/jucer_CommandLine.cpp @@ -279,13 +279,15 @@ namespace std::cout << "Writing: " << targetFile.getFullPathName() << std::endl; TemporaryFile temp (targetFile); - std::unique_ptr out (temp.getFile().createOutputStream()); - bool ok = out != nullptr && zip.writeToStream (*out, nullptr); - out.reset(); - ok = ok && temp.overwriteTargetFileWithTemporary(); + { + FileOutputStream out (temp.getFile()); - if (! ok) + if (! (out.openedOk() && zip.writeToStream (out, nullptr))) + ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName()); + } + + if (! temp.overwriteTargetFileWithTemporary()) ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName()); } diff --git a/extras/Projucer/Source/CodeEditor/jucer_ItemPreviewComponent.h b/extras/Projucer/Source/CodeEditor/jucer_ItemPreviewComponent.h index 507d2e7da0..0cfc8e7935 100644 --- a/extras/Projucer/Source/CodeEditor/jucer_ItemPreviewComponent.h +++ b/extras/Projucer/Source/CodeEditor/jucer_ItemPreviewComponent.h @@ -82,33 +82,29 @@ private: facts.add (file.getFullPathName()); drawable.reset(); + if (auto input = std::unique_ptr (file.createInputStream())) { - std::unique_ptr input (file.createInputStream()); + auto totalSize = input->getTotalLength(); + String formatName; - if (input != nullptr) + if (auto* format = ImageFileFormat::findImageFormatForStream (*input)) + formatName = " " + format->getFormatName(); + + input.reset(); + + auto image = ImageCache::getFromFile (file); + + if (image.isValid()) { - auto totalSize = input->getTotalLength(); - String formatName; + auto* d = new DrawableImage(); + d->setImage (image); + drawable.reset (d); - if (auto* format = ImageFileFormat::findImageFormatForStream (*input)) - formatName = " " + format->getFormatName(); - - input.reset(); - - auto image = ImageCache::getFromFile (file); - - if (image.isValid()) - { - auto* d = new DrawableImage(); - d->setImage (image); - drawable.reset (d); - - facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName); - } - - if (totalSize > 0) - facts.add (File::descriptionOfSizeInBytes (totalSize)); + facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName); } + + if (totalSize > 0) + facts.add (File::descriptionOfSizeInBytes (totalSize)); } if (drawable == nullptr) diff --git a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp index ae46ec2a0b..3937c72b04 100644 --- a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp +++ b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp @@ -486,9 +486,7 @@ int OggVorbisAudioFormat::estimateOggFileQuality (const File& source) { if (auto* in = source.createInputStream()) { - std::unique_ptr r (createReaderFor (in, true)); - - if (r != nullptr) + if (auto r = std::unique_ptr (createReaderFor (in, true))) { auto lengthSecs = r->lengthInSamples / r->sampleRate; auto approxBitsPerSecond = (int) (source.getSize() * 8 / lengthSecs); diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h index 9231399dbb..52ef95233f 100644 --- a/modules/juce_core/files/juce_File.h +++ b/modules/juce_core/files/juce_File.h @@ -607,6 +607,17 @@ public: //============================================================================== /** Creates a stream to read from this file. + Note that this is an old method, and actually it's usually best to avoid it and + instead use an RAII pattern with an FileInputStream directly, e.g. + @code + FileInputStream input (fileToOpen); + + if (input.openedOk()) + { + input.read (etc... + } + @endcode + @returns a stream that will read from this file (initially positioned at the start of the file), or nullptr if the file can't be opened for some reason @see createOutputStream, loadFileAsData @@ -615,13 +626,28 @@ public: /** Creates a stream to write to this file. + Note that this is an old method, and actually it's usually best to avoid it and + instead use an RAII pattern with an FileOutputStream directly, e.g. + @code + FileOutputStream output (fileToOpen); + + if (output.openedOk()) + { + output.read etc... + } + @endcode + If the file exists, the stream that is returned will be positioned ready for writing at the end of the file. If you want to write to the start of the file, replacing the existing content, then you can do the following: @code - auto* stream = file.createOutputStream(); - stream->setPosition (0); - stream->truncate(); + FileOutputStream output (fileToOverwrite); + + if (output.openedOk()) + { + output.setPosition (0); + output.truncate(); + ... @endcode @returns a stream that will write to this file (initially positioned at the diff --git a/modules/juce_core/files/juce_FileOutputStream.h b/modules/juce_core/files/juce_FileOutputStream.h index f3729186f8..eb73e471e8 100644 --- a/modules/juce_core/files/juce_FileOutputStream.h +++ b/modules/juce_core/files/juce_FileOutputStream.h @@ -42,9 +42,19 @@ public: does not exist), the failedToOpen() method will return true. If the file already exists when opened, the stream's write-position will - be set to the end of the file. To overwrite an existing file, - use File::deleteFile() before opening the stream, or use setPosition(0) - after it's opened (although this won't truncate the file). + be set to the end of the file. To overwrite an existing file, you can truncate + it like this: + + @code + FileOutputStream stream (file); + + if (stream.openedOk()) + { + stream.setPosition (0); + stream.truncate(); + ... + @endcode + Destroying a FileOutputStream object does not force the operating system to write the buffered data to disk immediately. If this is required you diff --git a/modules/juce_core/files/juce_TemporaryFile.h b/modules/juce_core/files/juce_TemporaryFile.h index f9a9283829..8629193b99 100644 --- a/modules/juce_core/files/juce_TemporaryFile.h +++ b/modules/juce_core/files/juce_TemporaryFile.h @@ -41,12 +41,10 @@ namespace juce TemporaryFile temp (myTargetFile); // create a stream to the temporary file, and write some data to it... - std::unique_ptr out (temp.getFile().createOutputStream()); - - if (out != nullptr) + if (auto out = std::unique_ptr (temp.getFile().createOutputStream())) { out->write ( ...etc ) - out = nullptr; // (deletes the stream) + out.reset(); // (deletes the stream) // ..now we've finished writing, this will rename the temp file to // make it replace the target file we specified above. diff --git a/modules/juce_core/network/juce_URL.cpp b/modules/juce_core/network/juce_URL.cpp index 9cd676c748..6611031738 100644 --- a/modules/juce_core/network/juce_URL.cpp +++ b/modules/juce_core/network/juce_URL.cpp @@ -118,9 +118,7 @@ URL::DownloadTask* URL::DownloadTask::createFallbackDownloader (const URL& urlTo const size_t bufferSize = 0x8000; targetFileToUse.deleteFile(); - std::unique_ptr outputStream (targetFileToUse.createOutputStream (bufferSize)); - - if (outputStream != nullptr) + if (auto outputStream = std::unique_ptr (targetFileToUse.createOutputStream (bufferSize))) { std::unique_ptr stream (new WebInputStream (urlToUse, usePostRequest)); stream->withExtraHeaders (extraHeadersToUse); diff --git a/modules/juce_gui_basics/filebrowser/juce_ContentSharer.cpp b/modules/juce_gui_basics/filebrowser/juce_ContentSharer.cpp index dcd368b7de..9759956dac 100644 --- a/modules/juce_gui_basics/filebrowser/juce_ContentSharer.cpp +++ b/modules/juce_gui_basics/filebrowser/juce_ContentSharer.cpp @@ -111,9 +111,7 @@ private: if (tempFile.create().wasOk()) { - std::unique_ptr outputStream (tempFile.createOutputStream()); - - if (outputStream != nullptr) + if (auto outputStream = std::unique_ptr (tempFile.createOutputStream())) { size_t pos = 0; size_t totalSize = data.getSize(); diff --git a/modules/juce_gui_basics/filebrowser/juce_ImagePreviewComponent.cpp b/modules/juce_gui_basics/filebrowser/juce_ImagePreviewComponent.cpp index 17c564cce3..5d65ae8643 100644 --- a/modules/juce_gui_basics/filebrowser/juce_ImagePreviewComponent.cpp +++ b/modules/juce_gui_basics/filebrowser/juce_ImagePreviewComponent.cpp @@ -38,12 +38,12 @@ ImagePreviewComponent::~ImagePreviewComponent() //============================================================================== void ImagePreviewComponent::getThumbSize (int& w, int& h) const { - const int availableW = proportionOfWidth (0.97f); - const int availableH = getHeight() - 13 * 4; + auto availableW = proportionOfWidth (0.97f); + auto availableH = getHeight() - 13 * 4; - const double scale = jmin (1.0, - availableW / (double) w, - availableH / (double) h); + auto scale = jmin (1.0, + availableW / (double) w, + availableH / (double) h); w = roundToInt (scale * w); h = roundToInt (scale * h); @@ -66,18 +66,18 @@ void ImagePreviewComponent::timerCallback() currentDetails.clear(); repaint(); - std::unique_ptr in (fileToLoad.createInputStream()); + FileInputStream in (fileToLoad); - if (in != nullptr && in->getFile().existsAsFile()) + if (in.openedOk() && fileToLoad.existsAsFile()) { - if (ImageFileFormat* const format = ImageFileFormat::findImageFormatForStream (*in)) + if (auto format = ImageFileFormat::findImageFormatForStream (in)) { - currentThumbnail = format->decodeImage (*in); + currentThumbnail = format->decodeImage (in); if (currentThumbnail.isValid()) { - int w = currentThumbnail.getWidth(); - int h = currentThumbnail.getHeight(); + auto w = currentThumbnail.getWidth(); + auto h = currentThumbnail.getHeight(); currentDetails << fileToLoad.getFileName() << "\n" @@ -99,13 +99,13 @@ void ImagePreviewComponent::paint (Graphics& g) { g.setFont (13.0f); - int w = currentThumbnail.getWidth(); - int h = currentThumbnail.getHeight(); + auto w = currentThumbnail.getWidth(); + auto h = currentThumbnail.getHeight(); getThumbSize (w, h); const int numLines = 4; - const int totalH = 13 * numLines + h + 4; - const int y = (getHeight() - totalH) / 2; + auto totalH = 13 * numLines + h + 4; + auto y = (getHeight() - totalH) / 2; g.drawImageWithin (currentThumbnail, (getWidth() - w) / 2, y, w, h,