OpenShot Audio Library | OpenShotAudio  0.3.1
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 (! openedOk())
83  return false;
84 
85  if (bytesInBuffer + numBytes < bufferSize)
86  {
87  memcpy (buffer + bytesInBuffer, src, numBytes);
88  bytesInBuffer += numBytes;
89  currentPosition += (int64) numBytes;
90  }
91  else
92  {
93  if (! flushBuffer())
94  return false;
95 
96  if (numBytes < bufferSize)
97  {
98  memcpy (buffer + bytesInBuffer, src, numBytes);
99  bytesInBuffer += numBytes;
100  currentPosition += (int64) numBytes;
101  }
102  else
103  {
104  auto bytesWritten = writeInternal (src, numBytes);
105 
106  if (bytesWritten < 0)
107  return false;
108 
109  currentPosition += (int64) bytesWritten;
110  return bytesWritten == (ssize_t) numBytes;
111  }
112  }
113 
114  return true;
115 }
116 
117 bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
118 {
119  jassert (((ssize_t) numBytes) >= 0);
120 
121  if (bytesInBuffer + numBytes < bufferSize)
122  {
123  memset (buffer + bytesInBuffer, byte, numBytes);
124  bytesInBuffer += numBytes;
125  currentPosition += (int64) numBytes;
126  return true;
127  }
128 
129  return OutputStream::writeRepeatedByte (byte, numBytes);
130 }
131 
132 } // namespace juce
bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat) override
bool write(const void *, size_t) override
bool setPosition(int64) override
bool openedOk() const noexcept
FileOutputStream(const File &fileToWriteTo, size_t bufferSizeToUse=16384)
virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat)