Browse Source

Tidied up some comments and usage of File::createOutputStream() and createInputStream()

tags/2021-05-28
jules 7 years ago
parent
commit
302019dd43
13 changed files with 112 additions and 85 deletions
  1. +2
    -4
      examples/Audio/AudioRecordingDemo.h
  2. +15
    -15
      examples/GUI/CameraDemo.h
  3. +5
    -3
      examples/GUI/DialogsDemo.h
  4. +5
    -4
      examples/Utilities/NetworkingDemo.h
  5. +7
    -5
      extras/Projucer/Source/Application/jucer_CommandLine.cpp
  6. +16
    -20
      extras/Projucer/Source/CodeEditor/jucer_ItemPreviewComponent.h
  7. +1
    -3
      modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp
  8. +29
    -3
      modules/juce_core/files/juce_File.h
  9. +13
    -3
      modules/juce_core/files/juce_FileOutputStream.h
  10. +2
    -4
      modules/juce_core/files/juce_TemporaryFile.h
  11. +1
    -3
      modules/juce_core/network/juce_URL.cpp
  12. +1
    -3
      modules/juce_gui_basics/filebrowser/juce_ContentSharer.cpp
  13. +15
    -15
      modules/juce_gui_basics/filebrowser/juce_ImagePreviewComponent.cpp

+ 2
- 4
examples/Audio/AudioRecordingDemo.h View File

@@ -78,15 +78,13 @@ public:
{
// Create an OutputStream to write to our destination file...
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...
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)


+ 15
- 15
examples/GUI/CameraDemo.h View File

@@ -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<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
}


+ 5
- 3
examples/GUI/DialogsDemo.h View File

@@ -335,9 +335,11 @@ private:
fileToSave = fileToSave.getChildFile ("JUCE.png");
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...",


+ 5
- 4
examples/Utilities/NetworkingDemo.h View File

@@ -103,15 +103,16 @@ public:
StringPairArray responseHeaders;
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())
+ "Response headers: " + newLine
+ responseHeaders.getDescription() + newLine
+ "----------------------------------------------------" + newLine
+ stream->readEntireStreamAsString();
}
if (statusCode != 0)
return "Failed to connect, status code = " + String (statusCode);


+ 7
- 5
extras/Projucer/Source/Application/jucer_CommandLine.cpp View File

@@ -279,13 +279,15 @@ namespace
std::cout << "Writing: " << targetFile.getFullPathName() << std::endl;
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());
}


+ 16
- 20
extras/Projucer/Source/CodeEditor/jucer_ItemPreviewComponent.h View File

@@ -82,33 +82,29 @@ private:
facts.add (file.getFullPathName());
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)


+ 1
- 3
modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp View File

@@ -486,9 +486,7 @@ int OggVorbisAudioFormat::estimateOggFileQuality (const File& source)
{
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 approxBitsPerSecond = (int) (source.getSize() * 8 / lengthSecs);


+ 29
- 3
modules/juce_core/files/juce_File.h View File

@@ -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


+ 13
- 3
modules/juce_core/files/juce_FileOutputStream.h View File

@@ -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


+ 2
- 4
modules/juce_core/files/juce_TemporaryFile.h View File

@@ -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<FileOutputStream> out (temp.getFile().createOutputStream());
if (out != nullptr)
if (auto out = std::unique_ptr<FileOutputStream> (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.


+ 1
- 3
modules/juce_core/network/juce_URL.cpp View File

@@ -118,9 +118,7 @@ URL::DownloadTask* URL::DownloadTask::createFallbackDownloader (const URL& urlTo
const size_t bufferSize = 0x8000;
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));
stream->withExtraHeaders (extraHeadersToUse);


+ 1
- 3
modules/juce_gui_basics/filebrowser/juce_ContentSharer.cpp View File

@@ -111,9 +111,7 @@ private:
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 totalSize = data.getSize();


+ 15
- 15
modules/juce_gui_basics/filebrowser/juce_ImagePreviewComponent.cpp View File

@@ -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<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())
{
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,


Loading…
Cancel
Save