OpenShot Library | libopenshot-audio  0.2.0
juce_FileOutputStream.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
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.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 int64 juce_fileSetPosition (void* handle, int64 pos);
27 
28 //==============================================================================
29 FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
30  : file (f),
31  bufferSize (bufferSizeToUse),
32  buffer (jmax (bufferSizeToUse, (size_t) 16))
33 {
34  openHandle();
35 }
36 
38 {
39  flushBuffer();
40  closeHandle();
41 }
42 
44 {
45  return currentPosition;
46 }
47 
48 bool FileOutputStream::setPosition (int64 newPosition)
49 {
50  if (newPosition != currentPosition)
51  {
52  flushBuffer();
53  currentPosition = juce_fileSetPosition (fileHandle, newPosition);
54  }
55 
56  return newPosition == currentPosition;
57 }
58 
59 bool FileOutputStream::flushBuffer()
60 {
61  bool ok = true;
62 
63  if (bytesInBuffer > 0)
64  {
65  ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer);
66  bytesInBuffer = 0;
67  }
68 
69  return ok;
70 }
71 
73 {
74  flushBuffer();
75  flushInternal();
76 }
77 
78 bool FileOutputStream::write (const void* const src, const size_t numBytes)
79 {
80  jassert (src != nullptr && ((ssize_t) numBytes) >= 0);
81 
82  if (bytesInBuffer + numBytes < bufferSize)
83  {
84  memcpy (buffer + bytesInBuffer, src, numBytes);
85  bytesInBuffer += numBytes;
86  currentPosition += (int64) numBytes;
87  }
88  else
89  {
90  if (! flushBuffer())
91  return false;
92 
93  if (numBytes < bufferSize)
94  {
95  memcpy (buffer + bytesInBuffer, src, numBytes);
96  bytesInBuffer += numBytes;
97  currentPosition += (int64) numBytes;
98  }
99  else
100  {
101  auto bytesWritten = writeInternal (src, numBytes);
102 
103  if (bytesWritten < 0)
104  return false;
105 
106  currentPosition += (int64) bytesWritten;
107  return bytesWritten == (ssize_t) numBytes;
108  }
109  }
110 
111  return true;
112 }
113 
114 bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
115 {
116  jassert (((ssize_t) numBytes) >= 0);
117 
118  if (bytesInBuffer + numBytes < bufferSize)
119  {
120  memset (buffer + bytesInBuffer, byte, numBytes);
121  bytesInBuffer += numBytes;
122  currentPosition += (int64) numBytes;
123  return true;
124  }
125 
126  return OutputStream::writeRepeatedByte (byte, numBytes);
127 }
128 
129 } // namespace juce
juce::FileOutputStream::flush
void flush() override
If the stream is using a buffer, this will ensure it gets written out to the destination.
Definition: juce_FileOutputStream.cpp:72
juce::FileOutputStream::setPosition
bool setPosition(int64) override
Tries to move the stream's output position.
Definition: juce_FileOutputStream.cpp:48
juce::File
Represents a local file or directory.
Definition: juce_File.h:44
juce::FileOutputStream::writeRepeatedByte
bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat) override
Writes a byte to the output stream a given number of times.
Definition: juce_FileOutputStream.cpp:114
juce::OutputStream::writeRepeatedByte
virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat)
Writes a byte to the output stream a given number of times.
Definition: juce_OutputStream.cpp:77
juce::FileOutputStream::write
bool write(const void *, size_t) override
Writes a block of data to the stream.
Definition: juce_FileOutputStream.cpp:78
juce::FileOutputStream::FileOutputStream
FileOutputStream(const File &fileToWriteTo, size_t bufferSizeToUse=16384)
Creates a FileOutputStream.
Definition: juce_FileOutputStream.cpp:29
juce::FileOutputStream::getPosition
int64 getPosition() override
Returns the stream's current position.
Definition: juce_FileOutputStream.cpp:43
juce::FileOutputStream::~FileOutputStream
~FileOutputStream() override
Destructor.
Definition: juce_FileOutputStream.cpp:37