| @@ -78,15 +78,13 @@ public: | |||||
| { | { | ||||
| // Create an OutputStream to write to our destination file... | // Create an OutputStream to write to our destination file... | ||||
| file.deleteFile(); | file.deleteFile(); | ||||
| std::unique_ptr<FileOutputStream> fileStream (file.createOutputStream()); | |||||
| if (fileStream.get() != nullptr) | |||||
| if (auto fileStream = std::unique_ptr<FileOutputStream> (file.createOutputStream())) | |||||
| { | { | ||||
| // Now create a WAV writer object that writes to our output stream... | // Now create a WAV writer object that writes to our output stream... | ||||
| WavAudioFormat wavFormat; | 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) | fileStream.release(); // (passes responsibility for deleting the stream to the writer object that is now using it) | ||||
| @@ -340,25 +340,25 @@ private: | |||||
| #if JUCE_ANDROID || JUCE_IOS | #if JUCE_ANDROID || JUCE_IOS | ||||
| auto imageFile = File::getSpecialLocation (File::tempDirectory).getNonexistentChildFile ("JuceCameraPhotoDemo", ".jpg"); | auto imageFile = File::getSpecialLocation (File::tempDirectory).getNonexistentChildFile ("JuceCameraPhotoDemo", ".jpg"); | ||||
| if (auto stream = std::unique_ptr<OutputStream> (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<CameraDemo> safeThis (this); | |||||
| SafePointer<CameraDemo> 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 | #endif | ||||
| } | } | ||||
| @@ -335,9 +335,11 @@ private: | |||||
| fileToSave = fileToSave.getChildFile ("JUCE.png"); | fileToSave = fileToSave.getChildFile ("JUCE.png"); | ||||
| fileToSave.deleteFile(); | fileToSave.deleteFile(); | ||||
| std::unique_ptr<OutputStream> outStream (fileToSave.createOutputStream()); | |||||
| std::unique_ptr<InputStream> inStream (createAssetInputStream ("juce_icon.png")); | |||||
| outStream->writeFromInputStream (*inStream, -1); | |||||
| FileOutputStream outStream (fileToSave); | |||||
| if (outStream.openedOk()) | |||||
| if (auto inStream = std::unique_ptr<InputStream> (createAssetInputStream ("juce_icon.png"))) | |||||
| outStream.writeFromInputStream (*inStream, -1); | |||||
| } | } | ||||
| fc.reset (new FileChooser ("Choose a file to save...", | fc.reset (new FileChooser ("Choose a file to save...", | ||||
| @@ -103,15 +103,16 @@ public: | |||||
| StringPairArray responseHeaders; | StringPairArray responseHeaders; | ||||
| int statusCode = 0; | int statusCode = 0; | ||||
| std::unique_ptr<InputStream> stream (url.createInputStream (false, nullptr, nullptr, {}, | |||||
| 10000, // timeout in millisecs | |||||
| &responseHeaders, &statusCode)); | |||||
| if (stream.get() != nullptr) | |||||
| if (auto stream = std::unique_ptr<InputStream> (url.createInputStream (false, nullptr, nullptr, {}, | |||||
| 10000, // timeout in millisecs | |||||
| &responseHeaders, &statusCode))) | |||||
| { | |||||
| return (statusCode != 0 ? "Status code: " + String (statusCode) + newLine : String()) | return (statusCode != 0 ? "Status code: " + String (statusCode) + newLine : String()) | ||||
| + "Response headers: " + newLine | + "Response headers: " + newLine | ||||
| + responseHeaders.getDescription() + newLine | + responseHeaders.getDescription() + newLine | ||||
| + "----------------------------------------------------" + newLine | + "----------------------------------------------------" + newLine | ||||
| + stream->readEntireStreamAsString(); | + stream->readEntireStreamAsString(); | ||||
| } | |||||
| if (statusCode != 0) | if (statusCode != 0) | ||||
| return "Failed to connect, status code = " + String (statusCode); | return "Failed to connect, status code = " + String (statusCode); | ||||
| @@ -279,13 +279,15 @@ namespace | |||||
| std::cout << "Writing: " << targetFile.getFullPathName() << std::endl; | std::cout << "Writing: " << targetFile.getFullPathName() << std::endl; | ||||
| TemporaryFile temp (targetFile); | TemporaryFile temp (targetFile); | ||||
| std::unique_ptr<FileOutputStream> out (temp.getFile().createOutputStream()); | |||||
| bool ok = out != nullptr && zip.writeToStream (*out, nullptr); | |||||
| out.reset(); | |||||
| ok = ok && temp.overwriteTargetFileWithTemporary(); | |||||
| { | |||||
| FileOutputStream out (temp.getFile()); | |||||
| if (! (out.openedOk() && zip.writeToStream (out, nullptr))) | |||||
| ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName()); | |||||
| } | |||||
| if (! ok) | |||||
| if (! temp.overwriteTargetFileWithTemporary()) | |||||
| ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName()); | ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName()); | ||||
| } | } | ||||
| @@ -82,33 +82,29 @@ private: | |||||
| facts.add (file.getFullPathName()); | facts.add (file.getFullPathName()); | ||||
| drawable.reset(); | drawable.reset(); | ||||
| if (auto input = std::unique_ptr<FileInputStream> (file.createInputStream())) | |||||
| { | { | ||||
| std::unique_ptr<InputStream> input (file.createInputStream()); | |||||
| auto totalSize = input->getTotalLength(); | |||||
| String formatName; | |||||
| if (input != nullptr) | |||||
| { | |||||
| auto totalSize = input->getTotalLength(); | |||||
| String formatName; | |||||
| if (auto* format = ImageFileFormat::findImageFormatForStream (*input)) | |||||
| formatName = " " + format->getFormatName(); | |||||
| input.reset(); | |||||
| if (auto* format = ImageFileFormat::findImageFormatForStream (*input)) | |||||
| formatName = " " + format->getFormatName(); | |||||
| auto image = ImageCache::getFromFile (file); | |||||
| input.reset(); | |||||
| if (image.isValid()) | |||||
| { | |||||
| auto* d = new DrawableImage(); | |||||
| d->setImage (image); | |||||
| drawable.reset (d); | |||||
| auto image = ImageCache::getFromFile (file); | |||||
| facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName); | |||||
| } | |||||
| if (image.isValid()) | |||||
| { | |||||
| auto* d = new DrawableImage(); | |||||
| d->setImage (image); | |||||
| drawable.reset (d); | |||||
| 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) | if (drawable == nullptr) | ||||
| @@ -486,9 +486,7 @@ int OggVorbisAudioFormat::estimateOggFileQuality (const File& source) | |||||
| { | { | ||||
| if (auto* in = source.createInputStream()) | if (auto* in = source.createInputStream()) | ||||
| { | { | ||||
| std::unique_ptr<AudioFormatReader> r (createReaderFor (in, true)); | |||||
| if (r != nullptr) | |||||
| if (auto r = std::unique_ptr<AudioFormatReader> (createReaderFor (in, true))) | |||||
| { | { | ||||
| auto lengthSecs = r->lengthInSamples / r->sampleRate; | auto lengthSecs = r->lengthInSamples / r->sampleRate; | ||||
| auto approxBitsPerSecond = (int) (source.getSize() * 8 / lengthSecs); | auto approxBitsPerSecond = (int) (source.getSize() * 8 / lengthSecs); | ||||
| @@ -607,6 +607,17 @@ public: | |||||
| //============================================================================== | //============================================================================== | ||||
| /** Creates a stream to read from this file. | /** 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 | @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 | start of the file), or nullptr if the file can't be opened for some reason | ||||
| @see createOutputStream, loadFileAsData | @see createOutputStream, loadFileAsData | ||||
| @@ -615,13 +626,28 @@ public: | |||||
| /** Creates a stream to write to this file. | /** 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 | 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, | 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: | replacing the existing content, then you can do the following: | ||||
| @code | @code | ||||
| auto* stream = file.createOutputStream(); | |||||
| stream->setPosition (0); | |||||
| stream->truncate(); | |||||
| FileOutputStream output (fileToOverwrite); | |||||
| if (output.openedOk()) | |||||
| { | |||||
| output.setPosition (0); | |||||
| output.truncate(); | |||||
| ... | |||||
| @endcode | @endcode | ||||
| @returns a stream that will write to this file (initially positioned at the | @returns a stream that will write to this file (initially positioned at the | ||||
| @@ -42,9 +42,19 @@ public: | |||||
| does not exist), the failedToOpen() method will return true. | does not exist), the failedToOpen() method will return true. | ||||
| If the file already exists when opened, the stream's write-position will | 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 | Destroying a FileOutputStream object does not force the operating system | ||||
| to write the buffered data to disk immediately. If this is required you | to write the buffered data to disk immediately. If this is required you | ||||
| @@ -41,12 +41,10 @@ namespace juce | |||||
| TemporaryFile temp (myTargetFile); | TemporaryFile temp (myTargetFile); | ||||
| // create a stream to the temporary file, and write some data to it... | // create a stream to the temporary file, and write some data to it... | ||||
| std::unique_ptr<FileOutputStream> out (temp.getFile().createOutputStream()); | |||||
| if (out != nullptr) | |||||
| if (auto out = std::unique_ptr<FileOutputStream> (temp.getFile().createOutputStream())) | |||||
| { | { | ||||
| out->write ( ...etc ) | 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 | // ..now we've finished writing, this will rename the temp file to | ||||
| // make it replace the target file we specified above. | // make it replace the target file we specified above. | ||||
| @@ -118,9 +118,7 @@ URL::DownloadTask* URL::DownloadTask::createFallbackDownloader (const URL& urlTo | |||||
| const size_t bufferSize = 0x8000; | const size_t bufferSize = 0x8000; | ||||
| targetFileToUse.deleteFile(); | targetFileToUse.deleteFile(); | ||||
| std::unique_ptr<FileOutputStream> outputStream (targetFileToUse.createOutputStream (bufferSize)); | |||||
| if (outputStream != nullptr) | |||||
| if (auto outputStream = std::unique_ptr<FileOutputStream> (targetFileToUse.createOutputStream (bufferSize))) | |||||
| { | { | ||||
| std::unique_ptr<WebInputStream> stream (new WebInputStream (urlToUse, usePostRequest)); | std::unique_ptr<WebInputStream> stream (new WebInputStream (urlToUse, usePostRequest)); | ||||
| stream->withExtraHeaders (extraHeadersToUse); | stream->withExtraHeaders (extraHeadersToUse); | ||||
| @@ -111,9 +111,7 @@ private: | |||||
| if (tempFile.create().wasOk()) | if (tempFile.create().wasOk()) | ||||
| { | { | ||||
| std::unique_ptr<FileOutputStream> outputStream (tempFile.createOutputStream()); | |||||
| if (outputStream != nullptr) | |||||
| if (auto outputStream = std::unique_ptr<FileOutputStream> (tempFile.createOutputStream())) | |||||
| { | { | ||||
| size_t pos = 0; | size_t pos = 0; | ||||
| size_t totalSize = data.getSize(); | size_t totalSize = data.getSize(); | ||||
| @@ -38,12 +38,12 @@ ImagePreviewComponent::~ImagePreviewComponent() | |||||
| //============================================================================== | //============================================================================== | ||||
| void ImagePreviewComponent::getThumbSize (int& w, int& h) const | 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); | w = roundToInt (scale * w); | ||||
| h = roundToInt (scale * h); | h = roundToInt (scale * h); | ||||
| @@ -66,18 +66,18 @@ void ImagePreviewComponent::timerCallback() | |||||
| currentDetails.clear(); | currentDetails.clear(); | ||||
| repaint(); | repaint(); | ||||
| std::unique_ptr<FileInputStream> 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()) | if (currentThumbnail.isValid()) | ||||
| { | { | ||||
| int w = currentThumbnail.getWidth(); | |||||
| int h = currentThumbnail.getHeight(); | |||||
| auto w = currentThumbnail.getWidth(); | |||||
| auto h = currentThumbnail.getHeight(); | |||||
| currentDetails | currentDetails | ||||
| << fileToLoad.getFileName() << "\n" | << fileToLoad.getFileName() << "\n" | ||||
| @@ -99,13 +99,13 @@ void ImagePreviewComponent::paint (Graphics& g) | |||||
| { | { | ||||
| g.setFont (13.0f); | g.setFont (13.0f); | ||||
| int w = currentThumbnail.getWidth(); | |||||
| int h = currentThumbnail.getHeight(); | |||||
| auto w = currentThumbnail.getWidth(); | |||||
| auto h = currentThumbnail.getHeight(); | |||||
| getThumbSize (w, h); | getThumbSize (w, h); | ||||
| const int numLines = 4; | 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, | g.drawImageWithin (currentThumbnail, | ||||
| (getWidth() - w) / 2, y, w, h, | (getWidth() - w) / 2, y, w, h, | ||||